import React, { useContext, useMemo, useState } from 'react';
import cls from 'clsx';
import ConfigProviderContext from '../../config-provider/ConfigProviderContext';
import { LIMIT_TYPE } from '../constants';
import { Stepper } from '../../stepper';
var QUOTA_LIMIT = LIMIT_TYPE.QUOTA_LIMIT,
    STOCK_LIMIT = LIMIT_TYPE.STOCK_LIMIT;

var SkuStepper = function SkuStepper(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

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

  var stepperLimit = 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 = useMemo(function () {
    return +props.startSaleNum < 1 ? 1 : +props.startSaleNum;
  }, [props.startSaleNum]);
  var quotaContent = 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.createElement("div", {
    className: cls(bem('stepper-stock'))
  }, /*#__PURE__*/React.createElement("div", {
    className: cls(bem('stepper-title'))
  }, props.stepperTitle), /*#__PURE__*/React.createElement(Stepper, {
    min: stepperMinLimit,
    max: stepperLimit,
    className: cls(bem('stepper')),
    disableInput: props.disableStepperInput,
    value: props.currentNum,
    onChange: onChange,
    onOverlimit: onOverLimit
  }), !props.hideQuotaText && quotaContent && /*#__PURE__*/React.createElement("span", {
    className: cls(bem('stepper-quota'))
  }, quotaContent));
};

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