Liferay.Loader.define("commerce-frontend-js@3.0.30/components/quantity_selector/QuantitySelector", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/icon', 'frontend-js-react-web$classnames', 'frontend-js-react-web$prop-types', 'frontend-js-react-web$react', '../../utilities/throttle'], 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 _icon = _interopRequireWildcard(require("@frontend-taglib-clay$clayui/icon"));

    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 _throttle = _interopRequireDefault(require("../../utilities/throttle"));

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

    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 _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;
    }

    var THROTTLE_TIMEOUT = 1000;

    function QuantitySelector(props) {
      var _useState = (0, _react.useState)(!!props.minQuantity && props.minQuantity > props.quantity ? props.minQuantity : props.quantity),
          _useState2 = _slicedToArray(_useState, 2),
          currentQuantity = _useState2[0],
          setCurrentQuantity = _useState2[1];

      var _useState3 = (0, _react.useState)(currentQuantity + props.multipleQuantity <= props.maxQuantity),
          _useState4 = _slicedToArray(_useState3, 2),
          nextAvailable = _useState4[0],
          setNextAvailable = _useState4[1];

      var _useState5 = (0, _react.useState)(currentQuantity - props.multipleQuantity >= props.minQuantity),
          _useState6 = _slicedToArray(_useState5, 2),
          prevAvailable = _useState6[0],
          setPrevAvailable = _useState6[1];

      var _useState7 = (0, _react.useState)(false),
          _useState8 = _slicedToArray(_useState7, 2),
          isThrottling = _useState8[0],
          setIsThrottling = _useState8[1];

      var inputRef = /*#__PURE__*/(0, _react.createRef)();
      (0, _react.useEffect)(function () {
        setCurrentQuantity(props.quantity);
      }, [props.quantity, setCurrentQuantity]);
      (0, _react.useEffect)(function () {
        if (props.throttleOnUpdate) {
          setIsThrottling(true);
          props.onUpdate(currentQuantity).then(function () {
            return setIsThrottling(false);
          });
        } else {
          props.onUpdate(currentQuantity);
        } // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [currentQuantity]);
      (0, _react.useEffect)(function () {
        setNextAvailable(currentQuantity + props.multipleQuantity <= props.maxQuantity);
        setPrevAvailable(currentQuantity - props.multipleQuantity >= props.minQuantity);
      }, [currentQuantity, props.maxQuantity, props.minQuantity, props.multipleQuantity]);

      function updateCurrentQuantity(newQuantity) {
        if (newQuantity >= props.minQuantity && newQuantity <= props.maxQuantity && newQuantity % props.multipleQuantity === 0) {
          setCurrentQuantity(newQuantity);
        }
      }

      function handleInputChange() {
        var value = inputRef.current.value;
        return updateCurrentQuantity(parseInt(value, 10));
      }

      var decreaseQuantity = (0, _throttle.default)(function _decreaseQuantity() {
        if (prevAvailable) {
          updateCurrentQuantity(currentQuantity - props.multipleQuantity);
        }
      }, THROTTLE_TIMEOUT),
          increaseQuantity = (0, _throttle.default)(function _increaseQuantity() {
        if (nextAvailable) {
          updateCurrentQuantity(currentQuantity + props.multipleQuantity);
        }
      }, THROTTLE_TIMEOUT);

      var btnSizeClass;
      var formControlSizeClass;

      if (props.size === 'large') {
        btnSizeClass = 'btn-lg';
        formControlSizeClass = 'form-control-lg';
      }

      if (props.size === 'small') {
        btnSizeClass = 'btn-sm';
        formControlSizeClass = 'form-control-sm';
      }

      var content = /*#__PURE__*/_react.default.createElement("div", {
        className: "quantity-selector"
      }, props.allowedQuantities ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("select", {
        className: (0, _classnames.default)('form-control', formControlSizeClass),
        name: props.inputName,
        onChange: function handleSelectChange() {
          var value = inputRef.current.value;
          setCurrentQuantity(value);
        },
        ref: inputRef,
        value: currentQuantity
      }, props.allowedQuantities.map(function (val) {
        return (/*#__PURE__*/_react.default.createElement("option", {
            key: val,
            value: val
          }, val)
        );
      }))) : props.style === 'simple' ? /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group input-group-sm simple"
      }, (props.prependedIcon || props.prependedText) && /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group-item input-group-item-shrink input-group-prepend"
      }, /*#__PURE__*/_react.default.createElement("span", {
        className: "input-group-text"
      }, props.prependedIcon ? /*#__PURE__*/_react.default.createElement(_icon.default, {
        symbol: props.prependedIcon
      }) : props.prependedText)), /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)('input-group-item input-group-item-shrink', (props.appendedIcon || props.appendedText) && 'input-group-prepend')
      }, /*#__PURE__*/_react.default.createElement("input", {
        className: (0, _classnames.default)('form-control text-center', formControlSizeClass),
        disabled: props.disabled,
        max: props.maxQuantity,
        min: props.minQuantity,
        name: props.inputName,
        onChange: handleInputChange,
        ref: inputRef,
        step: props.multipleQuantity,
        type: "number",
        value: currentQuantity
      })), (props.appendedIcon || props.appendedText) && /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group-append input-group-item input-group-item-shrink"
      }, /*#__PURE__*/_react.default.createElement("span", {
        className: "input-group-text"
      }, props.appendedIcon ? /*#__PURE__*/_react.default.createElement(_icon.default, {
        symbol: props.appendedIcon
      }) : props.appendedText))) : /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group justify-content-center"
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group-item input-group-item-shrink input-group-prepend"
      }, /*#__PURE__*/_react.default.createElement("button", {
        className: (0, _classnames.default)('btn btn-monospaced btn-secondary', btnSizeClass),
        disabled: isThrottling || props.disabled || !prevAvailable,
        onClick: decreaseQuantity,
        type: 'button'
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        symbol: "hr"
      }))), /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group-item input-group-prepend"
      }, /*#__PURE__*/_react.default.createElement("input", {
        className: (0, _classnames.default)('form-control text-center', formControlSizeClass),
        disabled: props.disabled,
        max: props.maxQuantity,
        min: props.minQuantity,
        name: props.inputName,
        onChange: handleInputChange,
        onKeyUp: function handleInputKeyUp(e) {
          switch (e.key) {
            case 'ArrowUp':
              increaseQuantity();
              break;

            case 'ArrowDown':
              decreaseQuantity();
              break;

            case 'Enter':
            default:
              break;
          }
        },
        ref: inputRef,
        step: props.multipleQuantity,
        type: "text",
        value: currentQuantity
      })), /*#__PURE__*/_react.default.createElement("div", {
        className: "input-group-append input-group-item input-group-item-shrink"
      }, /*#__PURE__*/_react.default.createElement("button", {
        className: (0, _classnames.default)('btn btn-monospaced btn-secondary', btnSizeClass),
        disabled: isThrottling || props.disabled || !nextAvailable,
        onClick: increaseQuantity,
        type: 'button'
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        symbol: "plus"
      })))));

      return props.spritemap ? /*#__PURE__*/_react.default.createElement(_icon.ClayIconSpriteContext.Provider, {
        value: props.spritemap
      }, content) : content;
    }

    QuantitySelector.defaultProps = {
      disabled: false,
      maxQuantity: 99999999,
      minQuantity: 1,
      multipleQuantity: 1,
      onUpdate: function onUpdate() {},
      style: 'default',
      throttleOnUpdate: false
    };
    var _default = QuantitySelector;
    exports.default = _default;
    //# sourceMappingURL=QuantitySelector.js.map
  }
});
//# sourceMappingURL=QuantitySelector.js.map