import React, { forwardRef, useImperativeHandle, useState } from "react";
import { Popover } from "antd";
import { locales } from "@pisell/utils";
import { PisellPriceKeyboard } from "@pisell/materials";
import "./PisellPriceKeyboardPopover.less";
//#region src/pro/pisellSimplePriceKeyboardPopover/PisellPriceKeyboardPopover.tsx
const PisellPriceKeyboardPopover = forwardRef(({ children, value = "", onChange, onConfirm, onCancel, showCurrencySymbol = true, useThousandsSeparator = false, selectType = "dark", disabled = false, overlayClassName = "", style }, ref) => {
	const [open, setOpen] = useState(false);
	const [tempValue, setTempValue] = useState("");
	const [isFirstInput, setIsFirstInput] = useState(true);
	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.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,
			useThousandsSeparator
		},
		selectType,
		backgroundColor: "transparent"
	}));
	return /* @__PURE__ */ React.createElement(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
export { PisellPriceKeyboardPopover as default };
