"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

var _Object$defineProperty2 = require("@babel/runtime-corejs2/core-js/object/define-property");

_Object$defineProperty2(exports, "__esModule", {
  value: true
});

exports.BaseRegistry = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/define-property"));

var _defineProperties = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/define-properties"));

var _getOwnPropertyDescriptors = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptors"));

require("core-js/modules/es6.array.for-each");

var _getOwnPropertyDescriptor = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-descriptor"));

require("core-js/modules/es6.array.filter");

var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/get-own-property-symbols"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/objectWithoutProperties"));

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

var _keys = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/keys"));

require("regenerator-runtime/runtime");

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));

var _defineProperty3 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/defineProperty"));

var _core = _interopRequireDefault(require("../core"));

function ownKeys(object, enumerableOnly) { var keys = (0, _keys["default"])(object); if (_getOwnPropertySymbols["default"]) { var symbols = (0, _getOwnPropertySymbols["default"])(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return (0, _getOwnPropertyDescriptor["default"])(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty3["default"])(target, key, source[key]); }); } else if (_getOwnPropertyDescriptors["default"]) { (0, _defineProperties["default"])(target, (0, _getOwnPropertyDescriptors["default"])(source)); } else { ownKeys(Object(source)).forEach(function (key) { (0, _defineProperty2["default"])(target, key, (0, _getOwnPropertyDescriptor["default"])(source, key)); }); } } return target; }

/**
 *
 */
var BaseRegistry =
/*#__PURE__*/
function () {
  function BaseRegistry() {
    (0, _classCallCheck2["default"])(this, BaseRegistry);
    (0, _defineProperty3["default"])(this, "_registryConfig", {});
  }

  (0, _createClass2["default"])(BaseRegistry, [{
    key: "_saveConfig",
    value: function _saveConfig() {
      throw new Error('_saveConfig must be implemented in subclass');
    }
  }, {
    key: "_getClients",
    value: function _getClients() {
      return this._registryConfig.clients || (this._registryConfig.clients = {});
    }
  }, {
    key: "_getConnections",
    value: function _getConnections() {
      return this._registryConfig.connections || (this._registryConfig.connections = {});
    }
  }, {
    key: "getConnectionNames",
    value: function getConnectionNames() {
      return _regenerator["default"].async(function getConnectionNames$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              return _context.abrupt("return", (0, _keys["default"])(this._getConnections()));

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getConnection",
    value: function getConnection(name) {
      var config;
      return _regenerator["default"].async(function getConnection$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return _regenerator["default"].awrap(this.getConnectionConfig(name));

            case 2:
              config = _context2.sent;
              return _context2.abrupt("return", config ? new _core["default"].Connection(config) : null);

            case 4:
            case "end":
              return _context2.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getConnectionConfig",
    value: function getConnectionConfig(name) {
      var connections, connConfig, client, connConfig_;
      return _regenerator["default"].async(function getConnectionConfig$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              if (!name) {
                name = this._registryConfig['default'];
              }

              connections = this._getConnections();
              connConfig = name ? connections[name] : undefined;

              if (connConfig) {
                _context3.next = 5;
                break;
              }

              return _context3.abrupt("return", null);

            case 5:
              client = connConfig.client, connConfig_ = (0, _objectWithoutProperties2["default"])(connConfig, ["client"]);

              if (!client) {
                _context3.next = 18;
                break;
              }

              _context3.t0 = _objectSpread;
              _context3.t1 = {};
              _context3.t2 = connConfig_;
              _context3.t3 = _objectSpread;
              _context3.t4 = {};
              _context3.next = 14;
              return _regenerator["default"].awrap(this.getClientConfig(client));

            case 14:
              _context3.t5 = _context3.sent;
              _context3.t6 = (0, _context3.t3)(_context3.t4, _context3.t5);
              _context3.t7 = {
                oauth2: _context3.t6
              };
              return _context3.abrupt("return", (0, _context3.t0)(_context3.t1, _context3.t2, _context3.t7));

            case 18:
              return _context3.abrupt("return", connConfig_);

            case 19:
            case "end":
              return _context3.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "saveConnectionConfig",
    value: function saveConnectionConfig(name, connConfig) {
      var connections, oauth2, connConfig_, persistConnConfig, clientName;
      return _regenerator["default"].async(function saveConnectionConfig$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              connections = this._getConnections();
              oauth2 = connConfig.oauth2, connConfig_ = (0, _objectWithoutProperties2["default"])(connConfig, ["oauth2"]);
              persistConnConfig = connConfig_;

              if (oauth2) {
                clientName = this._findClientName(oauth2);

                if (clientName) {
                  persistConnConfig = _objectSpread({}, persistConnConfig, {
                    client: clientName
                  });
                }

                delete connConfig.oauth2;
              }

              connections[name] = persistConnConfig;

              this._saveConfig();

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "_findClientName",
    value: function _findClientName(_ref) {
      var clientId = _ref.clientId,
          loginUrl = _ref.loginUrl;

      var clients = this._getClients();

      for (var _i = 0, _Object$keys2 = (0, _keys["default"])(clients); _i < _Object$keys2.length; _i++) {
        var name = _Object$keys2[_i];
        var client = clients[name];

        if (client.clientId === clientId && (client.loginUrl || 'https://login.salesforce.com') === loginUrl) {
          return name;
        }
      }

      return null;
    }
  }, {
    key: "setDefaultConnection",
    value: function setDefaultConnection(name) {
      return _regenerator["default"].async(function setDefaultConnection$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              this._registryConfig['default'] = name;

              this._saveConfig();

            case 2:
            case "end":
              return _context5.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "removeConnectionConfig",
    value: function removeConnectionConfig(name) {
      var connections;
      return _regenerator["default"].async(function removeConnectionConfig$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              connections = this._getConnections();
              delete connections[name];

              this._saveConfig();

            case 3:
            case "end":
              return _context6.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getClientConfig",
    value: function getClientConfig(name) {
      var clients, clientConfig;
      return _regenerator["default"].async(function getClientConfig$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              clients = this._getClients();
              clientConfig = clients[name];
              return _context7.abrupt("return", clientConfig && _objectSpread({}, clientConfig));

            case 3:
            case "end":
              return _context7.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getClientNames",
    value: function getClientNames() {
      return _regenerator["default"].async(function getClientNames$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              return _context8.abrupt("return", (0, _keys["default"])(this._getClients()));

            case 1:
            case "end":
              return _context8.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "registerClientConfig",
    value: function registerClientConfig(name, clientConfig) {
      var clients;
      return _regenerator["default"].async(function registerClientConfig$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              clients = this._getClients();
              clients[name] = clientConfig;

              this._saveConfig();

            case 3:
            case "end":
              return _context9.stop();
          }
        }
      }, null, this);
    }
  }]);
  return BaseRegistry;
}();

exports.BaseRegistry = BaseRegistry;
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 