'use strict';

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

var _regenerator;

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

var _bluebird;

function _load_bluebird() {
  return _bluebird = require('bluebird');
}

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 _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _fsExtra;

function _load_fsExtra() {
  return _fsExtra = _interopRequireDefault(require('fs-extra'));
}

var _constants;

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

var _utils;

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

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

function _extendableBuiltin(cls) {
  function ExtendableBuiltin() {
    var instance = Reflect.construct(cls, Array.from(arguments));
    Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
    return instance;
  }

  ExtendableBuiltin.prototype = Object.create(cls.prototype, {
    constructor: {
      value: cls,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });

  if (Object.setPrototypeOf) {
    Object.setPrototypeOf(ExtendableBuiltin, cls);
  } else {
    ExtendableBuiltin.__proto__ = cls;
  }

  return ExtendableBuiltin;
}

function getPath() {
  return (_path || _load_path()).default.join((_constants || _load_constants()).GLOBAL_CONFIG, (_constants || _load_constants()).GLOBAL_CONFIG_FILE);
}

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

  function Config() {
    _classCallCheck(this, Config);

    return _possibleConstructorReturn(this, (Config.__proto__ || Object.getPrototypeOf(Config)).apply(this, arguments));
  }

  _createClass(Config, [{
    key: 'toPlainObject',
    value: function toPlainObject() {
      return (0, (_utils || _load_utils()).mapToObject)(this);
    }
  }, {
    key: 'toJson',
    value: function toJson() {
      return JSON.stringify(this.toPlainObject());
    }
  }, {
    key: 'write',
    value: function write() {
      return (_fsExtra || _load_fsExtra()).default.outputFile(getPath(), this.toJson());
    }
  }, {
    key: 'writeSync',
    value: function writeSync() {
      return (_fsExtra || _load_fsExtra()).default.outputFileSync(getPath(), this.toJson());
    }
  }], [{
    key: 'loadSync',
    value: function loadSync() {
      try {
        var configPath = getPath();
        if (!(_fsExtra || _load_fsExtra()).default.existsSync(configPath)) {
          var config = new Config([]);
          config.writeSync();
          return config;
        }
        var contents = (_fsExtra || _load_fsExtra()).default.readFileSync(configPath);
        return new Config((0, (_utils || _load_utils()).objectToTupleArray)(JSON.parse(contents.toString())));
      } catch (err) {
        return err;
      }
    }
  }, {
    key: 'load',
    value: function () {
      var _ref = (0, (_bluebird || _load_bluebird()).coroutine)( /*#__PURE__*/(_regenerator || _load_regenerator()).default.mark(function _callee() {
        var configPath, exists, config, contents;
        return (_regenerator || _load_regenerator()).default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.prev = 0;
                configPath = getPath();
                _context.next = 4;
                return (_fsExtra || _load_fsExtra()).default.exists(configPath);

              case 4:
                exists = _context.sent;

                if (exists) {
                  _context.next = 10;
                  break;
                }

                config = new Config([]);
                _context.next = 9;
                return config.write();

              case 9:
                return _context.abrupt('return', config);

              case 10:
                _context.next = 12;
                return (_fsExtra || _load_fsExtra()).default.readFile(configPath);

              case 12:
                contents = _context.sent;
                return _context.abrupt('return', new Config((0, (_utils || _load_utils()).objectToTupleArray)(JSON.parse(contents.toString()))));

              case 16:
                _context.prev = 16;
                _context.t0 = _context['catch'](0);
                return _context.abrupt('return', _context.t0);

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

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

      return load;
    }()
  }]);

  return Config;
}(_extendableBuiltin(Map));

exports.default = Config;