import { C as Icon, b as FormMessageList, d as FormGroup, f as FormDescription, x as FormLabel } from "./splitted-input-diff-kvHJ6NHp.js";
import { Zt as cn } from "./connect-MjNGHHUz-BfDIyhte.js";
import { i as TextInputDiff, t as withFieldValidation } from "./with-field-validation-DGjBGqeq.js";
import { t as Input } from "./input-9oyyJREH.js";
import { forwardRef, useId, useRef, useState } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region node_modules/@powerhousedao/document-engineering/dist/src/ui/components/data-entry/currency-code-picker/utils.js
/**
* Get the fiat currencies
* @returns {Currency[]} The fiat currencies
*/
const fiatCurrencies = () => [
	{
		ticker: "USD",
		crypto: false,
		label: "USD",
		symbol: "$"
	},
	{
		ticker: "EUR",
		crypto: false,
		label: "EUR",
		symbol: "€"
	},
	{
		ticker: "GBP",
		crypto: false,
		label: "GBP",
		symbol: "£"
	}
];
/**
* Get the crypto currencies
* @returns {Currency[]} The crypto currencies
*/
const cryptoCurrencies = () => [
	{
		ticker: "DAI",
		crypto: true,
		label: "DAI",
		symbol: "DAI"
	},
	{
		ticker: "ETH",
		crypto: true,
		label: "ETH",
		symbol: "ETH"
	},
	{
		ticker: "MKR",
		crypto: true,
		label: "MKR",
		symbol: "MKR"
	},
	{
		ticker: "SKY",
		crypto: true,
		label: "SKY",
		symbol: "SKY"
	},
	{
		ticker: "USDC",
		crypto: true,
		label: "USDC",
		symbol: "USDC"
	},
	{
		ticker: "USDS",
		crypto: true,
		label: "USDS",
		symbol: "USDS"
	}
];
/**
* Get the currencies
* @returns {Currency[]} The currencies
*/
const currencies = () => {
	return [...fiatCurrencies(), ...cryptoCurrencies()];
};
const getCurrencies = (allowedTypes = "Both") => {
	switch (allowedTypes) {
		case "Fiat": return fiatCurrencies();
		case "Crypto": return cryptoCurrencies();
		case "Both": return currencies();
	}
};
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/scalars/components/number-field/number-field-validations.js
const isInteger = (value) => Number.isInteger(Number(value));
const isFloat = (value) => {
	const number = Number(value);
	return !isNaN(number) && isFinite(number);
};
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
const isValidNumber = (value) => {
	return /^-?\d+(\.\d+)?$/.test(value);
};
const validateNumericType = ({ numericType }) => (value) => {
	if (value === "") return true;
	if (!isValidNumber(value)) return "Value is not a valid number";
	const isPositive = Number(value) > 0;
	const isNegative = Number(value) < 0;
	const isInt = isInteger(value);
	const numberValue = Number(value);
	if (Math.abs(numberValue) > Number.MAX_SAFE_INTEGER && numericType !== "BigInt") return numericType && [
		"Float",
		"PositiveFloat",
		"NegativeFloat",
		"NonNegativeFloat",
		"NonPositiveFloat"
	].includes(numericType) ? "Value is too large for float" : "Value is too large for int";
	switch (numericType) {
		case "PositiveInt":
			if (!isInt) return "Value must be a positive integer";
			if (isPositive) return true;
			return "Value must be a positive integer";
		case "NegativeInt":
			if (!isInt) return "Value must be a negative integer";
			if (Number(value) >= 0) return "Value must be a negative integer";
			if (isNegative) return true;
			return "Value must be a negative integer";
		case "NonNegativeInt":
			if (!isInt) return "Value must be a non-negative integer";
			if (Number(value) >= 0) return true;
			return "Value must be a non-negative integer";
		case "NonPositiveInt":
			if (!isInt) return "Value must be a non-positive integer";
			if (Number(value) <= 0) return true;
			return "Value must be a non-positive integer";
		case "PositiveFloat":
			if (!isFloat(value)) return "Value must be a positive float";
			if (parseFloat(value) > 0) return true;
			return "Value must be a positive float";
		case "NegativeFloat":
			if (!isFloat(value)) return "Value must be a negative float";
			if (parseFloat(value) >= 0) return "Value must be a negative float";
			if (isNegative) return true;
			return "Value must be a negative float";
		case "NonNegativeFloat":
			if (!isFloat(value)) return "Value must be a non-negative float ";
			if (parseFloat(value) >= 0) return true;
			return "Value must be a non-negative float";
		case "NonPositiveFloat":
			if (!isFloat(value)) return "Value must be a non-positive float";
			if (parseFloat(value) <= 0) return true;
			return "Value must be a non-positive float";
		case "Int":
			if (!isInt) return "Value must be an integer";
			return true;
		case "Float":
			if (!isFloat(value)) return "Value must be a float";
			return true;
		case "BigInt":
			if (isInt) return true;
			return "This is not a valid BigInt";
		case void 0: return true;
		default: return true;
	}
};
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/ui/components/data-entry/amount-input/utils.js
const getDefaultUnits = (type) => {
	switch (type) {
		case "AmountFiat": return fiatCurrencies();
		case "AmountCrypto": return cryptoCurrencies();
		case "AmountCurrency": return currencies();
		case "Amount":
		case "AmountPercentage": return [];
		default: return [];
	}
};
/**
* Checks if a given string value represents a valid BigInt.
* A valid BigInt can be a positive or negative integer, or zero,
* without decimal points or other non-digit characters (except for the leading minus sign).
*
* @param value The string to validate.
* @returns True if the value is a valid BigInt string, false otherwise.
*/
const isValidBigInt = (value) => {
	if (!value) return true;
	return /^-?\d+$/.test(value);
};
const isValidNumberGreaterThanMaxSafeInteger = (value) => {
	if (!value) return false;
	return isValidBigInt(value) && Math.abs(Number(value)) > Number.MAX_SAFE_INTEGER;
};
const isNotSafeValue = (value) => {
	return Math.abs(Number(value)) > Number.MAX_SAFE_INTEGER;
};
const displayValueAmount = ({ value, precision, viewPrecision, trailingZeros }) => {
	if (!value) return;
	if (viewPrecision !== void 0 && precision === void 0) {
		const formattedValue = parseFloat(value).toFixed(viewPrecision);
		return trailingZeros ? formattedValue : parseFloat(formattedValue).toString();
	}
	if (precision !== void 0 && viewPrecision === void 0) {
		const formattedValue = parseFloat(value).toFixed(precision);
		return trailingZeros ? formattedValue : parseFloat(formattedValue).toString();
	}
	if (precision !== void 0 && viewPrecision !== void 0) {
		const formattedValue = parseFloat(value).toFixed(viewPrecision);
		return trailingZeros ? formattedValue : parseFloat(formattedValue);
	}
	return value;
};
const handleEventOnChange = (value) => {
	const nativeEvent = new Event("change", {
		bubbles: true,
		cancelable: true
	});
	Object.defineProperty(nativeEvent, "target", {
		value: { value },
		writable: false
	});
	return nativeEvent;
};
const handleEventOnBlur = (value) => {
	const nativeEvent = new Event("blur", {
		bubbles: true,
		cancelable: true
	});
	Object.defineProperty(nativeEvent, "target", {
		value: { value },
		writable: false
	});
	return nativeEvent;
};
const createAmountValue = (inputValue) => {
	return inputValue === "" ? void 0 : inputValue;
};
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/ui/components/data-entry/number-input/utils.js
const regex = /^-?\d*\.?\d*$/;
const floatTypes = [
	"NegativeFloat",
	"PositiveFloat",
	"NonNegativeFloat",
	"NonPositiveFloat",
	"Float"
];
const formatValue = (value, precision, trailingZeros) => {
	const formattedValue = parseFloat(value).toFixed(precision);
	return trailingZeros ? formattedValue : parseFloat(formattedValue).toString();
};
function getDisplayValue(value, transformProps) {
	if (!value) return "";
	const { numericType, precision, trailingZeros = false } = transformProps || {};
	const isFloat = numericType && floatTypes.includes(numericType);
	const numberValue = Number(value);
	if (numericType === "BigInt") return value;
	if (isFloat && precision === 0) return numberValue.toString();
	if (Math.abs(numberValue) > MAX_SAFE_INTEGER) return value.toString();
	if (precision !== 0) return formatValue(value, precision, trailingZeros);
	return value;
}
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/ui/components/data-entry/number-input/use-number-input.js
const useNumberInput = ({ value, maxValue, minValue, step = 1, onChange, numericType, onBlur, trailingZeros = false, precision = 0 }) => {
	const buttonRef = useRef(null);
	const canIncrement = maxValue !== void 0 && (typeof value === "bigint" ? value >= BigInt(maxValue) : Number(value) >= maxValue) || Number(value) > MAX_SAFE_INTEGER && numericType !== "BigInt";
	const canDecrement = minValue !== void 0 && (typeof value === "bigint" ? value <= BigInt(minValue) : Number(value) <= minValue) || Number(value) > MAX_SAFE_INTEGER && numericType !== "BigInt";
	const isBigInt = numericType && numericType === "BigInt";
	const preventInvalidCharsAndHandleArrows = (e) => {
		if ([
			"e",
			"E",
			"+"
		].includes(e.key)) {
			e.preventDefault();
			return;
		}
		if (e.key === "ArrowUp" || e.key === "ArrowDown") {
			e.preventDefault();
			stepValueHandler(e, e.key === "ArrowUp" ? "increment" : "decrement");
		}
	};
	const blockInvalidPaste = (e) => {
		const pastedData = e.clipboardData.getData("Text");
		if (/[eE]/.test(pastedData)) e.preventDefault();
	};
	const preventLetterInput = (event) => {
		const char = event.key;
		if (/^[a-zA-Z]$/.test(char) && !(event.ctrlKey || event.metaKey)) event.preventDefault();
	};
	const stepValueHandler = (e, operation) => {
		e.preventDefault();
		let newValue;
		if (isBigInt) newValue = BigInt(value ?? 0) + BigInt(step || 1) * (operation === "increment" ? BigInt(1) : BigInt(-1));
		else {
			const currentValue = Number(value ?? 0);
			const adjustment = (step || 1) * (operation === "increment" ? 1 : -1);
			newValue = parseFloat((currentValue + adjustment).toFixed(precision || 10));
		}
		if (!isBigInt) {
			if (maxValue !== void 0 && Number(newValue) > maxValue) return;
			if (minValue !== void 0 && Number(newValue) < minValue) return;
		}
		const nativeEvent = new Event("change", {
			bubbles: true,
			cancelable: true
		});
		Object.defineProperty(nativeEvent, "target", {
			value: { value: newValue },
			writable: false
		});
		onChange?.(nativeEvent);
	};
	const handleBlur = (e) => {
		const inputValue = e.target.value;
		if (inputValue === "" || isNaN(Number(inputValue))) {
			onBlur?.(e);
			return;
		}
		const integerTypes = [
			"PositiveInt",
			"NegativeInt",
			"NonNegativeInt",
			"NonPositiveInt",
			"BigInt",
			"Int"
		];
		const formattedValue = getDisplayValue(inputValue, {
			numericType,
			precision,
			trailingZeros
		});
		if (isNotSafeValue(inputValue)) {
			onBlur?.(e);
			return;
		}
		const finalValue = !numericType || integerTypes.includes(numericType) ? parseFloat(inputValue).toString() : formattedValue;
		const nativeEvent = new Event("change", {
			bubbles: true,
			cancelable: true
		});
		Object.defineProperty(nativeEvent, "target", {
			value: { value: finalValue },
			writable: false
		});
		onChange?.(nativeEvent);
		onBlur?.(e);
	};
	return {
		canIncrement,
		canDecrement,
		preventInvalidCharsAndHandleArrows,
		stepValueHandler,
		blockInvalidPaste,
		preventLetterInput,
		isBigInt,
		handleBlur,
		buttonRef
	};
};
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/ui/components/data-entry/number-input/number-input.js
const NumberInputRaw = forwardRef(({ label, name, description, value, defaultValue, onChange, onBlur, errors, warnings, className, id: propId, minValue, maxValue, step = 1, pattern, trailingZeros = false, numericType = "Float", precision = 0, enableStepCarets = false, baseValue, viewMode = "edition", ...props }, ref) => {
	const generatedId = useId();
	const id = propId ?? generatedId;
	const { canIncrement, canDecrement, preventInvalidCharsAndHandleArrows, stepValueHandler, blockInvalidPaste, preventLetterInput, isBigInt, handleBlur, buttonRef } = useNumberInput({
		value,
		maxValue,
		minValue,
		step,
		onChange,
		numericType,
		onBlur,
		trailingZeros,
		precision
	});
	if (viewMode === "edition") return jsxs(FormGroup, { children: [
		label && jsx(FormLabel, {
			htmlFor: id,
			required: props.required,
			disabled: props.disabled,
			hasError: !!errors?.length,
			className: "mb-[3px]",
			children: label
		}),
		jsxs("div", {
			className: "relative flex items-center group",
			children: [jsx(Input, {
				id,
				name,
				className: cn("pr-8", className),
				pattern: isBigInt ? regex.toString() : pattern?.toString(),
				type: "text",
				inputMode: "numeric",
				role: "spinbutton",
				min: minValue,
				max: maxValue,
				"aria-valuemin": minValue,
				"aria-valuemax": maxValue,
				"aria-invalid": !!errors?.length,
				onKeyDown: (e) => {
					preventLetterInput(e);
					preventInvalidCharsAndHandleArrows(e);
				},
				value: value !== void 0 ? value.toString() : defaultValue !== void 0 ? defaultValue.toString() : "",
				onBlur: handleBlur,
				onChange,
				onPaste: blockInvalidPaste,
				ref,
				"data-cast": isBigInt ? "BigInt" : "Number",
				...props
			}), enableStepCarets && jsxs("div", {
				className: "absolute inset-y-2 right-3 flex flex-col justify-center group-focus-within:opacity-100 transition-opacity",
				children: [jsx("button", {
					"aria-label": "Increment",
					disabled: canIncrement || props.disabled,
					onMouseDown: (e) => {
						e.preventDefault();
					},
					type: "button",
					onClick: (e) => {
						stepValueHandler(e, "increment");
						if (buttonRef.current) buttonRef.current.focus();
					},
					children: jsx(Icon, {
						size: 10,
						name: "ChevronDown",
						className: cn("rotate-180 text-gray-700 dark:text-gray-300", (canIncrement || props.disabled) && "cursor-not-allowed")
					})
				}), jsx("button", {
					"aria-label": "Decrement",
					onMouseDown: (e) => {
						e.preventDefault();
					},
					disabled: canDecrement || props.disabled,
					type: "button",
					onClick: (e) => {
						stepValueHandler(e, "decrement");
						if (buttonRef.current) buttonRef.current.focus();
					},
					children: jsx(Icon, {
						size: 10,
						name: "ChevronDown",
						className: cn("items-center justify-center text-gray-700 dark:text-gray-300", (canDecrement || props.disabled) && "cursor-not-allowed")
					})
				})]
			})]
		}),
		description && jsx(FormDescription, { children: description }),
		warnings && jsx(FormMessageList, {
			messages: warnings,
			type: "warning"
		}),
		errors && jsx(FormMessageList, {
			messages: errors,
			type: "error"
		})
	] });
	return jsx(TextInputDiff, {
		value: value ?? defaultValue ?? "",
		viewMode,
		diffMode: "sentences",
		baseValue: baseValue ?? "",
		label,
		required: props.required,
		"data-testid": "number-input-diff"
	});
});
NumberInputRaw.displayName = "NumberInputRaw";
const NumberInputUncontroller = forwardRef((props, ref) => {
	const [value, setValue] = useState(props.value ?? props.defaultValue);
	const handleChange = (e) => {
		const newValue = e.target.value;
		setValue(newValue);
	};
	return jsx(NumberInputRaw, {
		...props,
		value,
		onChange: handleChange,
		ref
	});
});
NumberInputUncontroller.displayName = "NumberInputUncontroller";
const NumberInput = forwardRef((props, ref) => {
	if (props.onChange) return jsx(NumberInputRaw, {
		...props,
		ref
	});
	return jsx(NumberInputUncontroller, {
		...props,
		ref
	});
});
NumberInput.displayName = "NumberInput";
//#endregion
//#region node_modules/@powerhousedao/document-engineering/dist/src/scalars/components/number-field/number-field.js
const NumberField = withFieldValidation(NumberInput, { validations: { _numericType: validateNumericType } });
NumberField.displayName = "NumberField";
//#endregion
export { getDefaultUnits as a, isNotSafeValue as c, isValidNumber as d, getCurrencies as f, displayValueAmount as i, isValidBigInt as l, NumberInput as n, handleEventOnBlur as o, createAmountValue as r, handleEventOnChange as s, NumberField as t, isValidNumberGreaterThanMaxSafeInteger as u };

//# sourceMappingURL=number-field-Bm2o1RCl.js.map