const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.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("./PisellPriceKeyboardModal.less");
//#region src/pro/pisellPriceKeyboardModal/PisellPriceKeyboardModal.tsx
const PisellPriceKeyboardModal = ({ visible = false, value = "", onChange, onClose, triggerRef, showCurrencySymbol = true, useThousandsSeparator = false, className, style }) => {
	const [keyboardPosition, setKeyboardPosition] = (0, react.useState)({
		top: 0,
		left: 0
	});
	const keyboardRef = (0, react.useRef)(null);
	const calculatePosition = () => {
		if (!(triggerRef === null || triggerRef === void 0 ? void 0 : triggerRef.current)) return;
		const rect = triggerRef.current.getBoundingClientRect();
		const keyboardWidth = 360;
		const keyboardHeight = 405;
		const viewportWidth = window.innerWidth;
		const viewportHeight = window.innerHeight;
		let top = rect.bottom + window.scrollY + 8;
		let left = rect.left + window.scrollX;
		if (left + keyboardWidth > viewportWidth) left = viewportWidth - keyboardWidth - 16;
		if (left < 16) left = 16;
		if (top + keyboardHeight > viewportHeight + window.scrollY) top = rect.top + window.scrollY - keyboardHeight - 8;
		if (top < window.scrollY + 16) top = window.scrollY + 16;
		setKeyboardPosition({
			top,
			left
		});
	};
	(0, react.useEffect)(() => {
		if (visible) calculatePosition();
	}, [visible, triggerRef]);
	(0, react.useEffect)(() => {
		const handleClickOutside = (event) => {
			var _triggerRef$current;
			if (visible && keyboardRef.current && !keyboardRef.current.contains(event.target) && !(triggerRef === null || triggerRef === void 0 || (_triggerRef$current = triggerRef.current) === null || _triggerRef$current === void 0 ? void 0 : _triggerRef$current.contains(event.target))) onClose === null || onClose === void 0 || onClose();
		};
		if (visible) document.addEventListener("mousedown", handleClickOutside);
		return () => {
			document.removeEventListener("mousedown", handleClickOutside);
		};
	}, [
		visible,
		onClose,
		triggerRef
	]);
	const handlePriceChange = (val) => {
		onChange === null || onChange === void 0 || onChange(val);
	};
	const handleConfirm = (val) => {
		onChange === null || onChange === void 0 || onChange(val);
		onClose === null || onClose === void 0 || onClose();
	};
	if (!visible) return null;
	return /* @__PURE__ */ react.default.createElement("div", { style: {
		position: "fixed",
		top: 0,
		left: 0,
		right: 0,
		bottom: 0,
		zIndex: 1e3,
		pointerEvents: "none"
	} }, /* @__PURE__ */ react.default.createElement(antd.Modal, {
		open: true,
		onCancel: onClose,
		footer: null,
		closable: false,
		centered: false,
		mask: false,
		className: `pisell-price-keyboard-modal positioned ${className || ""}`,
		getContainer: false,
		style: require_objectSpread2._objectSpread2({
			position: "absolute",
			top: keyboardPosition.top,
			left: keyboardPosition.left,
			margin: 0,
			pointerEvents: "auto"
		}, style),
		modalRender: (modal) => /* @__PURE__ */ react.default.createElement("div", { ref: keyboardRef }, modal)
	}, /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellPriceKeyboard, {
		value,
		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: "dark",
		backgroundColor: "transparent"
	})));
};
//#endregion
exports.default = PisellPriceKeyboardModal;
