const require_runtime = require("../../_virtual/_rolldown/runtime.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _pisell_utils = require("@pisell/utils");
let _pisell_materials = require("@pisell/materials");
require("./PisellPriceKeyboardPopover.less");
//#region src/pro/pisellSimplePriceKeyboardPopover/PisellPriceKeyboardPopover.tsx
const PisellPriceKeyboardPopover = (0, react.forwardRef)(({ children, value = "", onChange, onConfirm, onCancel, showCurrencySymbol = true, useThousandsSeparator = false, selectType = "dark", disabled = false, overlayClassName = "", style }, ref) => {
	const [open, setOpen] = (0, react.useState)(false);
	const [tempValue, setTempValue] = (0, react.useState)("");
	const [isFirstInput, setIsFirstInput] = (0, react.useState)(true);
	(0, react.useImperativeHandle)(ref, () => ({
		open: () => {
			setOpen(true);
			setTempValue(value);
			setIsFirstInput(true);
		},
		close: () => setOpen(false),
		isOpen: () => open
	}));
	const handleOpenChange = (visible) => {
		if (disabled) return;
		setOpen(visible);
		if (visible) {
			setTempValue(value);
			setIsFirstInput(true);
		} else onCancel === null || onCancel === void 0 || onCancel();
	};
	const handlePriceChange = (val) => {
		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);
			const currentValueStr = String(tempValue || "").replace(/[^\d.]/g, "");
			const newValueStr = String(val).replace(/[^\d.]/g, "");
			if (currentValueStr && currentValueStr !== "0" && newValueStr.length > currentValueStr.length && newValueStr.startsWith(currentValueStr)) {
				const 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);
	};
	const handleConfirm = (val) => {
		onConfirm === null || onConfirm === void 0 || onConfirm(val);
		setOpen(false);
	};
	const keyboardContent = /* @__PURE__ */ react.default.createElement("div", { className: "pisell-price-keyboard-popover-content" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellPriceKeyboard, {
		value: tempValue,
		onChange: handlePriceChange,
		onEnter: handleConfirm,
		doneText: _pisell_utils.locales.getText("pisell2.customise-item.save"),
		resetText: _pisell_utils.locales.getText("pisell2.booking.info.text.clear"),
		amountProps: {
			showCurrencySymbol,
			useThousandsSeparator
		},
		selectType,
		backgroundColor: "transparent"
	}));
	return /* @__PURE__ */ react.default.createElement(antd.Popover, {
		open,
		content: keyboardContent,
		trigger: disabled ? [] : ["click"],
		overlayInnerStyle: { padding: 0 },
		destroyTooltipOnHide: true,
		arrow: false,
		align: { offset: [0, 0] },
		overlayClassName: `pisell-price-keyboard-popover ${overlayClassName}`,
		onOpenChange: handleOpenChange,
		overlayStyle: style
	}, children);
});
PisellPriceKeyboardPopover.displayName = "PisellPriceKeyboardPopover";
//#endregion
exports.default = PisellPriceKeyboardPopover;
