Liferay.Loader.define("segments-experiment-web@2.0.19/js/util/APIService.es", ['module', 'exports', 'require', 'frontend-js-web', 'frontend-js-react-web$prop-types'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports._getFormDataRequest = _getFormDataRequest;
    exports["default"] = void 0;

    var _frontendJsWeb = require("frontend-js-web");

    var _propTypes = _interopRequireDefault(require("frontend-js-react-web$prop-types"));

    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { "default": obj };
    }

    function _slicedToArray(arr, i) {
      return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
    }

    function _nonIterableRest() {
      throw new TypeError("Invalid attempt to destructure non-iterable instance");
    }

    function _iterableToArrayLimit(arr, i) {
      if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) {
        return;
      }var _arr = [];var _n = true;var _d = false;var _e = undefined;try {
        for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
          _arr.push(_s.value);if (i && _arr.length === i) break;
        }
      } catch (err) {
        _d = true;_e = err;
      } finally {
        try {
          if (!_n && _i["return"] != null) _i["return"]();
        } finally {
          if (_d) throw _e;
        }
      }return _arr;
    }

    function _arrayWithHoles(arr) {
      if (Array.isArray(arr)) return arr;
    }

    function APIService(_ref) {
      var contentPageEditorNamespace = _ref.contentPageEditorNamespace,
          endpoints = _ref.endpoints,
          namespace = _ref.namespace;
      var calculateSegmentsExperimentEstimatedDurationURL = endpoints.calculateSegmentsExperimentEstimatedDurationURL,
          createSegmentsExperimentURL = endpoints.createSegmentsExperimentURL,
          createSegmentsVariantURL = endpoints.createSegmentsVariantURL,
          deleteSegmentsExperimentURL = endpoints.deleteSegmentsExperimentURL,
          deleteSegmentsVariantURL = endpoints.deleteSegmentsVariantURL,
          editSegmentsExperimentStatusURL = endpoints.editSegmentsExperimentStatusURL,
          editSegmentsExperimentURL = endpoints.editSegmentsExperimentURL,
          editSegmentsVariantURL = endpoints.editSegmentsVariantURL,
          runSegmentsExperimentURL = endpoints.runSegmentsExperimentURL;

      return {
        createExperiment: function createExperiment(body) {
          return _fetchWithError(createSegmentsExperimentURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        createVariant: function createVariant(body) {
          return _fetchWithError(createSegmentsVariantURL, {
            body: _getFormDataRequest(body, contentPageEditorNamespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        deleteExperiment: function deleteExperiment(body) {
          return _fetchWithError(deleteSegmentsExperimentURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        deleteVariant: function deleteVariant(body) {
          return _fetchWithError(deleteSegmentsVariantURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        editExperiment: function editExperiment(body) {
          return _fetchWithError(editSegmentsExperimentURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        editExperimentStatus: function editExperimentStatus(body) {
          return _fetchWithError(editSegmentsExperimentStatusURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        editVariant: function editVariant(body) {
          return _fetchWithError(editSegmentsVariantURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        getEstimatedTime: function getEstimatedTime(body) {
          return _fetchWithError(calculateSegmentsExperimentEstimatedDurationURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        publishExperience: function publishExperience(body) {
          // TODO somehow type this
          return _fetchWithError(editSegmentsExperimentStatusURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        },
        runExperiment: function runExperiment(body) {
          return _fetchWithError(runSegmentsExperimentURL, {
            body: _getFormDataRequest(body, namespace),
            credentials: 'include',
            method: 'POST'
          });
        }
      };
    }

    APIService.propTypes = {
      contentPageEditorNamespace: _propTypes["default"].string.isRequired,
      endpoints: _propTypes["default"].shape({
        calculateSegmentsExperimentEstimatedDurationURL: _propTypes["default"].string.isRequired,
        createSegmentsExperimentURL: _propTypes["default"].string.isRequired,
        createSegmentsVariantURL: _propTypes["default"].string.isRequired,
        deleteSegmentsExperimentURL: _propTypes["default"].string.isRequired,
        deleteSegmentsVariantURL: _propTypes["default"].string.isRequired,
        editSegmentsExperimentStatusURL: _propTypes["default"].string.isRequired,
        editSegmentsExperimentURL: _propTypes["default"].string.isRequired,
        editSegmentsVariantURL: _propTypes["default"].string.isRequired,
        runSegmentsExperimentURL: _propTypes["default"].string.isRequired
      }),
      namespace: _propTypes["default"].string.isRequired
    };
    var _default = APIService;
    /**
     *
     *
     * @export
     * @param {Object} body
     * @param {string} prefix
     * @param {FormData} [formData=new FormData()]
     * @returns {FormData}
     */

    exports["default"] = _default;

    function _getFormDataRequest(body, prefix) {
      var formData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new FormData();
      Object.entries(body).forEach(function (_ref2) {
        var _ref3 = _slicedToArray(_ref2, 2),
            key = _ref3[0],
            value = _ref3[1];

        formData.append("".concat(prefix).concat(key), value);
      });
      return formData;
    }
    /**
     * Wrapper to `fetch` function throwing an error when `error` is present in the response
     */

    function _fetchWithError(url) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      return (0, _frontendJsWeb.fetch)(url, options).then(function (response) {
        return response.json();
      }).then(function (objectResponse) {
        if (objectResponse.error) {
          throw objectResponse.error;
        }

        return objectResponse;
      });
    }
    //# sourceMappingURL=APIService.es.js.map
  }
});
//# sourceMappingURL=APIService.es.js.map