import { getLocale } from "../../utils/locales.js";
import { _objectSpread2 } from "../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import locales_default from "./locales.js";
import React, { useEffect, useMemo, useState } from "react";
import { Input, Popover } from "antd";
import { locales } from "@pisell/utils";
import { VirtualKeyboard } from "@pisell/materials";
import "./index.less";
import Delete from "@pisell/icon/es/Delete";
//#region src/pro/pisellPhoneKeyboard/index.tsx
const PisellPhoneKeyboard = (props) => {
	locales.init(locales_default, getLocale());
	const { onChange, value, popoverProps, virtualKeyboardProps, keyboardProps, virtualKeyInputProps } = props;
	const [open, setOpen] = useState(false);
	const items = [
		{
			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: "ok",
			label: locales.getText("pisell2.text.confirm"),
			disabled: false,
			type: "primary"
		}
	];
	/**
	* @title: 返回逻辑
	* @description:
	* @param {any} e
	* @return {*}
	* @Author: zhiwei.Wang
	* @Date: 2024-11-02 09:30
	*/
	const onOk = (e) => {
		onChange === null || onChange === void 0 || onChange(e);
		setOpen(false);
	};
	const onOpenChange = (e) => {
		setOpen(e);
	};
	useEffect(() => {
		if (open) document.body.classList.add("hide-scroll");
		else setTimeout(() => {
			document.body.classList.remove("hide-scroll");
		}, 150);
	}, [open]);
	useEffect(() => {
		document.body.id = "body";
	}, []);
	const adjustFontSize = useMemo(() => {
		if (!value) return 30;
		const maxFontSize = 60;
		const minFontSize = 10;
		const defaultFontSize = maxFontSize;
		const inputWidth = 248;
		const span = document.createElement("span");
		span.style.position = "absolute";
		span.style.whiteSpace = "nowrap";
		span.style.visibility = "hidden";
		span.style.fontSize = `${defaultFontSize}px`;
		span.textContent = value;
		document.body.appendChild(span);
		let fontSize = maxFontSize;
		while (span.offsetWidth > inputWidth && fontSize > minFontSize) {
			fontSize -= .5;
			span.style.fontSize = `${fontSize}px`;
		}
		document.body.removeChild(span);
		return fontSize;
	}, [value]);
	return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(Popover, _objectSpread2({
		content: /* @__PURE__ */ React.createElement(VirtualKeyboard, _objectSpread2({
			keyboardProps: _objectSpread2({
				keydown: false,
				items,
				onChange: (e) => {
					if (e === "reset") {
						onChange("");
						return;
					}
					if (e === "ok") {
						onOk(value || "");
						return;
					}
					if (e === "delete") {
						onChange((value === null || value === void 0 ? void 0 : value.slice(0, -1)) || "");
						return;
					}
					onChange(value + String(e));
				}
			}, keyboardProps),
			virtualKeyInputProps: _objectSpread2({
				value,
				onChange: (e) => {
					onChange(e.target.value);
				},
				style: {
					textAlign: "left",
					fontSize: adjustFontSize
				}
			}, virtualKeyInputProps)
		}, virtualKeyboardProps)),
		open,
		arrow: false,
		onOpenChange,
		trigger: ["click"]
	}, popoverProps), /* @__PURE__ */ React.createElement(Input, {
		readOnly: true,
		value,
		style: { width: "100%" },
		size: "large"
	})));
};
//#endregion
export { PisellPhoneKeyboard as default };
