text: import { configure, storiesOf, addParameters } from '@storybook/react';
  import { withKnobs } from '@storybook/addon-knobs/react';
  import { withContexts } from '@storybook/addon-contexts/react';
  
  import { capitalCase } from 'change-case';
  import faker from 'faker';
  import viewports from './viewports';
  import contexts from './contexts';
  
  import './styles.css';
  
  const CONFIG_EXPORTS = ['wrapStory'];
  
  function loadStories() {
    const r = require.context('../packages', true, /^(?:\.\/[^\/]+)\/src\/.*\.stories\.jsx?$/);
    r.keys().forEach(m => {
      const storyMod = r(m);
      const mSplit = m.split('/');
      const [, pkg, _, __, folder, story] = mSplit;
      if (mSplit[mSplit.length - 1] !== (story || folder) || !m.startsWith(`./${pkg}/src/components`))
        return;
  
      const storyNameParts = [pkg, folder, story].filter(v => !!v);
      if (storyNameParts.length === 3) storyNameParts[1] = capitalCase(storyNameParts[1]);
      storyNameParts[storyNameParts.length - 1] = storyNameParts[storyNameParts.length - 1].replace(
        /\.stories\.jsx?/,
        '',
      );
      const storyName = storyNameParts.join('/');
  
      const stories = storiesOf(storyName, module)
        .addDecorator(withKnobs)
        .addDecorator(withContexts(contexts));
  
      const applyConfig = storyVariant => {
        const withWrap = storyMod.wrapStory || (s => s);
        return () => {
          faker.seed(1);
          return withWrap(storyVariant());
        };
      };
  
      Object.keys(storyMod)
        .filter(v => !CONFIG_EXPORTS.includes(v))
        .forEach(variant =>
          stories.add(capitalCase(variant), applyConfig(storyMod[variant]), {
            snapshotDelay: storyMod[variant].snapshotDelay,
          }),
        );
    });
  }
  
  addParameters({
    options: {
      showRoots: true,
      storySort: (a, b) => {
        const idA = a[0];
        const idB = b[0];
        const [componentA, storyA] = idA.split('--');
        const [componentB, storyB] = idB.split('--');
  
        if (componentA === componentB && storyA === 'default') return -1;
        if (componentA === componentB && storyB === 'default') return 1;
        if (idA < idB) return -1;
        if (idA > idB) return 1;
        return 0;
      },
    },
    viewport: {
      viewports,
      defaultViewport: 'desktop',
    },
  });
  
  configure(loadStories, module);
  