const require_runtime = require("../../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_objectWithoutProperties = require("../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js");
const require_useEngineContext = require("../../../hooks/useEngineContext.js");
const require_index = require("../../../materials/src/components/virtualInput/index.js");
const require_locales = require("../locales.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");
let classnames = require("classnames");
classnames = require_runtime.__toESM(classnames);
let ahooks = require("ahooks");
require("./index.less");
//#region src/pro/keyboards/numberKeyboard/index.tsx
const _excluded = ["containerProps", "children"], _excluded2 = [
	"renderType",
	"open",
	"className"
];
const generateSimpleUUID = () => {
	return Math.random().toString(36).substring(2) + Date.now().toString(36);
};
/**
* NumberKeyboardBase - 数字输入键盘基础组件
* 支持整数模式和小数模式
*/
const NumberKeyboardBase = (props) => {
	var _context$engine;
	const { mode = "integer", originalValue = "", placeholder = "", max, min, maxLength, precision = 2, disabled = false, doneText, resetText, clearText, label, errorText, style, className, keydown = true, inputStyle, inputWrapperStyle, onEnter, onReset } = props;
	const context = require_useEngineContext.default();
	_pisell_utils.locales.init(require_locales.default, (context === null || context === void 0 || (_context$engine = context.engine) === null || _context$engine === void 0 || (_context$engine = _context$engine.props) === null || _context$engine === void 0 ? void 0 : _context$engine.locale) || "en");
	const [internalValue, setInternalValue] = (0, ahooks.useControllableValue)(props, { defaultValue: "" });
	const uniqueKeyboardId = (0, react.useMemo)(() => {
		return `number-keyboard-${generateSimpleUUID()}`;
	}, []);
	const [isSelected, setIsSelected] = (0, react.useState)(!!internalValue);
	(0, react.useEffect)(() => {
		if (internalValue) setIsSelected(true);
	}, []);
	const isOutOfRange = (0, react.useMemo)(() => {
		if (!internalValue) return false;
		const numValue = parseFloat(internalValue);
		if (isNaN(numValue)) return false;
		if (max !== void 0 && numValue > max) return true;
		if (min !== void 0 && numValue < min) return true;
		return false;
	}, [
		internalValue,
		max,
		min
	]);
	const errorMessage = (0, react.useMemo)(() => {
		if (!isOutOfRange) return "";
		if (errorText) return errorText;
		const numValue = parseFloat(internalValue);
		if (max !== void 0 && numValue > max) return _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.max")(max);
		if (min !== void 0 && numValue < min) return _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.min")(min);
		return "";
	}, [
		isOutOfRange,
		errorText,
		internalValue,
		max,
		min
	]);
	const handleValueChange = (0, react.useCallback)((newValue) => {
		if (isSelected && newValue !== "") setIsSelected(false);
		setInternalValue(newValue);
	}, [isSelected, setInternalValue]);
	const handleDelete = (0, react.useCallback)(() => {
		if (isSelected) setIsSelected(false);
		if (internalValue.length > 0) handleValueChange(internalValue.slice(0, -1));
	}, [
		internalValue,
		isSelected,
		handleValueChange
	]);
	const handleClear = (0, react.useCallback)(() => {
		handleValueChange("");
		setIsSelected(false);
	}, [handleValueChange]);
	const handleReset = (0, react.useCallback)(() => {
		setInternalValue(String(originalValue));
		setIsSelected(true);
		onReset === null || onReset === void 0 || onReset();
	}, [
		originalValue,
		setInternalValue,
		onReset
	]);
	const handleDone = (0, react.useCallback)(() => {
		if (isOutOfRange) return;
		onEnter === null || onEnter === void 0 || onEnter(internalValue);
	}, [
		internalValue,
		isOutOfRange,
		onEnter
	]);
	const handleNumberInput = (0, react.useCallback)((num) => {
		let newValue;
		if (isSelected) {
			newValue = num;
			setIsSelected(false);
		} else newValue = internalValue + num;
		if (maxLength && newValue.replace(".", "").length > maxLength) return;
		if (mode === "integer" && newValue.includes(".")) return;
		if (mode === "decimal" && newValue.includes(".")) {
			const parts = newValue.split(".");
			if (parts[1] && parts[1].length > precision) return;
		}
		if (num === "." && internalValue.includes(".")) return;
		if (newValue.length > 1 && newValue.startsWith("0") && !newValue.startsWith("0.")) newValue = newValue.replace(/^0+/, "") || "0";
		if (num === "00") {
			if (internalValue === "" || internalValue === "0") newValue = "0";
		}
		handleValueChange(newValue);
	}, [
		internalValue,
		isSelected,
		maxLength,
		mode,
		precision,
		handleValueChange
	]);
	const handleKeyboardChange = (0, react.useCallback)((key) => {
		if (key === void 0) return;
		const keyStr = String(key);
		switch (keyStr) {
			case "delete":
			case "backspace":
				handleDelete();
				break;
			case "clear":
				handleClear();
				break;
			case "reset":
				handleReset();
				break;
			case "done":
			case "ok":
				handleDone();
				break;
			default:
				if (/^[0-9.]$/.test(keyStr) || keyStr === "00") handleNumberInput(keyStr);
				break;
		}
	}, [
		handleDelete,
		handleClear,
		handleReset,
		handleDone,
		handleNumberInput
	]);
	const integerKeyboardItems = (0, react.useMemo)(() => {
		return [
			{
				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: "reset",
				label: resetText || _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.reset"),
				disabled: false,
				type: "text",
				style: {
					color: "#F04438",
					fontSize: 18
				}
			},
			{
				value: 0,
				label: "0",
				disabled: false,
				type: "text"
			},
			{
				value: "done",
				label: doneText || _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.ok"),
				disabled: isOutOfRange,
				type: "primary",
				style: { fontSize: 18 }
			}
		];
	}, [
		resetText,
		doneText,
		isOutOfRange
	]);
	const decimalKeyboardItems = (0, react.useMemo)(() => {
		return [
			{
				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: 0,
				label: "0",
				disabled: false,
				type: "text"
			},
			{
				value: "00",
				label: "00",
				disabled: false,
				type: "text"
			},
			{
				value: ".",
				label: ".",
				disabled: false,
				type: "text"
			}
		];
	}, []);
	const decimalRightItems = (0, react.useMemo)(() => {
		return [
			{
				value: "clear",
				label: clearText || _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.clear"),
				disabled: false,
				type: "text",
				size: 1,
				style: { fontSize: 18 }
			},
			{
				value: "reset",
				label: resetText || _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.reset"),
				disabled: false,
				type: "text",
				size: 1,
				style: {
					color: "#F04438",
					fontSize: 18
				}
			},
			{
				value: "done",
				label: doneText || _pisell_utils.locales.getText("pisell2.keyboard.number-keybard.ok"),
				disabled: isOutOfRange,
				type: "primary",
				size: 2,
				style: { fontSize: 18 }
			}
		];
	}, [
		clearText,
		resetText,
		doneText,
		isOutOfRange
	]);
	const displayValue = (0, react.useMemo)(() => {
		return internalValue || "";
	}, [internalValue]);
	const renderBottomContent = (0, react.useCallback)(() => {
		if (isOutOfRange && errorMessage) return /* @__PURE__ */ react.default.createElement("div", { className: "pisell-number-keyboard-error-message" }, /* @__PURE__ */ react.default.createElement(_pisell_materials.Icon, {
			type: "pisell2-alert-circle",
			size: 16
		}), errorMessage);
		return null;
	}, [errorMessage]);
	return /* @__PURE__ */ react.default.createElement("div", {
		className: (0, classnames.default)("pisell-number-keyboard", `pisell-number-keyboard--${mode}`, { "pisell-number-keyboard--disabled": disabled }, className),
		style
	}, label && /* @__PURE__ */ react.default.createElement("div", { className: "pisell-number-keyboard-label" }, label), /* @__PURE__ */ react.default.createElement("div", { className: (0, classnames.default)("pisell-number-keyboard-input-wrapper") }, /* @__PURE__ */ react.default.createElement(require_index.default, {
		focused: !disabled,
		selected: isSelected && !!internalValue,
		value: displayValue,
		placeholder,
		wrapperStyle: require_objectSpread2._objectSpread2({
			flex: 1,
			padding: "19px 16px",
			borderRadius: "8px",
			backgroundColor: "white",
			boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.06)",
			overflow: "hidden"
		}, inputWrapperStyle),
		style: require_objectSpread2._objectSpread2({
			fontSize: "48px",
			fontWeight: "bold",
			border: "none",
			background: "transparent",
			width: "100%",
			color: isOutOfRange ? "#D92D20" : void 0
		}, inputStyle),
		preventBlurElements: [`#${uniqueKeyboardId}`],
		allowClear: true,
		onClear: handleDelete,
		textAlign: "center"
	}), renderBottomContent()), /* @__PURE__ */ react.default.createElement(_pisell_materials.Keyboard, {
		id: uniqueKeyboardId,
		className: (0, classnames.default)("pisell-number-keyboard-keys", {
			"pisell-number-keyboard-keys--integer": mode === "integer",
			"pisell-number-keyboard-keys--decimal": mode === "decimal"
		}),
		items: mode === "integer" ? integerKeyboardItems : decimalKeyboardItems,
		rightItems: mode === "decimal" ? decimalRightItems : void 0,
		keydown,
		onChange: handleKeyboardChange
	}));
};
/**
* NumberKeyboard - 数字输入键盘
* 支持基础形式展示、popover 形态或 modal 形态
*/
const NumberKeyboard = (props) => {
	const { containerProps, children } = props, keyboardProps = require_objectWithoutProperties._objectWithoutProperties(props, _excluded);
	if (!containerProps) return /* @__PURE__ */ react.default.createElement(NumberKeyboardBase, keyboardProps);
	const _ref = containerProps || {}, { renderType, open, className } = _ref, restProps = require_objectWithoutProperties._objectWithoutProperties(_ref, _excluded2);
	if (renderType === "popover") return /* @__PURE__ */ react.default.createElement(antd.Popover, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({
		key: `${open}`,
		open,
		trigger: ["click"],
		overlayClassName: (0, classnames.default)("pisell-number-keyboard-popover", className),
		destroyTooltipOnHide: true,
		overlayInnerStyle: { padding: 0 },
		arrow: false
	}, restProps), {}, { content: /* @__PURE__ */ react.default.createElement(NumberKeyboardBase, keyboardProps) }), children);
	if (renderType === "modal") return open ? /* @__PURE__ */ react.default.createElement(_pisell_materials.PisellModal, require_objectSpread2._objectSpread2({
		platform: "pc",
		open: true,
		width: "unset",
		header: null,
		footer: false,
		mask: false,
		maskClosable: true,
		bodyStyle: {
			padding: "0px",
			backgroundColor: "transparent"
		},
		closeIcon: null,
		className: (0, classnames.default)("pisell-number-keyboard-modal", className),
		destroyOnHidden: true
	}, restProps), /* @__PURE__ */ react.default.createElement(NumberKeyboardBase, keyboardProps)) : null;
	return /* @__PURE__ */ react.default.createElement(NumberKeyboardBase, keyboardProps);
};
//#endregion
exports.default = NumberKeyboard;
