import { _objectSpread2 } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js";
import { _objectWithoutProperties } from "../../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectWithoutProperties.js";
import useEngineContext from "../../../hooks/useEngineContext.js";
import VirtualInput$1 from "../../../materials/src/components/virtualInput/index.js";
import locales_default from "../locales.js";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Popover } from "antd";
import { locales } from "@pisell/utils";
import { Icon, Keyboard, PisellModal } from "@pisell/materials";
import classNames from "classnames";
import { useControllableValue } from "ahooks";
import "./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 = useEngineContext();
	locales.init(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] = useControllableValue(props, { defaultValue: "" });
	const uniqueKeyboardId = useMemo(() => {
		return `number-keyboard-${generateSimpleUUID()}`;
	}, []);
	const [isSelected, setIsSelected] = useState(!!internalValue);
	useEffect(() => {
		if (internalValue) setIsSelected(true);
	}, []);
	const isOutOfRange = 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 = useMemo(() => {
		if (!isOutOfRange) return "";
		if (errorText) return errorText;
		const numValue = parseFloat(internalValue);
		if (max !== void 0 && numValue > max) return locales.getText("pisell2.keyboard.number-keybard.max")(max);
		if (min !== void 0 && numValue < min) return locales.getText("pisell2.keyboard.number-keybard.min")(min);
		return "";
	}, [
		isOutOfRange,
		errorText,
		internalValue,
		max,
		min
	]);
	const handleValueChange = useCallback((newValue) => {
		if (isSelected && newValue !== "") setIsSelected(false);
		setInternalValue(newValue);
	}, [isSelected, setInternalValue]);
	const handleDelete = useCallback(() => {
		if (isSelected) setIsSelected(false);
		if (internalValue.length > 0) handleValueChange(internalValue.slice(0, -1));
	}, [
		internalValue,
		isSelected,
		handleValueChange
	]);
	const handleClear = useCallback(() => {
		handleValueChange("");
		setIsSelected(false);
	}, [handleValueChange]);
	const handleReset = useCallback(() => {
		setInternalValue(String(originalValue));
		setIsSelected(true);
		onReset === null || onReset === void 0 || onReset();
	}, [
		originalValue,
		setInternalValue,
		onReset
	]);
	const handleDone = useCallback(() => {
		if (isOutOfRange) return;
		onEnter === null || onEnter === void 0 || onEnter(internalValue);
	}, [
		internalValue,
		isOutOfRange,
		onEnter
	]);
	const handleNumberInput = 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 = 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 = 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 || 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 || locales.getText("pisell2.keyboard.number-keybard.ok"),
				disabled: isOutOfRange,
				type: "primary",
				style: { fontSize: 18 }
			}
		];
	}, [
		resetText,
		doneText,
		isOutOfRange
	]);
	const decimalKeyboardItems = 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 = useMemo(() => {
		return [
			{
				value: "clear",
				label: clearText || locales.getText("pisell2.keyboard.number-keybard.clear"),
				disabled: false,
				type: "text",
				size: 1,
				style: { fontSize: 18 }
			},
			{
				value: "reset",
				label: resetText || locales.getText("pisell2.keyboard.number-keybard.reset"),
				disabled: false,
				type: "text",
				size: 1,
				style: {
					color: "#F04438",
					fontSize: 18
				}
			},
			{
				value: "done",
				label: doneText || locales.getText("pisell2.keyboard.number-keybard.ok"),
				disabled: isOutOfRange,
				type: "primary",
				size: 2,
				style: { fontSize: 18 }
			}
		];
	}, [
		clearText,
		resetText,
		doneText,
		isOutOfRange
	]);
	const displayValue = useMemo(() => {
		return internalValue || "";
	}, [internalValue]);
	const renderBottomContent = useCallback(() => {
		if (isOutOfRange && errorMessage) return /* @__PURE__ */ React.createElement("div", { className: "pisell-number-keyboard-error-message" }, /* @__PURE__ */ React.createElement(Icon, {
			type: "pisell2-alert-circle",
			size: 16
		}), errorMessage);
		return null;
	}, [errorMessage]);
	return /* @__PURE__ */ React.createElement("div", {
		className: classNames("pisell-number-keyboard", `pisell-number-keyboard--${mode}`, { "pisell-number-keyboard--disabled": disabled }, className),
		style
	}, label && /* @__PURE__ */ React.createElement("div", { className: "pisell-number-keyboard-label" }, label), /* @__PURE__ */ React.createElement("div", { className: classNames("pisell-number-keyboard-input-wrapper") }, /* @__PURE__ */ React.createElement(VirtualInput$1, {
		focused: !disabled,
		selected: isSelected && !!internalValue,
		value: displayValue,
		placeholder,
		wrapperStyle: _objectSpread2({
			flex: 1,
			padding: "19px 16px",
			borderRadius: "8px",
			backgroundColor: "white",
			boxShadow: "0px 1px 2px rgba(16, 24, 40, 0.06)",
			overflow: "hidden"
		}, inputWrapperStyle),
		style: _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.createElement(Keyboard, {
		id: uniqueKeyboardId,
		className: classNames("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 = _objectWithoutProperties(props, _excluded);
	if (!containerProps) return /* @__PURE__ */ React.createElement(NumberKeyboardBase, keyboardProps);
	const _ref = containerProps || {}, { renderType, open, className } = _ref, restProps = _objectWithoutProperties(_ref, _excluded2);
	if (renderType === "popover") return /* @__PURE__ */ React.createElement(Popover, _objectSpread2(_objectSpread2({
		key: `${open}`,
		open,
		trigger: ["click"],
		overlayClassName: classNames("pisell-number-keyboard-popover", className),
		destroyTooltipOnHide: true,
		overlayInnerStyle: { padding: 0 },
		arrow: false
	}, restProps), {}, { content: /* @__PURE__ */ React.createElement(NumberKeyboardBase, keyboardProps) }), children);
	if (renderType === "modal") return open ? /* @__PURE__ */ React.createElement(PisellModal, _objectSpread2({
		platform: "pc",
		open: true,
		width: "unset",
		header: null,
		footer: false,
		mask: false,
		maskClosable: true,
		bodyStyle: {
			padding: "0px",
			backgroundColor: "transparent"
		},
		closeIcon: null,
		className: classNames("pisell-number-keyboard-modal", className),
		destroyOnHidden: true
	}, restProps), /* @__PURE__ */ React.createElement(NumberKeyboardBase, keyboardProps)) : null;
	return /* @__PURE__ */ React.createElement(NumberKeyboardBase, keyboardProps);
};
//#endregion
export { NumberKeyboard as default };
