Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
require("../../chunk-BN4qSVkS.cjs");
const require_hooks_useNumberInput_utils_cleanValue = require("./utils/cleanValue.cjs");
const require_hooks_useNumberInput_utils_fixedDecimalValue = require("./utils/fixedDecimalValue.cjs");
const require_hooks_useNumberInput_utils_getSuffix = require("./utils/getSuffix.cjs");
const require_hooks_useNumberInput_utils_formatValue = require("./utils/formatValue.cjs");
const require_hooks_useNumberInput_utils_getLocaleConfig = require("./utils/getLocaleConfig.cjs");
const require_hooks_useNumberInput_utils_isNumber = require("./utils/isNumber.cjs");
const require_hooks_useNumberInput_utils_padTrimValue = require("./utils/padTrimValue.cjs");
const require_hooks_useNumberInput_utils_repositionCursor = require("./utils/repositionCursor.cjs");
let react = require("react");
//#region src/hooks/useNumberInput/useNumberInput.tsx
function useNumberInput({ allowDecimals = true, allowNegativeValue = false, decimalsLimit, maxLength: userMaxLength, value: userValue, onChange, fixedDecimalLength, decimalScale, prefix, suffix, intlConfig, step, min, max, disableGroupSeparators = false, disableAbbreviations = false, decimalSeparator: _decimalSeparator, groupSeparator: _groupSeparator, transformRawValue, formatValueOnBlur = true, ref }) {
	if (_decimalSeparator && require_hooks_useNumberInput_utils_isNumber.isNumber(_decimalSeparator)) throw new Error("decimalSeparator cannot be a number");
	if (_groupSeparator && require_hooks_useNumberInput_utils_isNumber.isNumber(_groupSeparator)) throw new Error("groupSeparator cannot be a number");
	const localeConfig = (0, react.useMemo)(() => require_hooks_useNumberInput_utils_getLocaleConfig.getLocaleConfig(intlConfig), [intlConfig]);
	const decimalSeparator = _decimalSeparator || localeConfig.decimalSeparator || "";
	const groupSeparator = _groupSeparator || localeConfig.groupSeparator || "";
	if (decimalSeparator && groupSeparator && decimalSeparator === groupSeparator && disableGroupSeparators === false) throw new Error("decimalSeparator cannot be the same as groupSeparator");
	const formatValueOptions = {
		decimalSeparator,
		groupSeparator,
		disableGroupSeparators,
		intlConfig,
		prefix: prefix || localeConfig.prefix,
		suffix
	};
	const cleanValueOptions = {
		decimalSeparator,
		groupSeparator,
		allowDecimals,
		decimalsLimit: decimalsLimit || fixedDecimalLength || 2,
		allowNegativeValue,
		disableAbbreviations,
		prefix: prefix || localeConfig.prefix,
		transformRawValue
	};
	const [stateValue, setStateValue] = (0, react.useState)(() => userValue != null ? require_hooks_useNumberInput_utils_formatValue.formatValue({
		...formatValueOptions,
		decimalScale,
		value: String(userValue)
	}) : "");
	const [dirty, setDirty] = (0, react.useState)(false);
	const [cursor, setCursor] = (0, react.useState)(0);
	const [changeCount, setChangeCount] = (0, react.useState)(0);
	const [lastKeyStroke, setLastKeyStroke] = (0, react.useState)(null);
	/**
	* Process change in value
	*/
	const processChange = (value, selectionStart) => {
		setDirty(true);
		const { modifiedValue, cursorPosition } = require_hooks_useNumberInput_utils_repositionCursor.repositionCursor({
			selectionStart,
			value,
			lastKeyStroke,
			stateValue,
			groupSeparator
		});
		const stringValue = require_hooks_useNumberInput_utils_cleanValue.cleanValue({
			value: modifiedValue,
			...cleanValueOptions
		});
		if (userMaxLength && stringValue.replace(/-/g, "").length > userMaxLength) return;
		if (stringValue === "" || stringValue === "-" || stringValue === decimalSeparator) {
			onChange && onChange("", {
				float: null,
				formatted: "",
				value: ""
			});
			setStateValue(stringValue);
			setCursor(1);
			return;
		}
		const stringValueWithoutSeparator = decimalSeparator ? stringValue.replace(decimalSeparator, ".") : stringValue;
		const numberValue = parseFloat(stringValueWithoutSeparator);
		const formattedValue = require_hooks_useNumberInput_utils_formatValue.formatValue({
			value: stringValue,
			...formatValueOptions
		});
		if (cursorPosition != null) {
			let newCursor = cursorPosition + (formattedValue.length - value.length);
			newCursor = newCursor <= 0 ? prefix ? prefix.length : 0 : newCursor;
			setCursor(newCursor);
			setChangeCount(changeCount + 1);
		}
		setStateValue(formattedValue);
		if (onChange) onChange(stringValue, {
			float: numberValue,
			formatted: formattedValue,
			value: stringValue
		});
	};
	/**
	* Handle change event
	*/
	const handleOnChange = (event) => {
		const { target: { value, selectionStart } } = event;
		processChange(value, selectionStart);
	};
	/**
	* Handle focus event
	*/
	const handleOnFocus = (event) => {
		return stateValue ? stateValue.length : 0;
	};
	/**
	* Handle blur event
	*
	* Format value by padding/trimming decimals if required by
	*/
	const handleOnBlur = (event) => {
		const { target: { value } } = event;
		const valueOnly = require_hooks_useNumberInput_utils_cleanValue.cleanValue({
			value,
			...cleanValueOptions
		});
		if (valueOnly === "-" || valueOnly === decimalSeparator || !valueOnly) {
			setStateValue("");
			return;
		}
		const newValue = require_hooks_useNumberInput_utils_padTrimValue.padTrimValue(require_hooks_useNumberInput_utils_fixedDecimalValue.fixedDecimalValue(valueOnly, decimalSeparator, fixedDecimalLength), decimalSeparator, decimalScale !== void 0 ? decimalScale : fixedDecimalLength);
		const numberValue = parseFloat(newValue.replace(decimalSeparator, "."));
		const formattedValue = require_hooks_useNumberInput_utils_formatValue.formatValue({
			...formatValueOptions,
			value: newValue
		});
		if (onChange && formatValueOnBlur) onChange(newValue, {
			float: numberValue,
			formatted: formattedValue,
			value: newValue
		});
		setStateValue(formattedValue);
	};
	/**
	* Handle key down event
	*
	* Increase or decrease value by step
	*/
	const handleOnKeyDown = (event) => {
		const { key } = event;
		setLastKeyStroke(key);
		if (step && (key === "ArrowUp" || key === "ArrowDown")) {
			event.preventDefault();
			setCursor(stateValue.length);
			const currentValue = parseFloat(userValue != null ? String(userValue).replace(decimalSeparator, ".") : require_hooks_useNumberInput_utils_cleanValue.cleanValue({
				value: stateValue,
				...cleanValueOptions
			})) || 0;
			const newValue = key === "ArrowUp" ? currentValue + step : currentValue - step;
			if (min !== void 0 && newValue < Number(min)) return;
			if (max !== void 0 && newValue > Number(max)) return;
			const fixedLength = String(step).includes(".") ? Number(String(step).split(".")[1].length) : void 0;
			processChange(String(fixedLength ? newValue.toFixed(fixedLength) : newValue).replace(".", decimalSeparator));
		}
	};
	/**
	* Handle key up event
	*
	* Move cursor if there is a suffix to prevent user typing past suffix
	*/
	const handleOnKeyUp = (event) => {
		const { key, currentTarget: { selectionStart } } = event;
		if (key !== "ArrowUp" && key !== "ArrowDown" && stateValue !== "-") {
			const suffix = require_hooks_useNumberInput_utils_getSuffix.getSuffix(stateValue, {
				groupSeparator,
				decimalSeparator
			});
			if (suffix && selectionStart && selectionStart > stateValue.length - suffix.length) {
				if (ref && ref.current) {
					const newCursor = stateValue.length - suffix.length;
					ref.current.setSelectionRange(newCursor, newCursor);
				}
			}
		}
	};
	(0, react.useEffect)(() => {
		if (userValue == null) setStateValue("");
	}, [userValue]);
	(0, react.useEffect)(() => {
		if (dirty && stateValue !== "-" && ref && ref.current && document.activeElement === ref.current) ref.current.setSelectionRange(cursor, cursor);
	}, [
		stateValue,
		cursor,
		ref,
		dirty,
		changeCount
	]);
	/**
	* If user has only entered "-" or decimal separator,
	* keep the char to allow them to enter next value
	*/
	const getRenderValue = () => {
		if (userValue != null && stateValue !== "-" && (!decimalSeparator || stateValue !== decimalSeparator)) return require_hooks_useNumberInput_utils_formatValue.formatValue({
			...formatValueOptions,
			decimalScale: dirty ? void 0 : decimalScale,
			value: String(userValue)
		});
		return stateValue;
	};
	return {
		getRenderValue,
		handleOnChange,
		handleOnBlur,
		handleOnFocus,
		handleOnKeyDown,
		handleOnKeyUp
	};
}
//#endregion
exports.useNumberInput = useNumberInput;
