Liferay.Loader.define("commerce-bom-admin-web@4.0.30/js/actions/area.es", ['module', 'exports', 'require', 'frontend-js-web'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.getAcceptLanguageHeaderParam = getAcceptLanguageHeaderParam;
    exports.default = exports.actions = exports.fetchHeaders = exports.actionDefinition = void 0;

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

    function _objectWithoutProperties(source, excluded) {
      if (source == null) return {};var target = _objectWithoutPropertiesLoose(source, excluded);var key, i;if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);for (i = 0; i < sourceSymbolKeys.length; i++) {
          key = sourceSymbolKeys[i];if (excluded.indexOf(key) >= 0) continue;if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;target[key] = source[key];
        }
      }return target;
    }

    function _objectWithoutPropertiesLoose(source, excluded) {
      if (source == null) return {};var target = {};var sourceKeys = Object.keys(source);var key, i;for (i = 0; i < sourceKeys.length; i++) {
        key = sourceKeys[i];if (excluded.indexOf(key) >= 0) continue;target[key] = source[key];
      }return target;
    }

    var actionDefinition = {
      CREATE_SPOT: 'createSpot',
      DELETE_SPOT_FULFILLED: 'deleteSpotFulfilled',
      DELETE_SPOT_PENDING: 'deleteSpotPending',
      DELETE_SPOT_REJECTED: 'deleteSpotRejected',
      GET_AREA_FULFILLED: 'getAreaFulfilled',
      GET_AREA_PENDING: 'getAreaPending',
      GET_AREA_REJECTED: 'getAreaRejected',
      GET_PRODUCTS_FULFILLED: 'getProductsFulfilled',
      GET_PRODUCTS_PENDING: 'getProductsPending',
      GET_PRODUCTS_REJECTED: 'getProductsRejected',
      HIGHLIGHT_DETAIL: 'highlightDetail',
      RESET_FORM_DATA: 'resetFormData',
      RESET_PRODUCTS: 'resetProducts',
      SELECT_DETAIL: 'selectDetail',
      SELECT_SPOT: 'selectSpot',
      SUBMIT_NEW_SPOT_FULFILLED: 'submitNewSpotFulfilled',
      SUBMIT_NEW_SPOT_PENDING: 'submitNewSpotPending',
      SUBMIT_NEW_SPOT_REJECTED: 'submitNewSpotRejected',
      SUBMIT_SPOT_CHANGES_FULFILLED: 'submitSpotChangesFulfilled',
      SUBMIT_SPOT_CHANGES_PENDING: 'submitSpotChangesPending',
      SUBMIT_SPOT_CHANGES_REJECTED: 'submitSpotChangesRejected',
      UNSELECT_SPOT: 'unselectSpot',
      UPDATE_FORM_VALUE: 'updateFormValue'
    };
    exports.actionDefinition = actionDefinition;

    function getAcceptLanguageHeaderParam() {
      var browserLang = navigator.language || navigator.userLanguage;
      var themeLang = Liferay.ThemeDisplay.getLanguageId().replace('_', '-');

      if (browserLang === themeLang) {
        return browserLang;
      }

      return "".concat(browserLang, ", ").concat(themeLang, ";q=0.8");
    }

    var fetchHeaders = new Headers({
      Accept: 'application/json',
      'Accept-Language': getAcceptLanguageHeaderParam(),
      'Content-Type': 'application/json'
    });
    exports.fetchHeaders = fetchHeaders;

    var highlightDetail = function highlightDetail(dispatch) {
      return function (number) {
        var showFirstResume = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
        return dispatch({
          payload: {
            number: number,
            showFirstResume: showFirstResume
          },
          type: actionDefinition.HIGHLIGHT_DETAIL
        });
      };
    };

    var select = function select(dispatch) {
      return function (id) {
        return dispatch({
          payload: id,
          type: actionDefinition.SELECT_DETAIL
        });
      };
    };

    var getArea = function getArea(dispatch) {
      return function (endpoint, id) {
        dispatch({
          type: actionDefinition.GET_AREA_PENDING
        });
        return (0, _frontendJsWeb.fetch)(endpoint + '/' + id).then(function (response) {
          return response.json();
        }).then(function (data) {
          return dispatch({
            payload: data,
            type: actionDefinition.GET_AREA_FULFILLED
          });
        }).catch(function (err) {
          return dispatch({
            payload: err,
            type: actionDefinition.GET_AREA_REJECTED
          });
        });
      };
    };

    var getProducts = function getProducts(dispatch) {
      return function (endpoint, query) {
        dispatch({
          type: actionDefinition.GET_PRODUCTS_PENDING
        });
        return (0, _frontendJsWeb.fetch)(endpoint + (query ? "?q=".concat(query) : '')).then(function (response) {
          return response.json();
        }).then(function (data) {
          return dispatch({
            payload: data && data.items,
            type: actionDefinition.GET_PRODUCTS_FULFILLED
          });
        }).catch(function (err) {
          return dispatch({
            payload: err,
            type: actionDefinition.GET_PRODUCTS_REJECTED
          });
        });
      };
    };

    var resetProducts = function resetProducts(dispatch) {
      return function () {
        return dispatch({
          type: actionDefinition.RESET_PRODUCTS
        });
      };
    };

    var createSpot = function createSpot(dispatch) {
      return function (position) {
        return dispatch({
          payload: position,
          type: actionDefinition.CREATE_SPOT
        });
      };
    };

    var selectSpot = function selectSpot(dispatch) {
      return function (spotId) {
        return dispatch({
          payload: spotId,
          type: actionDefinition.SELECT_SPOT
        });
      };
    };

    var unselectSpot = function unselectSpot(dispatch) {
      return function () {
        return dispatch({
          type: actionDefinition.UNSELECT_SPOT
        });
      };
    };

    var updateFormValue = function updateFormValue(dispatch) {
      return function (key, value) {
        return dispatch({
          payload: {
            key: key,
            value: value
          },
          type: actionDefinition.UPDATE_FORM_VALUE
        });
      };
    };

    var submitNewSpot = function submitNewSpot(dispatch) {
      return function (endpoint, areaId, formData) {
        var number = formData.number,
            position = formData.position,
            productId = formData.productId;
        dispatch({
          type: actionDefinition.SUBMIT_NEW_SPOT_PENDING
        });
        return (0, _frontendJsWeb.fetch)(endpoint + '/' + areaId + '/spot', {
          body: JSON.stringify({
            number: number,
            position: position,
            productId: productId
          }),
          headers: fetchHeaders,
          method: 'POST'
        }).then(function () {
          dispatch({
            type: actionDefinition.SUBMIT_NEW_SPOT_FULFILLED
          });
          return getArea(dispatch)(endpoint, areaId);
        }).catch(function (err) {
          return dispatch({
            payload: err,
            type: actionDefinition.SUBMIT_NEW_SPOT_REJECTED
          });
        });
      };
    };

    var deleteSpot = function deleteSpot(dispatch) {
      return function (endpoint, areaId, spotId) {
        dispatch({
          type: actionDefinition.DELETE_SPOT_PENDING
        });
        return (0, _frontendJsWeb.fetch)(endpoint + '/' + areaId + '/spot/' + spotId, {
          method: 'DELETE'
        }).then(function () {
          dispatch({
            type: actionDefinition.DELETE_SPOT_FULFILLED
          });
          return getArea(dispatch)(endpoint, areaId);
        }).catch(function (err) {
          return dispatch({
            payload: err,
            type: actionDefinition.DELETE_SPOT_REJECTED
          });
        });
      };
    };

    var submitSpotChanges = function submitSpotChanges(dispatch) {
      return function (endpoint, areaId, formData) {
        var id = formData.id,
            data = _objectWithoutProperties(formData, ["id"]);

        dispatch({
          type: actionDefinition.SUBMIT_SPOT_CHANGES_PENDING
        });
        return (0, _frontendJsWeb.fetch)(endpoint + '/' + areaId + '/spot/' + id, {
          body: JSON.stringify(data),
          headers: fetchHeaders,
          method: 'PUT'
        }).then(function () {
          dispatch({
            type: actionDefinition.SUBMIT_SPOT_CHANGES_FULFILLED
          });
          return getArea(dispatch)(endpoint, areaId);
        }).catch(function (err) {
          return dispatch({
            payload: err,
            type: actionDefinition.SUBMIT_SPOT_CHANGES_REJECTED
          });
        });
      };
    };

    var actions = {
      createSpot: createSpot,
      deleteSpot: deleteSpot,
      getArea: getArea,
      getProducts: getProducts,
      highlightDetail: highlightDetail,
      resetProducts: resetProducts,
      select: select,
      selectSpot: selectSpot,
      submitNewSpot: submitNewSpot,
      submitSpotChanges: submitSpotChanges,
      unselectSpot: unselectSpot,
      updateFormValue: updateFormValue
    };
    exports.actions = actions;
    var _default = actions;
    exports.default = _default;
    //# sourceMappingURL=area.es.js.map
  }
});
//# sourceMappingURL=area.es.js.map