"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;
}

function _stringifyObject() {
  const data = _interopRequireDefault(require("stringify-object"));

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

  return data;
}

function _path() {
  const data = require("path");

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

  return data;
}

var _setConfig = _interopRequireDefault(require("./setConfig"));

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

function _default(api) {
  const service = api.service,
        cwd = api.cwd;

  function list() {
    console.log((0, _stringifyObject().default)(api.config, {
      indent: '  '
    }));
    return api.config;
  }

  function get(key) {
    if (api.config[key]) {
      console.log((0, _stringifyObject().default)(api.config[key], {
        indent: '  '
      }));
      return api.config[key];
    }
  }

  function set(key, value, plugin) {
    return (0, _setConfig.default)({
      key,
      value,
      file: service.userConfig.file || (0, _path().join)(cwd, '.umirc.js'),
      plugin
    });
  }

  function rm(key) {
    console.log('delete', key);
  }

  function configHandler(args) {
    switch (args._[0]) {
      case 'list':
        return list();

      case 'get':
        (0, _assert().default)(args._[1], `key must supplied, e.g. umi config get routes`);
        return get(args._[1]);

      case 'set':
        (0, _assert().default)(args._[1], `key, e.g. umi config set mountElementId root`);
        return set(args._[1], args._[2], args.plugin);

      case 'delete':
        (0, _assert().default)(args._[1], `key must supplied, e.g. umi config delete externals`);
        return rm(args._[1]);

      default:
        throw new Error(`unsupported action ${args._[0]} for umi config, try list, get, set and delete`);
    }
  }

  api.registerCommand('config', {
    description: 'update config via cli',
    options: {}
  }, configHandler);
}