import React from 'react';
import { useBasket, useProductState, ProductQuantityStatusEnum, useUIFunctions, useConfigState, useGlobalLabels } from '@sentecacommerce-theme/lib';
import { Button } from '../../../../../Common/Button';
export var AddToBasketButton = (_ref) => {
  var _attributeRules$attri;

  var {
    isSticky,
    productId,
    variantId,
    offerId,
    isSizeSelected,
    onAddCallback,
    metaData
  } = _ref;
  var {
    selectedVariant,
    attributeRules
  } = useProductState();
  var {
    attributeKeys,
    views
  } = useConfigState();
  var {
    addToBasket,
    status,
    isLoading
  } = useBasket({
    productId,
    variantId,
    offerId,
    quantity: selectedVariant == null ? void 0 : selectedVariant.quantity
  });
  var {
    enqueueView
  } = useUIFunctions();
  var {
    buttons,
    infoMessages
  } = useGlobalLabels();
  var allSizes = attributeRules == null ? void 0 : (_attributeRules$attri = attributeRules[attributeKeys.size]) == null ? void 0 : _attributeRules$attri.values;
  var hasSizes = Boolean(allSizes);

  var 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 === ProductQuantityStatusEnum.Available;
  return /*#__PURE__*/React.createElement(React.Fragment, null, isSticky ? /*#__PURE__*/React.createElement(Button, {
    disabled: !isAvailable || isLoading,
    variant: isAvailable ? 'primary' : 'disabled',
    onClick: onClick,
    fullWidth: true,
    borderRadius: "none"
  }, isAvailable && buttons.addToBag, status === ProductQuantityStatusEnum.MaxQuantityReached && infoMessages.maxQuantity, status === ProductQuantityStatusEnum.OutOfStock && infoMessages.outOfStock) : /*#__PURE__*/React.createElement(Button, {
    disabled: !isAvailable || isLoading,
    variant: isAvailable ? 'primary' : 'disabled',
    onClick: onClick,
    fullWidth: true,
    borderRadius: "large"
  }, isAvailable && buttons.addToBag, status === ProductQuantityStatusEnum.MaxQuantityReached && infoMessages.maxQuantity, status === ProductQuantityStatusEnum.OutOfStock && infoMessages.outOfStock));
};
//# sourceMappingURL=index.js.map