function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useMemo, useState } from 'react';
import { Popover } from 'antd';
import Delete from '@pisell/icon/es/Delete';
import Plus from '@pisell/icon/es/Plus';
import Minus from '@pisell/icon/es/Minus';
// import { VirtualKeyboard } from '@pisell/materials';
import NumberKeyBoard from "../numberKeyboard";
import classNames from 'classnames';
import { locales } from '@pisell/utils';
import "./index.less";
var PisellNumberSelector = function PisellNumberSelector(_ref) {
  var _ref$value = _ref.value,
    value = _ref$value === void 0 ? 0 : _ref$value,
    _ref$min = _ref.min,
    min = _ref$min === void 0 ? 0 : _ref$min,
    _ref$max = _ref.max,
    max = _ref$max === void 0 ? 999 : _ref$max,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 1 : _ref$step,
    _ref$width = _ref.width,
    width = _ref$width === void 0 ? '112px' : _ref$width,
    _ref$styleType = _ref.styleType,
    styleType = _ref$styleType === void 0 ? 'capsule' : _ref$styleType,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    onChange = _ref.onChange,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'middle' : _ref$size,
    className = _ref.className,
    _ref$enablePriceAdjus = _ref.enablePriceAdjust,
    enablePriceAdjust = _ref$enablePriceAdjus === void 0 ? false : _ref$enablePriceAdjus,
    _ref$originValue = _ref.originValue,
    originValue = _ref$originValue === void 0 ? 0 : _ref$originValue;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(value.toString()),
    _useState4 = _slicedToArray(_useState3, 2),
    inputValue = _useState4[0],
    setInputValue = _useState4[1];
  var _useState5 = useState(value.toString()),
    _useState6 = _slicedToArray(_useState5, 2),
    tempValue = _useState6[0],
    setTempValue = _useState6[1]; // 添加临时值状态

  var KeyboardItems = useMemo(function () {
    return [{
      value: 1,
      label: '1',
      disabled: false,
      type: 'text'
    }, {
      value: 2,
      label: '2',
      disabled: false,
      type: 'text'
    }, {
      value: 3,
      label: '3',
      disabled: false,
      type: 'text'
    }, {
      value: 4,
      label: '4',
      disabled: false,
      type: 'text'
    }, {
      value: 5,
      label: '5',
      disabled: false,
      type: 'text'
    }, {
      value: 6,
      label: '6',
      disabled: false,
      type: 'text'
    }, {
      value: 7,
      label: '7',
      disabled: false,
      type: 'text'
    }, {
      value: 8,
      label: '8',
      disabled: false,
      type: 'text'
    }, {
      value: 9,
      label: '9',
      disabled: false,
      type: 'text'
    }, {
      value: 'delete',
      label: /*#__PURE__*/React.createElement(Delete, {
        className: "virtual-keyboard-input-delete-icon"
      }),
      disabled: false,
      type: 'text'
    }, {
      value: 0,
      label: '0',
      disabled: false,
      type: 'text'
    }, {
      value: 'verify',
      label: locales.getText('confirm') || '确认',
      disabled: false,
      type: 'primary'
    }];
  }, []);

  // 减少数值
  var handleDecrease = function handleDecrease(e) {
    if (disabled) return;
    e.stopPropagation(); // 阻止事件冒泡，避免触发键盘
    var newValue = Math.max(min, value - step);
    onChange === null || onChange === void 0 || onChange(newValue);
  };

  // 增加数值
  var handleIncrease = function handleIncrease(e) {
    if (disabled) return;
    e.stopPropagation(); // 阻止事件冒泡，避免触发键盘
    var newValue = Math.min(max, value + step);
    onChange === null || onChange === void 0 || onChange(newValue);
  };
  var handleChange = function handleChange(e) {
    if (!e) {
      setTempValue(min.toString());
      return;
    }
    if (e === 'delete') {
      // 删除最后一位数字
      var newTempValue = tempValue.slice(0, -1);
      setTempValue(newTempValue || '0');
      return;
    } else if (e === 'verify') {
      // 确认时提交暂存的值
      var newValue = parseInt(tempValue) || 0;
      var clampedValue = Math.max(min, Math.min(max, newValue));
      onChange === null || onChange === void 0 || onChange(clampedValue);
      setOpen(false); // 手动关闭弹窗
      return;
    } else {
      // 更新暂存值，不直接触发onChange
      setTempValue(e);
    }
  };

  // 价格调整面板内容
  var adjustPriceContent = useMemo(function () {
    if (!enablePriceAdjust) return null;
    return /*#__PURE__*/React.createElement(NumberKeyBoard, {
      value: tempValue // 使用临时值
      ,
      max: max,
      onChange: handleChange,
      onOk: function onOk() {
        var newValue = parseInt(tempValue) || 0;
        var clampedValue = Math.max(min, Math.min(max, newValue));
        onChange === null || onChange === void 0 || onChange(clampedValue);
        setOpen(false);
      }
    });
  }, [enablePriceAdjust, tempValue, max, handleChange]);

  // 点击数字的处理
  var handleNumberClick = function handleNumberClick() {
    if (disabled) return;
    if (enablePriceAdjust) {
      // Popover 会自动处理打开，我们只需要设置初始值
      setTempValue(value.toString()); // 设置临时值为当前值
    }
  };

  // 处理弹窗开关时的值同步
  var handleOpenChange = function handleOpenChange(visible) {
    setOpen(visible);
    if (visible) {
      // 打开时设置临时值为当前值
      setTempValue(value.toString());
    } else {
      // 关闭时检查是否需要提交值
      var tempNumber = parseInt(tempValue) || 0;
      var clampedValue = Math.max(min, Math.min(max, tempNumber));
      if (clampedValue !== value) {
        onChange === null || onChange === void 0 || onChange(clampedValue);
      }
      // 重置临时值为当前值，确保下次打开时显示正确的值
      setTempValue(value.toString());
    }
  };

  // 数字显示区域
  var numberDisplayElement = enablePriceAdjust ? /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: classNames('pisell-number-selector-number', {
      'pisell-number-selector-number-clickable': enablePriceAdjust && !disabled
    }),
    onClick: handleNumberClick,
    disabled: disabled
  }, value) : /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-number-selector-number', {
      'pisell-number-selector-number-clickable': enablePriceAdjust && !disabled
    })
  }, value);

  // 如果启用价格调整，只包装数字区域
  var numberElement = enablePriceAdjust ? /*#__PURE__*/React.createElement(Popover, {
    open: open,
    content: adjustPriceContent,
    trigger: ['click'] // 恢复点击触发
    ,
    overlayInnerStyle: {
      padding: 0
    },
    destroyTooltipOnHide: true,
    arrow: false,
    align: {
      offset: [0, 0]
    },
    overlayClassName: "pisell-number-selector-popover",
    onOpenChange: handleOpenChange
  }, numberDisplayElement) : numberDisplayElement;
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-number-selector-new', className, _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, "pisell-number-selector-".concat(size), size), 'pisell-number-selector-disabled', disabled), 'pisell-number-selector-capsule', styleType === 'capsule'), 'pisell-number-selector-separation', styleType === 'separation')),
    style: {
      width: width
    }
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-number-selector-btn pisell-number-selector-minus",
    onClick: handleDecrease,
    disabled: disabled || value <= min
  }, /*#__PURE__*/React.createElement(Minus, {
    size: 16
  })), numberElement, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-number-selector-btn pisell-number-selector-plus",
    onClick: handleIncrease,
    disabled: disabled || value >= max
  }, /*#__PURE__*/React.createElement(Plus, {
    size: 16
  })));
};
export default PisellNumberSelector;