"use strict";

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

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

var _clsx = _interopRequireDefault(require("clsx"));

var _ConfigProviderContext = _interopRequireDefault(require("../../config-provider/ConfigProviderContext"));

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

var _stepper = require("../../stepper");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }

var QUOTA_LIMIT = _constants.LIMIT_TYPE.QUOTA_LIMIT,
    STOCK_LIMIT = _constants.LIMIT_TYPE.STOCK_LIMIT;

var SkuStepper = function SkuStepper(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

  var _createNamespace = createNamespace('sku', prefixCls),
      bem = _createNamespace[0];

  var _useState = (0, _react.useState)(STOCK_LIMIT),
      limitType = _useState[0],
      setLimitType = _useState[1];

  var stepperLimit = (0, _react.useMemo)(function () {
    var quotaLimit = +props.quota - +props.quotaUsed;
    var limit;
    var currentLimitType; // 无限购时直接取库存，有限购时取限购数和库存数中小的那个

    if (+props.quota > 0 && quotaLimit <= props.stock) {
      // 修正负的limit
      limit = quotaLimit < 0 ? 0 : quotaLimit;
      currentLimitType = QUOTA_LIMIT;
    } else {
      limit = props.stock;
      currentLimitType = STOCK_LIMIT;
    }

    setLimitType(currentLimitType);
    return limit;
  }, [props.quota, props.quotaUsed, props.stock]);
  var stepperMinLimit = (0, _react.useMemo)(function () {
    return +props.startSaleNum < 1 ? 1 : +props.startSaleNum;
  }, [props.startSaleNum]);
  var quotaContent = (0, _react.useMemo)(function () {
    var _props$customStepperC = props.customStepperConfig,
        quotaText = _props$customStepperC.quotaText,
        hideQuotaText = _props$customStepperC.hideQuotaText;
    if (hideQuotaText) return '';
    var text = '';

    if (quotaText) {
      text = quotaText;
    } else {
      var textArr = [];

      if (+props.startSaleNum > 1) {
        textArr.push(props.startSaleNum + "\u4EF6\u8D77\u552E");
      }

      if (+props.quota > 0) {
        textArr.push("\u9650\u8D2D" + +props.quota + "\u4EF6");
      }

      text = textArr.join(',');
    }

    return text;
  }, [JSON.stringify(props.customStepperConfig), props.startSaleNum, props.quota]);

  var checkState = function checkState(min, max) {
    // 如果选择小于起售，则强制变为起售
    if (props.currentNum < min || min > max) {
      props.onChange(min);
    } else if (props.currentNum > max) {
      // 当前选择数量大于最大可选时，需要重置已选数量
      props.onChange(max);
    }

    props.onSkuStepperState({
      valid: min <= max,
      min: min,
      max: max,
      limitType: limitType,
      quota: props.quota,
      quotaUsed: props.quotaUsed,
      startSaleNum: props.startSaleNum
    });
  };

  var setCurrentNum = function setCurrentNum(num) {
    props.onChange(num);
    checkState(stepperMinLimit, stepperLimit);
  };

  var onChange = function onChange(currentValue) {
    var intValue = parseInt(currentValue, 10);
    var handleStepperChange = props.customStepperConfig.handleStepperChange;
    if (handleStepperChange) handleStepperChange(intValue);
    setCurrentNum(currentValue);
  };

  var onOverLimit = function onOverLimit(action) {
    props.onSkuOverLimit({
      action: action,
      limitType: limitType,
      quota: props.quota,
      quotaUsed: props.quotaUsed,
      startSaleNum: props.startSaleNum
    });
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('stepper-stock'))
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('stepper-title'))
  }, props.stepperTitle), /*#__PURE__*/_react.default.createElement(_stepper.Stepper, {
    min: stepperMinLimit,
    max: stepperLimit,
    className: (0, _clsx.default)(bem('stepper')),
    disableInput: props.disableStepperInput,
    value: props.currentNum,
    onChange: onChange,
    onOverlimit: onOverLimit
  }), !props.hideQuotaText && quotaContent && /*#__PURE__*/_react.default.createElement("span", {
    className: (0, _clsx.default)(bem('stepper-quota'))
  }, quotaContent));
};

SkuStepper.defaultProps = {
  quota: 0,
  quotaUsed: 0,
  startSaleNum: 1,
  customStepperConfig: {}
};
var _default = SkuStepper;
exports.default = _default;