import { useIntl } from "react-intl";
import { useEffect, useState } from "react";
/**
 * A custom hook that provides internationalization utilities.
 * It formats messages and numbers, and includes special features for development mode.
 */
export default function useShortIntl() {
    const intl = useIntl();
    const { formatMessage, formatNumber } = intl;
    const [devMode, setDevMode] = useState(false);
    useEffect(() => {
        if (process.env.NODE_ENV === "development") {
            const keyDownDetect = (ev) => {
                if (ev.key === "i" && ev.ctrlKey) {
                    const keyUpDetect = (ev) => {
                        if (ev.key === "i") {
                            setDevMode(false);
                        }
                    };
                    setDevMode(true);
                    window.addEventListener("keyup", keyUpDetect);
                }
            };
            window.addEventListener("keydown", keyDownDetect);
        }
    });
    return {
        ...intl,
        txt: (id, params) => {
            return devMode ? id : formatMessage({ id }, params);
        },
        formatToDoubleDecimal(val, options = {}) {
            return formatNumber(val, {
                minimumFractionDigits: 2,
                maximumFractionDigits: 2,
                ...options,
            });
        },
        formatBigNumber(bigNumber, options) {
            const { decimalSeparator } = intl
                .formatNumberToParts(1000.1)
                .reduce((r, a) => {
                if (a.type === "decimal")
                    return { ...r, decimalSeparator: a.value };
                if (a.type === "group")
                    return { ...r, thousandSeparator: a.value };
                return r;
            }, {});
            const integerPart = bigNumber.integerValue(1);
            const integer = intl.formatNumber(integerPart.toNumber(), options);
            const decimals = bigNumber
                .minus(integerPart)
                .decimalPlaces(options?.maximumFractionDigits || bigNumber.decimalPlaces(), 5)
                .toString()
                .replace("0.", "");
            return `${integer}${decimals !== "0" ? `${decimalSeparator}${decimals}` : ""}`;
        },
    };
}
export function isolateMessages(preffix, strings) {
    return Object.entries(strings).reduce((r, [k, v]) => ({
        ...r,
        [`${preffix}.${k}`]: v,
    }), {});
}
//# sourceMappingURL=useShortIntl.js.map