'use strict';

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

var _regenerator;

function _load_regenerator() {
  return _regenerator = _interopRequireDefault(require('babel-runtime/regenerator'));
}

var get = exports.get = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee(key) {
    var config, val, gitVal;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (_globalConfig || _load_globalConfig()).GlobalConfig.load();

          case 2:
            config = _context.sent;
            val = config.get(key);

            if ((_ramda || _load_ramda()).default.isNil(val)) {
              _context.next = 6;
              break;
            }

            return _context.abrupt('return', val);

          case 6:
            _context.prev = 6;
            _context.next = 9;
            return (_gitconfig || _load_gitconfig()).default.get(key);

          case 9:
            gitVal = _context.sent;
            return _context.abrupt('return', gitVal);

          case 13:
            _context.prev = 13;
            _context.t0 = _context['catch'](6);
            return _context.abrupt('return', undefined);

          case 16:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[6, 13]]);
  }));

  return function get(_x) {
    return _ref.apply(this, arguments);
  };
}();

exports.set = set;
exports.setSync = setSync;
exports.del = del;
exports.delSync = delSync;
exports.getSync = getSync;
exports.list = list;
exports.listSync = listSync;

var _gitconfig;

function _load_gitconfig() {
  return _gitconfig = _interopRequireDefault(require('@teambit/gitconfig'));
}

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _globalConfig;

function _load_globalConfig() {
  return _globalConfig = require('../../../global-config');
}

var _config;

function _load_config() {
  return _config = _interopRequireDefault(require('../../../global-config/config'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../../../error/general-error'));
}

var _constants;

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

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function set(key, val) {
  if (!key || !val) {
    throw new (_generalError || _load_generalError()).default(`missing a configuration key and value. https://${(_constants || _load_constants()).BASE_DOCS_DOMAIN}/docs/conf-config.html`);
  }
  return (_globalConfig || _load_globalConfig()).GlobalConfig.load().then(function (config) {
    config.set(key, val);
    return config.write().then(function () {
      return config;
    });
  });
}

function setSync(key, val) {
  var config = (_globalConfig || _load_globalConfig()).GlobalConfig.loadSync();
  config.set(key, val);
  config.writeSync();
  return config;
}

function del(key) {
  return (_globalConfig || _load_globalConfig()).GlobalConfig.load().then(function (config) {
    config.delete(key);
    return config.write().then(function () {
      return config;
    });
  });
}

function delSync(key) {
  var config = (_globalConfig || _load_globalConfig()).GlobalConfig.loadSync();
  config.delete(key);
  config.writeSync();
  return config;
}

function getSync(key) {
  var config = (_globalConfig || _load_globalConfig()).GlobalConfig.loadSync();
  var val = config.get(key);
  if (!(_ramda || _load_ramda()).default.isNil(val)) return val;
  try {
    var gitVal = (_gitconfig || _load_gitconfig()).default.get.sync(key);
    return gitVal;
    // Ignore error from git config get
  } catch (err) {
    return undefined;
  }
}

function list() {
  return (_globalConfig || _load_globalConfig()).GlobalConfig.load().then(function (config) {
    return config.toPlainObject();
  });
}

function listSync() {
  var config = (_globalConfig || _load_globalConfig()).GlobalConfig.loadSync();
  return config.toPlainObject();
}