import { useLayoutEffect, useMemo, useRef } from "react";
export function useCurrencyInput(amount, locale, currency, formatterFactory, onChange) {
    const formatter = useMemo(() => formatterFactory(locale, currency), []);
    const number = useMemo(() => typeof amount === "number"
        ? amount
        : Number(amount?.replace(/[^0-9]/g, "") || 0) / 100, [amount]);
    const moneyFormat = useMemo(() => {
        return formatter.format(number);
    }, [number, formatter]);
    const lastPosition = useRef(0);
    const inputRef = useRef(null);
    useLayoutEffect(() => {
        const el = inputRef.current;
        const putCaretOn = el.value.length - lastPosition.current;
        el.setSelectionRange(putCaretOn, putCaretOn);
        onChange?.(moneyFormat);
    }, [moneyFormat]);
    return {
        inputRef,
        moneyFormat,
        lastPosition,
    };
}
//# sourceMappingURL=CurrencyInput.hook.js.map