import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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) { _defineProperty(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; }
import { getInvocationUrl, getSearchUrl, performPostRequest } from '../common/utils';

/**
 * Manual trigger rules API fetch. Fetches a list of manually triggered rules according to the context provided
 * in query.
 * @param site - the site ARI to fetch the rules against
 * @param query - the workspace/container/objects to fetch rules against
 */
export var searchManuallyTriggeredRules = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(site, query) {
    var url, response;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return getSearchUrl(site);
        case 1:
          url = _context.sent;
          _context.next = 2;
          return performPostRequest(url, {
            body: JSON.stringify(query)
          });
        case 2:
          response = _context.sent;
          return _context.abrupt("return", response.data.reduce(function (acc, _ref2) {
            var id = _ref2.id,
              name = _ref2.name,
              ruleScope = _ref2.ruleScope,
              userInputs = _ref2.userInputs;
            return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, id, {
              id: id,
              name: name,
              ruleScope: ruleScope,
              userInputPrompts: userInputs
            }));
          }, {}));
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function searchManuallyTriggeredRules(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

/**
 * Invoke a manually triggered rule via the API.
 * @param cloudId - the site the rule belongs to
 * @param ruleId - the identifier of the rule you want to invoke
 * @param objects - the list of objects you wish to create an automation event for.
 * @param userInputs - the optional user inputs if this manual trigger requires them
 */
export var invokeManuallyTriggeredRule = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(site, ruleId, objects, userInputs) {
    var url, bodyRaw;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return getInvocationUrl(site, ruleId);
        case 1:
          url = _context2.sent;
          bodyRaw = {
            objects: objects
          };
          if (userInputs) {
            bodyRaw.userInputs = userInputs;
          }
          return _context2.abrupt("return", performPostRequest(url, {
            body: JSON.stringify(bodyRaw)
          }));
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return function invokeManuallyTriggeredRule(_x3, _x4, _x5, _x6) {
    return _ref3.apply(this, arguments);
  };
}();