require("../../../_virtual/_rolldown/runtime.js");
let react = require("react");
let _pisell_utils = require("@pisell/utils");
//#region src/pro/priceKeyboard/hooks/keyActions.ts
/**
* 键盘输入处理 Hook
* @param params 配置参数
* @param params.onChange 值变化回调
* @param params.onEnter 确认回调
* @param params.onReset 重置回调
* @param params.value 当前值
* @param params.min 最小值限制
* @param params.max 最大值限制
* @param params.scale 小数位数限制
* @param params.mode 输入模式：'append' 追加模式（默认）| 'replace' 替换模式
*/
const useKeyActions = (params) => {
	const [error, setError] = (0, react.useState)(null);
	const { onChange, onEnter, onReset, value = "", min, max, mode = "append", scale } = params;
	/**
	* 检查小数位数是否超出限制
	*/
	const checkScale = (0, react.useCallback)((val) => {
		if (scale === void 0) return true;
		const decimalIndex = val.indexOf(".");
		if (decimalIndex === -1) return true;
		return val.substring(decimalIndex + 1).length <= scale;
	}, [scale]);
	/**
	* 数值范围校验
	*/
	const validateRange = (0, react.useCallback)((val) => {
		const trimmedVal = val.trim();
		const numVal = Number(trimmedVal);
		if (isNaN(numVal)) {
			const errorMsg = "invalid";
			setError({ msg: errorMsg });
			return {
				isValid: false,
				errorMsg
			};
		}
		if (min !== void 0 && numVal < min) {
			const errorMsg = "invalid";
			setError({ msg: errorMsg });
			return {
				isValid: false,
				errorMsg
			};
		}
		if (max !== void 0 && numVal > max) {
			const errorMsg = "invalid";
			setError({ msg: errorMsg });
			return {
				isValid: false,
				errorMsg
			};
		}
		setError(null);
		return { isValid: true };
	}, [min, max]);
	/**
	* 处理值变化
	*/
	const handleChangeValue = (0, react.useCallback)((val) => {
		if (!checkScale(val)) return {
			isValid: false,
			errorMsg: "decimal_places_exceeded"
		};
		const validation = validateRange(val);
		if (validation.isValid) onChange === null || onChange === void 0 || onChange(val);
		return validation;
	}, [
		onChange,
		validateRange,
		checkScale
	]);
	/**
	* 处理数字输入（0-9）
	*/
	const handleNumberInput = (0, react.useCallback)((inputVal) => {
		const numStr = String(inputVal);
		if (mode === "replace") return handleChangeValue(numStr);
		if (!value) return handleChangeValue(numStr);
		if (/^0+$/.test(value)) if (numStr === "0") return handleChangeValue(value + "0");
		else return handleChangeValue(numStr);
		if (/^0\.\d*$/.test(value)) return handleChangeValue(value + numStr);
		if (/^0+\d+$/.test(value)) return handleChangeValue(value.replace(/^0+/, "") + numStr);
		return handleChangeValue(value + numStr);
	}, [
		mode,
		value,
		handleChangeValue
	]);
	/**
	* 处理小数点输入
	*/
	const handleDecimalInput = (0, react.useCallback)(() => {
		if (mode === "replace") return handleChangeValue("0.");
		if (value.includes(".")) return;
		if (!value) return handleChangeValue("0.");
		if (/^0+$/.test(value)) return handleChangeValue("0.");
		if (/^0+\d+$/.test(value)) return handleChangeValue(value.replace(/^0+/, "") + ".");
		return handleChangeValue(value + ".");
	}, [
		mode,
		value,
		handleChangeValue
	]);
	/**
	* 处理其他字符输入
	*/
	const handleOtherInput = (0, react.useCallback)((inputVal) => {
		const inputStr = String(inputVal);
		if (mode === "replace") return handleChangeValue(inputStr);
		return handleChangeValue(value + inputStr);
	}, [
		mode,
		value,
		handleChangeValue
	]);
	return {
		handleChange: (0, react.useCallback)((val) => {
			switch (val) {
				case void 0: break;
				case "ok":
				case "enter":
				case "done":
					if (validateRange(value).isValid) onEnter === null || onEnter === void 0 || onEnter(value);
					break;
				case "reset":
					setError(null);
					onReset === null || onReset === void 0 || onReset();
					break;
				case "clear":
					setError(null);
					handleChangeValue("");
					break;
				case "delete":
					handleChangeValue(value.slice(0, -1));
					break;
				case ".":
					handleDecimalInput();
					break;
				default:
					if ((0, _pisell_utils.isNumber)(val) || String(val) === "0") handleNumberInput(val);
					else handleOtherInput(val);
					break;
			}
		}, [
			value,
			validateRange,
			onEnter,
			onReset,
			handleChangeValue,
			handleDecimalInput,
			handleNumberInput,
			handleOtherInput
		]),
		error
	};
};
//#endregion
exports.default = useKeyActions;
