"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 _path() {
  const data = require("path");

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

  return data;
}

function _fs() {
  const data = require("fs");

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

  return data;
}

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function _chalk() {
    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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _default(api) {
  const paths = api.paths,
        winPath = api.winPath,
        log = api.log;

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

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

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

        type: 'object',
        default: {
          chrome: 49,
          firefox: 64,
          safari: 10,
          edge: 13,
          ios: 10
        },
        choices: ['chrome', 'opera', 'edge', 'firefox', 'safari', 'ie', 'ios', 'android'],
        group: 'webpack',
        title: {
          'zh-CN': '浏览器兼容性',
          'en-US': 'Browser Compatibility'
        },
        description: {
          'zh-CN': '选择需要兼容的浏览器最低版本，会自动引入 Polyfill 和做语法转换。',
          'en-US': 'The minimum version of browsers you want to compatible with.'
        }
      };
    };
  });

  function writeTmpFile() {
    const tpl = (0, _fs().readFileSync)((0, _path().join)(__dirname, '../../template/polyfills.js.tpl'), 'utf-8');
    const result = api.Mustache.render(tpl, {
      url: api.config.targets && api.config.targets.ie && api.config.targets.ie <= 11,
      url_polyfill_path: winPath((0, _path().relative)(paths.absTmpDirPath, require.resolve('url-polyfill')))
    });
    api.debug(`write tmp file: polyfills.js, content: ${result}`);
    api.writeTmpFile('polyfills.js', result);
  }

  api.onGenerateFiles(() => {
    writeTmpFile();
  });
  api.addEntryPolyfillImports(() => {
    // BABEL_POLYFILL 的判断得放里面，允许插件里通过此环境变量禁用内置的补丁方案
    if (process.env.BABEL_POLYFILL !== 'none') {
      return [{
        source: './polyfills'
      }];
    } else {
      log.warn(_chalk().default.yellow(`Since you have configured the environment variable ${_chalk().default.bold('BABEL_POLYFILL')} to none, no patches will be included.`));
      return [];
    }
  });
  api.chainWebpackConfig(config => {
    if (process.env.BABEL_POLYFILL !== 'none') {
      // 不启用 BABEL_POLYFILL 时不锁 regenerator-runtime
      config.resolve.alias.set('regenerator-runtime', (0, _path().dirname)(require.resolve('regenerator-runtime/package')));
    }
  });
}