/* eslint global-require: off, import/no-mutable-exports: off */
import merge from 'lodash/merge';
import flow from 'lodash/flow';

const environment = process.env.NODE_ENV || 'test';

let start = () => {};

if (environment !== 'production') {
  const { default: Mirage, camelize } = require('@bigtest/mirage');
  const { default: coreModules } = require('./index');
  require('./force-fetch-polyfill');

  start = (scenarioNames, options = {}) => {
    const { coreScenarios = {}, baseConfig: coreConfig, ...coreOpts } = coreModules;
    const { scenarios = {}, baseConfig = () => {}, ...opts } = options;

    const server = new Mirage(merge({
      baseConfig: flow(coreConfig, baseConfig),
      environment
    }, coreOpts, opts));

    // the default scenario is only used when not in test mode
    let defaultScenario;
    if (!scenarioNames && environment !== 'test') {
      defaultScenario = 'default';
    }

    // mirage only loads a `default` scenario for us out of the box,
    // so instead of providing all scenarios we run specific scenarios
    // after the mirage server is initialized.
    [].concat(scenarioNames || defaultScenario).filter(Boolean).forEach(name => {
      const key = camelize(name);
      const scenario = scenarios[key] || coreScenarios[key];
      if (scenario) scenario(server);
    });

    return server;
  };
}

export default start;

@@ -454,16 +454,33 @@
  const %7B
+%0A      scenarios:
  coreSce
@@ -483,32 +483,75 @@
 eScenarios = %7B%7D,
+%0A      factories: coreFactories = %7B%7D,%0A     
  baseConfig: cor
@@ -558,16 +558,22 @@
 eConfig,
+%0A     
  ...core
@@ -576,16 +576,20 @@
 coreOpts
+%0A   
  %7D = cor
@@ -598,16 +598,17 @@
 odules;%0A
+%0A
     cons
@@ -610,16 +610,22 @@
  const %7B
+%0A     
  scenari
@@ -632,16 +632,44 @@
 os = %7B%7D,
+%0A      factories = %7B%7D,%0A     
  baseCon
@@ -687,16 +687,22 @@
  %7B%7D,
+%0A     
  ...opts
  %7D =
@@ -697,16 +697,20 @@
  ...opts
+%0A   
  %7D = opt
@@ -842,24 +842,205 @@
 s, opts));%0A%0A
+    // mirage conditionally includes factories, we want to include%0A    // all of them unconditionally%0A    server.loadFactories(coreFactories);%0A    server.loadFactories(factories);%0A%0A
     // the d

Fix mirage to always include factories