import miniraf from 'miniraf';
import { withReadme } from 'storybook-readme';
import { storiesOf } from './facade';
import './style.css';

if (process.env.NODE_ENV !== 'test') {
	global.jest = { mock: () => true, isMock: true };
} else {
	global.requestAnimationFrame = miniraf;
}

/**
 * Story loader utility
 * @param {string} componentPath Component name / path from the components dir
 * @param {function} callback
 */
const StoryLoader = (componentPath, callback) => {
	const stories = storiesOf(componentPath, module);

	// Load the readme for the component
	if (process.env.NODE_ENV !== 'test') {
		const readme = require(`../components/${componentPath}/README.md`);
		stories.addDecorator(withReadme(readme));
	}

	callback(stories);
};

export default StoryLoader;
