"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.AddToBasketButton = void 0;

var _react = _interopRequireDefault(require("react"));

var _lib = require("@sentecacommerce-theme/lib");

var _Button = require("../../../../../Common/Button");

var AddToBasketButton = function AddToBasketButton(_ref) {
  var _attributeRules$attri;

  var isSticky = _ref.isSticky,
      productId = _ref.productId,
      variantId = _ref.variantId,
      offerId = _ref.offerId,
      isSizeSelected = _ref.isSizeSelected,
      onAddCallback = _ref.onAddCallback,
      metaData = _ref.metaData;

  var _useProductState = (0, _lib.useProductState)(),
      selectedVariant = _useProductState.selectedVariant,
      attributeRules = _useProductState.attributeRules;

  var _useConfigState = (0, _lib.useConfigState)(),
      attributeKeys = _useConfigState.attributeKeys,
      views = _useConfigState.views;

  var _useBasket = (0, _lib.useBasket)({
    productId: productId,
    variantId: variantId,
    offerId: offerId,
    quantity: selectedVariant == null ? void 0 : selectedVariant.quantity
  }),
      addToBasket = _useBasket.addToBasket,
      status = _useBasket.status,
      isLoading = _useBasket.isLoading;

  var _useUIFunctions = (0, _lib.useUIFunctions)(),
      enqueueView = _useUIFunctions.enqueueView;

  var _useGlobalLabels = (0, _lib.useGlobalLabels)(),
      buttons = _useGlobalLabels.buttons,
      infoMessages = _useGlobalLabels.infoMessages;

  var allSizes = attributeRules == null ? void 0 : (_attributeRules$attri = attributeRules[attributeKeys.size]) == null ? void 0 : _attributeRules$attri.values;
  var hasSizes = Boolean(allSizes);

  var onClick = function onClick(e) {
    e.preventDefault();

    if (hasSizes && !isSizeSelected) {
      onAddCallback == null ? void 0 : onAddCallback();
    } else {
      enqueueView(views.bagModal, {
        selectedVariant: selectedVariant,
        metaData: metaData,
        modalState: {
          isOpen: true,
          type: 'basket'
        }
      });
      addToBasket(1);
    }
  };

  var isAvailable = status === _lib.ProductQuantityStatusEnum.Available;
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, isSticky ? /*#__PURE__*/_react["default"].createElement(_Button.Button, {
    disabled: !isAvailable || isLoading,
    variant: isAvailable ? 'primary' : 'disabled',
    onClick: onClick,
    fullWidth: true,
    borderRadius: "none"
  }, isAvailable && buttons.addToBag, status === _lib.ProductQuantityStatusEnum.MaxQuantityReached && infoMessages.maxQuantity, status === _lib.ProductQuantityStatusEnum.OutOfStock && infoMessages.outOfStock) : /*#__PURE__*/_react["default"].createElement(_Button.Button, {
    disabled: !isAvailable || isLoading,
    variant: isAvailable ? 'primary' : 'disabled',
    onClick: onClick,
    fullWidth: true,
    borderRadius: "large"
  }, isAvailable && buttons.addToBag, status === _lib.ProductQuantityStatusEnum.MaxQuantityReached && infoMessages.maxQuantity, status === _lib.ProductQuantityStatusEnum.OutOfStock && infoMessages.outOfStock));
};

exports.AddToBasketButton = AddToBasketButton;
//# sourceMappingURL=index.js.map