"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = _default;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _signale() {
  const data = _interopRequireDefault(require("signale"));

  _signale = function _signale() {
    return data;
  };

  return data;
}

function _lodash() {
  const data = require("lodash");

  _lodash = function _lodash() {
    return data;
  };

  return data;
}

function _assert() {
  const data = _interopRequireDefault(require("assert"));

  _assert = function _assert() {
    return data;
  };

  return data;
}

function _umiMock() {
  const data = require("umi-mock");

  _umiMock = function _umiMock() {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _default(api) {
  const errors = [];

  api._registerConfig(() => {
    return api => {
      return {
        name: 'mock',

        validate(val) {
          (0, _assert().default)((0, _lodash().isPlainObject)(val), `Configure item mock should be Plain Object, but got ${val}.`);
        },

        onChange() {
          api.service.restart(
          /* why */
          'Config mock Changed');
        },

        configs: [{
          group: 'basic',
          name: 'mock.exclude',
          type: 'string[]',
          title: {
            'zh-CN': 'Mock 忽略文件',
            'en-US': 'Ignored Mock Files'
          },
          description: {
            'zh-CN': '指定需要忽略的文件列表，支持 glob 格式。',
            'en-US': 'Support glob pattern.'
          }
        }]
      };
    };
  });

  api._beforeServerWithApp(({
    app
  }) => {
    if (process.env.MOCK !== 'none' && process.env.HTTP_MOCK !== 'none') {
      const beforeMiddlewares = api.applyPlugins('addMiddlewareBeforeMock', {
        initialValue: []
      });
      const afterMiddlewares = api.applyPlugins('addMiddlewareAfterMock', {
        initialValue: []
      });
      beforeMiddlewares.forEach(m => app.use(m));
      const cwd = api.cwd,
            config = api.config,
            _api$paths = api.paths,
            absPagesPath = _api$paths.absPagesPath,
            absSrcPath = _api$paths.absSrcPath;
      app.use((0, _umiMock().createMiddleware)({
        cwd,
        config,
        errors,
        absPagesPath,
        absSrcPath,
        watch: !process.env.WATCH_FILES,

        onStart({
          paths
        }) {
          api.addBabelRegister(paths);
        }

      }).middleware);
      afterMiddlewares.forEach(m => app.use(m));
    }
  });

  api.onDevCompileDone(() => {
    if (errors.length) {
      _signale().default.error(`Mock file parse failed`);

      errors.forEach(e => {
        console.error(e.message);
      });
    }
  });
}