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, { useState, useImperativeHandle, forwardRef } from 'react';
import { Popover } from 'antd';
import PisellPriceKeyboard from '@pisell/materials/src/components/pisellAdjustPrice/index';
import { locales } from '@pisell/utils';
import "./PisellPriceKeyboardPopover.less";
var PisellPriceKeyboardPopover = /*#__PURE__*/forwardRef(function (_ref, ref) {
  var children = _ref.children,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? '' : _ref$value,
    onChange = _ref.onChange,
    onConfirm = _ref.onConfirm,
    onCancel = _ref.onCancel,
    _ref$showCurrencySymb = _ref.showCurrencySymbol,
    showCurrencySymbol = _ref$showCurrencySymb === void 0 ? true : _ref$showCurrencySymb,
    _ref$useThousandsSepa = _ref.useThousandsSeparator,
    useThousandsSeparator = _ref$useThousandsSepa === void 0 ? false : _ref$useThousandsSepa,
    _ref$selectType = _ref.selectType,
    selectType = _ref$selectType === void 0 ? 'dark' : _ref$selectType,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$overlayClassName = _ref.overlayClassName,
    overlayClassName = _ref$overlayClassName === void 0 ? '' : _ref$overlayClassName,
    style = _ref.style;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    tempValue = _useState4[0],
    setTempValue = _useState4[1];
  var _useState5 = useState(true),
    _useState6 = _slicedToArray(_useState5, 2),
    isFirstInput = _useState6[0],
    setIsFirstInput = _useState6[1]; // 标记是否是首次输入

  // 暴露给父组件的方法
  useImperativeHandle(ref, function () {
    return {
      open: function open() {
        setOpen(true);
        setTempValue(value);
        setIsFirstInput(true);
      },
      close: function close() {
        return setOpen(false);
      },
      isOpen: function isOpen() {
        return open;
      }
    };
  });
  var handleOpenChange = function handleOpenChange(visible) {
    if (disabled) return;
    setOpen(visible);
    if (visible) {
      // 打开时初始化临时值，并重置首次输入标记
      setTempValue(value);
      setIsFirstInput(true);
    } else {
      // 关闭时调用取消回调
      onCancel === null || onCancel === void 0 || onCancel();
    }
  };

  // 处理价格变化（合并了 onChange 和 onEnter）
  var handlePriceChange = function handlePriceChange(val) {
    // 如果用户点击了清除按钮（值变为空或0），重置首次输入标记
    if ((val === '' || val === 0 || val === '0') && !isFirstInput) {
      setIsFirstInput(true);
      setTempValue(val);
      onChange === null || onChange === void 0 || onChange(val);
      return;
    }
    if (isFirstInput && val !== '' && val !== 0 && val !== '0') {
      setIsFirstInput(false);
      var currentValueStr = String(tempValue || '').replace(/[^\d.]/g, ''); // 去除非数字字符
      var newValueStr = String(val).replace(/[^\d.]/g, '');

      // 如果当前有值且新值是在当前值基础上追加的，说明用户输入了新数字
      if (currentValueStr && currentValueStr !== '0' && newValueStr.length > currentValueStr.length && newValueStr.startsWith(currentValueStr)) {
        // 提取用户真正输入的新数字
        var newInput = newValueStr.slice(currentValueStr.length);
        if (newInput) {
          // 直接使用用户输入的新数字，覆盖原值
          setTempValue(newInput);
          onChange === null || onChange === void 0 || onChange(newInput);
          return;
        }
      }
    }

    // 后续输入或其他情况正常处理
    setTempValue(val);
    onChange === null || onChange === void 0 || onChange(val);
  };

  // 处理确认（点击完成按钮）
  var handleConfirm = function handleConfirm(val) {
    onConfirm === null || onConfirm === void 0 || onConfirm(val);
    setOpen(false);
  };

  // 处理取消
  var handleCancel = function handleCancel() {
    setOpen(false);
    onCancel === null || onCancel === void 0 || onCancel();
  };
  var keyboardContent = /*#__PURE__*/React.createElement("div", {
    className: "pisell-price-keyboard-popover-content"
  }, /*#__PURE__*/React.createElement(PisellPriceKeyboard, {
    value: tempValue,
    onChange: handlePriceChange,
    onEnter: handleConfirm,
    doneText: locales.getText('pisell2.customise-item.save'),
    resetText: locales.getText('pisell2.booking.info.text.clear'),
    amountProps: {
      showCurrencySymbol: showCurrencySymbol,
      useThousandsSeparator: useThousandsSeparator
    },
    selectType: selectType,
    backgroundColor: "transparent"
  }));
  return /*#__PURE__*/React.createElement(Popover, {
    open: open,
    content: keyboardContent,
    trigger: disabled ? [] : ['click'],
    overlayInnerStyle: {
      padding: 0
    },
    destroyTooltipOnHide: true,
    arrow: false,
    align: {
      offset: [0, 0]
    },
    overlayClassName: "pisell-price-keyboard-popover ".concat(overlayClassName),
    onOpenChange: handleOpenChange,
    overlayStyle: style
  }, children);
});
PisellPriceKeyboardPopover.displayName = 'PisellPriceKeyboardPopover';
export default PisellPriceKeyboardPopover;