"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 _assert() {
  const data = _interopRequireDefault(require("assert"));

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

  return data;
}

var _getPlugins = require("../../getPlugins");

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

const debug = require('debug')('umi-build-dev:configPlugin:plugins');

function _default(api) {
  return {
    name: 'plugins',

    validate(val) {
      (0, _assert().default)(Array.isArray(val), `Configure item plugins should be Array, but got ${val}.`);
    },

    onChange(newConfig, oldConfig) {
      debug(`plugins changed from ${oldConfig[this.name]} to ${newConfig[this.name]}`);
      const result = (0, _getPlugins.diffPlugins)(newConfig[this.name], oldConfig[this.name], {
        cwd: api.service.cwd
      });

      if (result.pluginsChanged) {
        api.service.restart('Config plugins Changed');
      } else {
        debug(`result.optionChanged: ${result.optionChanged}`);
        result.optionChanged.forEach(({
          id,
          opts
        }) => {
          api.service.changePluginOption(id, opts);
        });
      }
    }

  };
}