Liferay.Loader.define("commerce-frontend-js@3.0.30/components/mini_cart/MiniCart", ['module', 'exports', 'require', 'frontend-js-react-web$classnames', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../ServiceProvider/index', '../../utilities/eventsDefinitions', '../../utilities/notifications', './MiniCartContext', './util/constants', './util/index', './util/labels', './util/views'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = void 0;

    var _classnames = _interopRequireDefault(require("frontend-js-react-web$classnames"));

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

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    var _index = _interopRequireDefault(require("../../ServiceProvider/index"));

    var _eventsDefinitions = require("../../utilities/eventsDefinitions");

    var _notifications = require("../../utilities/notifications");

    var _MiniCartContext = _interopRequireDefault(require("./MiniCartContext"));

    var _constants = require("./util/constants");

    var _index2 = require("./util/index");

    var _labels = require("./util/labels");

    var _views = require("./util/views");

    function _getRequireWildcardCache() {
      if (typeof WeakMap !== "function") return null;var cache = new WeakMap();_getRequireWildcardCache = function _getRequireWildcardCache() {
        return cache;
      };return cache;
    }

    function _interopRequireWildcard(obj) {
      if (obj && obj.__esModule) {
        return obj;
      }if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") {
        return { default: obj };
      }var cache = _getRequireWildcardCache();if (cache && cache.has(obj)) {
        return cache.get(obj);
      }var newObj = {};var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;for (var key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;if (desc && (desc.get || desc.set)) {
            Object.defineProperty(newObj, key, desc);
          } else {
            newObj[key] = obj[key];
          }
        }
      }newObj.default = obj;if (cache) {
        cache.set(obj, newObj);
      }return newObj;
    }

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

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (enumerableOnly) symbols = symbols.filter(function (sym) {
          return Object.getOwnPropertyDescriptor(object, sym).enumerable;
        });keys.push.apply(keys, symbols);
      }return keys;
    }

    function _objectSpread(target) {
      for (var i = 1; i < arguments.length; i++) {
        var source = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          ownKeys(Object(source)).forEach(function (key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
          });
        }
      }return target;
    }

    function _defineProperty(obj, key, value) {
      if (key in obj) {
        Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });
      } else {
        obj[key] = value;
      }return 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 MiniCart(_ref) {
      var cartActionURLs = _ref.cartActionURLs,
          cartViews = _ref.cartViews,
          displayDiscountLevels = _ref.displayDiscountLevels,
          displayTotalItemsQuantity = _ref.displayTotalItemsQuantity,
          itemsQuantity = _ref.itemsQuantity,
          labels = _ref.labels,
          onAddToCart = _ref.onAddToCart,
          orderId = _ref.orderId,
          spritemap = _ref.spritemap,
          summaryDataMapper = _ref.summaryDataMapper,
          toggleable = _ref.toggleable;

      var CartResource = _index.default.DeliveryCartAPI('v1');

      var _useState = (0, _react.useState)(!toggleable),
          _useState2 = _slicedToArray(_useState, 2),
          isOpen = _useState2[0],
          setIsOpen = _useState2[1];

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          isUpdating = _useState4[0],
          setIsUpdating = _useState4[1];

      var _useState5 = (0, _react.useState)({
        itemsQuantity: itemsQuantity
      }),
          _useState6 = _slicedToArray(_useState5, 2),
          cartState = _useState6[0],
          updateCartState = _useState6[1];

      var _useState7 = (0, _react.useState)(cartActionURLs),
          _useState8 = _slicedToArray(_useState7, 2),
          actionURLs = _useState8[0],
          setActionURLs = _useState8[1];

      var _useState9 = (0, _react.useState)({}),
          _useState10 = _slicedToArray(_useState9, 2),
          CartViews = _useState10[0],
          setCartViews = _useState10[1];

      var resetCartState = (0, _react.useCallback)(function () {
        return updateCartState({});
      }, [updateCartState]); // eslint-disable-next-line react-hooks/exhaustive-deps

      var updateCartModel = function updateCartModel(_ref2) {
        var cartId = _ref2.orderId;
        return CartResource.getCartByIdWithItems(cartId).then(function (model) {
          if (orderId !== cartId) {
            var orderUUID = model.orderUUID,
                checkoutURL = actionURLs.checkoutURL,
                orderDetailURL = actionURLs.orderDetailURL;
            setActionURLs({
              checkoutURL: checkoutURL,
              orderDetailURL: (0, _index2.regenerateOrderDetailURL)(orderDetailURL, orderUUID)
            });
          }

          updateCartState(_objectSpread(_objectSpread({}, cartState), model));
          onAddToCart(actionURLs, cartState);
        }).catch(_notifications.showErrorNotification);
      };

      (0, _react.useEffect)(function () {
        (0, _views.resolveCartViews)(_objectSpread(_objectSpread({}, _views.DEFAULT_VIEWS), cartViews)).then(function (views) {
          return setCartViews(views);
        });
      }, [cartViews]);
      (0, _react.useEffect)(function () {
        Liferay.on(_eventsDefinitions.ADD_TO_ORDER, updateCartModel);
        Liferay.on(_eventsDefinitions.CHANGE_ORDER, updateCartModel);
        return function () {
          Liferay.detach(_eventsDefinitions.ADD_TO_ORDER, updateCartModel);
          Liferay.detach(_eventsDefinitions.CHANGE_ORDER, updateCartModel);
        };
      }, [updateCartModel]);
      (0, _react.useEffect)(function () {
        if (orderId && orderId !== 0 && isOpen) {
          updateCartModel({
            orderId: orderId
          });
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [isOpen, orderId]);
      (0, _react.useEffect)(function () {
        Liferay.on(_eventsDefinitions.CHANGE_ACCOUNT, resetCartState);
        return function () {
          Liferay.detach(_eventsDefinitions.CHANGE_ACCOUNT, resetCartState);
        };
      }, [resetCartState]);
      return (/*#__PURE__*/_react.default.createElement(_MiniCartContext.default.Provider, {
          value: {
            CartResource: CartResource,
            CartViews: CartViews,
            actionURLs: actionURLs,
            cartState: cartState,
            closeCart: function closeCart() {
              return setIsOpen(false);
            },
            displayDiscountLevels: displayDiscountLevels,
            displayTotalItemsQuantity: displayTotalItemsQuantity,
            isOpen: isOpen,
            isUpdating: isUpdating,
            labels: _objectSpread(_objectSpread({}, _labels.DEFAULT_LABELS), labels),
            setIsUpdating: setIsUpdating,
            spritemap: spritemap,
            summaryDataMapper: summaryDataMapper,
            toggleable: toggleable,
            updateCartModel: updateCartModel,
            updateCartState: updateCartState
          }
        }, !!CartViews[_constants.CART] && /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)({
            'is-open': isOpen || !toggleable,
            'mini-cart': true
          })
        }, toggleable && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
          className: 'mini-cart-overlay',
          onClick: function onClick() {
            return setIsOpen(false);
          }
        }), /*#__PURE__*/_react.default.createElement(CartViews.Opener, {
          openCart: function openCart() {
            return setIsOpen(true);
          }
        })), /*#__PURE__*/_react.default.createElement(CartViews.Cart, null)))
      );
    }

    MiniCart.defaultProps = {
      cartViews: _views.DEFAULT_VIEWS,
      displayDiscountLevels: false,
      displayTotalItemsQuantity: false,
      itemsQuantity: 0,
      labels: _labels.DEFAULT_LABELS,
      onAddToCart: function onAddToCart() {},
      summaryDataMapper: _index2.summaryDataMapper,
      toggleable: true
    };
    var _default = MiniCart;
    exports.default = _default;
    //# sourceMappingURL=MiniCart.js.map
  }
});
//# sourceMappingURL=MiniCart.js.map