"use strict";

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

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

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

exports.SfdxRegistry = void 0;

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

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

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

require("core-js/modules/es6.function.name");

require("core-js/modules/es6.regexp.to-string");

require("core-js/modules/es6.date.to-string");

require("core-js/modules/es6.object.to-string");

var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));

require("regenerator-runtime/runtime");

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

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

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

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

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

var _child_process = require("child_process");

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

function isNotNullOrUndefined(v) {
  return v != null;
}
/**
 *
 */


var SfdxRegistry =
/*#__PURE__*/
function () {
  function SfdxRegistry(_ref) {
    var cliPath = _ref.cliPath;
    (0, _classCallCheck2["default"])(this, SfdxRegistry);
    (0, _defineProperty2["default"])(this, "_cliPath", void 0);
    (0, _defineProperty2["default"])(this, "_orgList", void 0);
    (0, _defineProperty2["default"])(this, "_orgInfoMap", {});
    (0, _defineProperty2["default"])(this, "_defaultOrgInfo", void 0);
    this._cliPath = cliPath;
  }

  (0, _createClass2["default"])(SfdxRegistry, [{
    key: "_createCommand",
    value: function _createCommand(command) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var args = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
      return "".concat(this._cliPath ? this._cliPath + '/' : '', "sfdx ").concat(command, " ").concat((0, _keys["default"])(options).map(function (option) {
        return "".concat(option.length > 1 ? '--' : '-').concat(option).concat(options[option] != null ? ' ' + options[option] : '');
      }).join(' '), " --json ").concat(args.join(' '));
    }
  }, {
    key: "_execCommand",
    value: function _execCommand(command) {
      var options,
          args,
          cmd,
          buf,
          ret,
          err,
          _args = arguments;
      return _regenerator["default"].async(function _execCommand$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
              args = _args.length > 2 && _args[2] !== undefined ? _args[2] : [];
              cmd = this._createCommand(command, options, args);
              _context.next = 5;
              return _regenerator["default"].awrap(new _promise["default"](function (resolve, reject) {
                (0, _child_process.exec)(cmd, function (err, ret) {
                  if (err) {
                    reject(err);
                  } else {
                    resolve(ret);
                  }
                });
              }));

            case 5:
              buf = _context.sent;
              ret = JSON.parse(buf.toString());

              if (!(ret.status === 0 && ret.result)) {
                _context.next = 11;
                break;
              }

              return _context.abrupt("return", ret.result);

            case 11:
              err = new Error(ret.message);
              err.name = ret.name;
              throw err;

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "_getOrgList",
    value: function _getOrgList() {
      return _regenerator["default"].async(function _getOrgList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              if (!this._orgList) {
                this._orgList = this._execCommand('force:org:list');
              }

              return _context2.abrupt("return", this._orgList);

            case 2:
            case "end":
              return _context2.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getConnectionNames",
    value: function getConnectionNames() {
      var _ref2, nonScratchOrgs, scratchOrgs;

      return _regenerator["default"].async(function getConnectionNames$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return _regenerator["default"].awrap(this._getOrgList());

            case 2:
              _ref2 = _context3.sent;
              nonScratchOrgs = _ref2.nonScratchOrgs;
              scratchOrgs = _ref2.scratchOrgs;
              return _context3.abrupt("return", [].concat((0, _toConsumableArray2["default"])(nonScratchOrgs.map(function (o) {
                return o.alias;
              }).filter(isNotNullOrUndefined)), (0, _toConsumableArray2["default"])(scratchOrgs.map(function (o) {
                return o.alias;
              }).filter(isNotNullOrUndefined)), (0, _toConsumableArray2["default"])(nonScratchOrgs.map(function (o) {
                return o.username;
              })), (0, _toConsumableArray2["default"])(scratchOrgs.map(function (o) {
                return o.username;
              }))));

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

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

            case 4:
            case "end":
              return _context4.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "_getOrgInfo",
    value: function _getOrgInfo(username) {
      var options, pOrgInfo, orgInfo;
      return _regenerator["default"].async(function _getOrgInfo$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              options = username ? {
                u: username
              } : {};

              if (!username || !this._orgInfoMap[username]) {
                pOrgInfo = this._execCommand('force:org:display', options);

                this._memoOrgInfo(pOrgInfo, username);
              }

              orgInfo = username ? this._orgInfoMap[username] : this._defaultOrgInfo;

              if (orgInfo) {
                _context5.next = 5;
                break;
              }

              throw new Error('no orginfo found');

            case 5:
              return _context5.abrupt("return", orgInfo);

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "_memoOrgInfo",
    value: function _memoOrgInfo(pOrgInfo, username) {
      var _this = this;

      var pOrgInfo_ = pOrgInfo.then(function (orgInfo) {
        _this._orgInfoMap[orgInfo.username] = pOrgInfo_;

        if (orgInfo.alias) {
          _this._orgInfoMap[orgInfo.alias] = pOrgInfo_;
        }

        return orgInfo;
      });

      if (username) {
        this._orgInfoMap[username] = pOrgInfo_;
      } else {
        this._defaultOrgInfo = pOrgInfo_;
      }
    }
  }, {
    key: "getConnectionConfig",
    value: function getConnectionConfig(name) {
      var orgInfo, accessToken, instanceUrl, loginUrl;
      return _regenerator["default"].async(function getConnectionConfig$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _context6.next = 2;
              return _regenerator["default"].awrap(this._getOrgInfo(name));

            case 2:
              orgInfo = _context6.sent;

              if (orgInfo) {
                _context6.next = 5;
                break;
              }

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

            case 5:
              accessToken = orgInfo.accessToken, instanceUrl = orgInfo.instanceUrl, loginUrl = orgInfo.loginUrl;
              return _context6.abrupt("return", {
                accessToken: accessToken,
                instanceUrl: instanceUrl,
                loginUrl: loginUrl
              });

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "saveConnectionConfig",
    value: function saveConnectionConfig(_name, _connConfig) {
      return _regenerator["default"].async(function saveConnectionConfig$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
            case "end":
              return _context7.stop();
          }
        }
      });
    }
  }, {
    key: "setDefaultConnection",
    value: function setDefaultConnection(_name) {
      return _regenerator["default"].async(function setDefaultConnection$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
            case "end":
              return _context8.stop();
          }
        }
      });
    }
  }, {
    key: "removeConnectionConfig",
    value: function removeConnectionConfig(name) {
      return _regenerator["default"].async(function removeConnectionConfig$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              _context9.next = 2;
              return _regenerator["default"].awrap(this._execCommand('force:org:delete', {
                u: name
              }));

            case 2:
            case "end":
              return _context9.stop();
          }
        }
      }, null, this);
    }
  }, {
    key: "getClientConfig",
    value: function getClientConfig(_name) {
      return _regenerator["default"].async(function getClientConfig$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              return _context10.abrupt("return", null);

            case 1:
            case "end":
              return _context10.stop();
          }
        }
      });
    }
  }, {
    key: "getClientNames",
    value: function getClientNames() {
      return _regenerator["default"].async(function getClientNames$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              return _context11.abrupt("return", []);

            case 1:
            case "end":
              return _context11.stop();
          }
        }
      });
    }
  }, {
    key: "registerClientConfig",
    value: function registerClientConfig(_name, _clientConfig) {
      return _regenerator["default"].async(function registerClientConfig$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
            case "end":
              return _context12.stop();
          }
        }
      });
    }
  }]);
  return SfdxRegistry;
}();

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