'use strict';

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _padRight;

function _load_padRight() {
  return _padRight = _interopRequireDefault(require('pad-right'));
}

var _chalk;

function _load_chalk() {
  return _chalk = _interopRequireDefault(require('chalk'));
}

var _command;

function _load_command() {
  return _command = _interopRequireDefault(require('../../command'));
}

var _utils;

function _load_utils() {
  return _utils = require('../../../utils');
}

var _constants;

function _load_constants() {
  return _constants = require('../../../constants');
}

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// import { config } from '../../../api/consumer';
var config = require('../../../api/consumer/lib/global-config');

var Config = function (_Command) {
  _inherits(Config, _Command);

  function Config() {
    var _ref;

    var _temp, _this, _ret;

    _classCallCheck(this, Config);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Config.__proto__ || Object.getPrototypeOf(Config)).call.apply(_ref, [this].concat(args))), _this), _this.name = 'config', _this.description = `global config management.\n  https://${(_constants || _load_constants()).BASE_DOCS_DOMAIN}/docs/conf-config.html`, _this.alias = '', _this.commands = [new ConfigSet(), new ConfigDel(), new ConfigGet(), new ConfigList()], _this.opts = [], _this.migration = false, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Config, [{
    key: 'action',
    value: function action() {
      return config.list();
    }
  }, {
    key: 'report',
    value: function report(conf) {
      return (0, (_utils || _load_utils()).objectToTupleArray)(conf).map(function (tuple) {
        tuple[0] = (0, (_padRight || _load_padRight()).default)(tuple[0], 30, ' ');
        return tuple.join('');
      }).join('\n');
    }
  }]);

  return Config;
}((_command || _load_command()).default);

exports.default = Config;

var ConfigSet = function (_Command2) {
  _inherits(ConfigSet, _Command2);

  function ConfigSet() {
    var _ref2;

    var _temp2, _this2, _ret2;

    _classCallCheck(this, ConfigSet);

    for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }

    return _ret2 = (_temp2 = (_this2 = _possibleConstructorReturn(this, (_ref2 = ConfigSet.__proto__ || Object.getPrototypeOf(ConfigSet)).call.apply(_ref2, [this].concat(args))), _this2), _this2.name = 'set <key> <val>', _this2.description = 'set a global configuration', _this2.alias = '', _this2.opts = [], _temp2), _possibleConstructorReturn(_this2, _ret2);
  }

  _createClass(ConfigSet, [{
    key: 'action',
    value: function action(_ref3) {
      var _ref4 = _slicedToArray(_ref3, 2),
          key = _ref4[0],
          value = _ref4[1];

      return config.set(key, value);
    }
  }, {
    key: 'report',
    value: function report(conf) {
      return (_chalk || _load_chalk()).default.green('added configuration successfully');
    }
  }]);

  return ConfigSet;
}((_command || _load_command()).default);

var ConfigGet = function (_Command3) {
  _inherits(ConfigGet, _Command3);

  function ConfigGet() {
    var _ref5;

    var _temp3, _this3, _ret3;

    _classCallCheck(this, ConfigGet);

    for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
      args[_key3] = arguments[_key3];
    }

    return _ret3 = (_temp3 = (_this3 = _possibleConstructorReturn(this, (_ref5 = ConfigGet.__proto__ || Object.getPrototypeOf(ConfigGet)).call.apply(_ref5, [this].concat(args))), _this3), _this3.name = 'get <key>', _this3.description = 'get a global configuration', _this3.alias = '', _this3.opts = [], _temp3), _possibleConstructorReturn(_this3, _ret3);
  }

  _createClass(ConfigGet, [{
    key: 'action',
    value: function action(_ref6) {
      var _ref7 = _slicedToArray(_ref6, 1),
          key = _ref7[0];

      return config.get(key);
    }
  }, {
    key: 'report',
    value: function report(value) {
      return value;
    }
  }]);

  return ConfigGet;
}((_command || _load_command()).default);

var ConfigList = function (_Command4) {
  _inherits(ConfigList, _Command4);

  function ConfigList() {
    var _ref8;

    var _temp4, _this4, _ret4;

    _classCallCheck(this, ConfigList);

    for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
      args[_key4] = arguments[_key4];
    }

    return _ret4 = (_temp4 = (_this4 = _possibleConstructorReturn(this, (_ref8 = ConfigList.__proto__ || Object.getPrototypeOf(ConfigList)).call.apply(_ref8, [this].concat(args))), _this4), _this4.name = 'list', _this4.description = 'list all configuration(s)', _this4.alias = '', _this4.opts = [], _temp4), _possibleConstructorReturn(_this4, _ret4);
  }

  _createClass(ConfigList, [{
    key: 'action',
    value: function action() {
      return config.list();
    }
  }, {
    key: 'report',
    value: function report(conf) {
      return (0, (_utils || _load_utils()).objectToTupleArray)(conf).map(function (tuple) {
        return tuple.join('     ');
      }).join('\n');
    }
  }]);

  return ConfigList;
}((_command || _load_command()).default);

var ConfigDel = function (_Command5) {
  _inherits(ConfigDel, _Command5);

  function ConfigDel() {
    var _ref9;

    var _temp5, _this5, _ret5;

    _classCallCheck(this, ConfigDel);

    for (var _len5 = arguments.length, args = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
      args[_key5] = arguments[_key5];
    }

    return _ret5 = (_temp5 = (_this5 = _possibleConstructorReturn(this, (_ref9 = ConfigDel.__proto__ || Object.getPrototypeOf(ConfigDel)).call.apply(_ref9, [this].concat(args))), _this5), _this5.name = 'del <key>', _this5.description = 'delete given key from global configuration', _this5.alias = '', _this5.opts = [], _temp5), _possibleConstructorReturn(_this5, _ret5);
  }

  _createClass(ConfigDel, [{
    key: 'action',
    value: function action(_ref10) {
      var _ref11 = _slicedToArray(_ref10, 1),
          key = _ref11[0];

      return config.del(key);
    }
  }, {
    key: 'report',
    value: function report(conf) {
      return (_chalk || _load_chalk()).default.green('deleted successfully');
    }
  }]);

  return ConfigDel;
}((_command || _load_command()).default);