'use strict';

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

var _lodash = require('lodash');

var _tools = require('views/utils/tools');

var _utils = require('./utils');

function reducer(state = [], { type, value, option }) {
  switch (type) {
    case '@@Plugin/initialize':
      {
        return value;
      }
    case '@@Plugin/add':
      {
        const i = (0, _utils.getPluginIndexByPackageName)(state, value.packageName);
        if (i === -1) {
          state = state.concat(value);
        } else {
          state[i] = value;
        }
        state = (0, _lodash.sortBy)(state, 'priority');
        return state;
      }
    case '@@Plugin/changeStatus':
      {
        const i = (0, _utils.getPluginIndexByPackageName)(state, value.packageName);
        for (const opt of option) {
          const { path, status } = opt;
          state = (0, _tools.reduxSet)(state, [i].concat(path.split('.')), status);
        }
        state = (0, _lodash.sortBy)(state, 'priority');
        return state;
      }
    case '@@Plugin/remove':
      {
        const i = (0, _utils.getPluginIndexByPackageName)(state, value.packageName);
        if (i !== -1) {
          state = [...state];
          state.splice(i, 1);
          return state;
        }
        break;
      }
    default:
      return state;
  }
}