'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 _callee2(key) {
    var _this = this;

    var getConfigObject, config, val, gitVal;
    return (_regenerator || _load_regenerator()).default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            getConfigObject = function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
                var configFromCache, config;
                return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        configFromCache = cache().get();

                        if (!configFromCache) {
                          _context.next = 3;
                          break;
                        }

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

                      case 3:
                        _context.next = 5;
                        return (_config || _load_config()).default.load();

                      case 5:
                        config = _context.sent;

                        cache().set(config);
                        return _context.abrupt('return', config);

                      case 8:
                      case 'end':
                        return _context.stop();
                    }
                  }
                }, _callee, _this);
              }));

              return function getConfigObject() {
                return _ref2.apply(this, arguments);
              };
            }();

            _context2.next = 3;
            return getConfigObject();

          case 3:
            config = _context2.sent;
            val = config.get(key);

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

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

          case 7:
            _context2.prev = 7;
            _context2.next = 10;
            return (_gitconfig || _load_gitconfig()).default.get(key);

          case 10:
            gitVal = _context2.sent;
            return _context2.abrupt('return', gitVal);

          case 14:
            _context2.prev = 14;
            _context2.t0 = _context2['catch'](7);
            return _context2.abrupt('return', undefined);

          case 17:
          case 'end':
            return _context2.stop();
        }
      }
    }, _callee2, this, [[7, 14]]);
  }));

  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 _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 (_config || _load_config()).default.load().then(function (config) {
    config.set(key, val);
    invalidateCache();
    return config.write().then(function () {
      return config;
    });
  });
}

function setSync(key, val) {
  var config = (_config || _load_config()).default.loadSync();
  config.set(key, val);
  invalidateCache();
  config.writeSync();
  return config;
}

function del(key) {
  return (_config || _load_config()).default.load().then(function (config) {
    config.delete(key);
    invalidateCache();
    return config.write().then(function () {
      return config;
    });
  });
}

function delSync(key) {
  var config = (_config || _load_config()).default.loadSync();
  config.delete(key);
  config.writeSync();
  invalidateCache();
  return config;
}

function getSync(key) {
  var getConfigObject = function getConfigObject() {
    var configFromCache = cache().get();
    if (configFromCache) return configFromCache;
    var config = (_config || _load_config()).default.loadSync();
    cache().set(config);
    return config;
  };
  var config = getConfigObject();
  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 (_config || _load_config()).default.load().then(function (config) {
    return config.toPlainObject();
  });
}

function listSync() {
  var config = (_config || _load_config()).default.loadSync();
  return config.toPlainObject();
}

function cache() {
  return {
    get: function get() {
      return cache.config;
    },
    set: function set(config) {
      cache.config = config;
    }
  };
}

function invalidateCache() {
  cache().set(null);
}