"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.performPostRequest = exports.performGetRequest = exports.getSearchUrl = exports.getInvocationUrl = exports.extractCloudIdAndProductFromSite = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/**
 * This function will extract the cloud ID and product from a product level Site ARI.
 * ARIs can be found at the following link: {@link https://developer.atlassian.com/platform/atlassian-resource-identifier/resource-owners/registry/#ari-registry}
 * @param site The Site ARI where the manual trigger rule is executed. Should be a product level Site ARI.
 */
var extractCloudIdAndProductFromSite = exports.extractCloudIdAndProductFromSite = function extractCloudIdAndProductFromSite(site) {
  var cloudId;
  var resourceOwner;
  var resourceType;
  var ariRegex = new RegExp('^ari:cloud:([a-zA-Z.\\-]+)::([a-zA-Z.\\-]+)/([a-zA-Z0-9\\-]+)');
  var parsedAri = site.match(ariRegex);
  if (parsedAri) {
    resourceOwner = parsedAri[1];
    resourceType = parsedAri[2];
    cloudId = parsedAri[3];
  }
  // Keep backwards compatibility for platform site ARIs
  if (resourceOwner === 'platform') {
    resourceOwner = 'jira';
  }
  if (resourceType !== 'site' || cloudId === undefined || resourceOwner === undefined) {
    throw new Error('Not a site ARI: ' + site);
  }
  return {
    product: resourceOwner,
    cloudId: cloudId
  };
};

/***
 * TODO remove when unified public API is available.
 *
 * Currently we're in a transitory phase where we have one API for invocation for Jira, and one for JSM. They concern themselves
 * with issues and alerts respectively.
 *
 * We are working on a unified public API that only cares about product-agnostic filters like object, workspace, container as
 * part of the collaboration model. For the meantime, you'll see two APIs instead. In this package, we're only using the
 * alerts API. It is this API that will be expanded to retroactively support Jira, and thus the updated collaboration
 * model language is used here, even though for the meantime we only concern ourselves with alerts.
 *
 */
var getSearchUrl = exports.getSearchUrl = /*#__PURE__*/function () {
  var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(site) {
    var _extractCloudIdAndPro, product, cloudId;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _extractCloudIdAndPro = extractCloudIdAndProductFromSite(site), product = _extractCloudIdAndPro.product, cloudId = _extractCloudIdAndPro.cloudId; // TODO in the unified API, GET requests as according to Atlassian API standards will destructure the whole query into
          // query parameters as part of the URL. For now, this is considered a POST request instead and thus the query is used
          // later on as the request body.
          return _context.abrupt("return", "/gateway/api/automation/public/".concat(product, "/").concat(cloudId, "/rest/v1/rule/manual/search"));
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function getSearchUrl(_x) {
    return _ref.apply(this, arguments);
  };
}();
var getInvocationUrl = exports.getInvocationUrl = /*#__PURE__*/function () {
  var _ref2 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(site, ruleId) {
    var _extractCloudIdAndPro2, product, cloudId;
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _extractCloudIdAndPro2 = extractCloudIdAndProductFromSite(site), product = _extractCloudIdAndPro2.product, cloudId = _extractCloudIdAndPro2.cloudId;
          return _context2.abrupt("return", "/gateway/api/automation/public/".concat(product, "/").concat(cloudId, "/rest/v1/rule/manual/").concat(ruleId, "/invocation"));
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return function getInvocationUrl(_x2, _x3) {
    return _ref2.apply(this, arguments);
  };
}();
var DEFAULT_HEADER = {
  'Content-Type': 'application/json'
};
var _performRequest = /*#__PURE__*/function () {
  var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(url, method, options) {
    var response;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 2;
          return fetch(url, _objectSpread(_objectSpread({
            headers: DEFAULT_HEADER
          }, options), {}, {
            method: method
          }));
        case 2:
          response = _context3.sent;
          return _context3.abrupt("return", response.json());
        case 4:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
  return function _performRequest(_x4, _x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}();
var performGetRequest = exports.performGetRequest = /*#__PURE__*/function () {
  var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(url, options) {
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          return _context4.abrupt("return", _performRequest(url, 'GET', options));
        case 1:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  }));
  return function performGetRequest(_x7, _x8) {
    return _ref4.apply(this, arguments);
  };
}();
var performPostRequest = exports.performPostRequest = /*#__PURE__*/function () {
  var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(url, options) {
    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          return _context5.abrupt("return", _performRequest(url, 'POST', options));
        case 1:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  }));
  return function performPostRequest(_x9, _x0) {
    return _ref5.apply(this, arguments);
  };
}();