// @flow
import { configure, addDecorator, addParameters } from '@storybook/react';
import { withA11y } from '@storybook/addon-a11y';
import { themeDecorator } from '../utils/storybook/addons/patchwork-theme/theme.decorator';
import { deriveStorybookTheme } from '../utils/storybook/addons/patchwork-theme/storybook.theme';
import { themes } from '../src';
import { intlDecorator } from './intl.decorator';

addParameters({
  options: {
    // DEFAULT_THEME_NAME is injected by webpack.
    /* eslint-disable no-undef */
    // $FlowFixMe
    theme: deriveStorybookTheme(themes[DEFAULT_THEME_NAME]),
    /* eslint-enable no-undef */
    storySort: (a, b) => a[1].id.localeCompare(b[1].id),
  },
});

const loadDocsStories = () => {
  // $FlowFixMe - flow doesn't like dynamic require because it' can't analyze it.
  const docsStoryContext = require.context('../docs', true, /\.story\.js$/);
  docsStoryContext.keys().forEach(docsStoryContext);
};

const loadThemedStories = () => {
  // $FlowFixMe - flow doesn't like dynamic require because it' can't analyze it.
  const themedStoryContext = require.context('../src', true, /\.story\.js$/);
  themedStoryContext.keys().forEach(themedStoryContext);
};

addDecorator(withA11y);
addDecorator(themeDecorator);
addDecorator(intlDecorator);

configure(() => {
  loadDocsStories();
  loadThemedStories();
}, module);
