/* @flow */
const React = require('react');
const { LiveProvider, LivePreview } = require('react-live');
const { ThemeProvider } = require('../src/themes');

// Exclude components that cannot be rendered in JSDOM.
const excludedComponents = ['Dropdown', 'Popover', 'Select', 'Tooltip'];

function getComponentNameFromPath(path) {
  // ./<component>/examples/index.js
  return path.split('/')[1];
}

// Dynamically require all example files.
/* $FlowFixMe */
const requireContext = require.context(
  '../src/website/app/demos',
  /* useSubdirectories */ true,
  /\/examples\/index\.js$/
);

const allExamples = requireContext
  .keys()
  .map(pathToFile => {
    const component = getComponentNameFromPath(pathToFile);
    if (excludedComponents.includes(component)) {
      return;
    }
    const examples = requireContext(pathToFile);
    const variants = {};
    examples.default.forEach(({ scope, source, id }) => {
      // eslint-disable-next-line react/display-name
      variants[id] = () => (
        <ThemeProvider>
          <LiveProvider code={source} scope={scope} mountStylesheet={false}>
            <LivePreview />
          </LiveProvider>
        </ThemeProvider>
      );
    });
    return { component, variants };
  })
  .filter(Boolean); // filter out empty items

module.exports = allExamples;

/* @flow */
const React = require('react');
const { LiveProvider, LivePreview } = require('react-live');
const { ThemeProvider } = require('../src/themes');
const demos = require('../src/website/app/demos').default;

const excludedComponents = ['Dropdown', 'Form', 'Popover', 'Select', 'Tooltip'];

module.exports = Object.values(demos)
  // $FlowFixMe
  .map(({ examples, title: component }) => {
    if (excludedComponents.includes(component)) {
      return;
    }

    return examples.reduce(
      (acc, { scope, source, id }) => {
        if (source) {
          // eslint-disable-next-line react/display-name
          acc.variants[id] = () => (
            <ThemeProvider>
              <LiveProvider code={source} scope={scope} mountStylesheet={false}>
                <LivePreview />
              </LiveProvider>
            </ThemeProvider>
          );
        }
        return acc;
      },
      { component, variants: {} }
    );
  })
  .filter(Boolean);

Update happo config. Fixes #536