"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCurrencyDisplay = void 0;
const react_1 = require("react");
const contexts_1 = require("../contexts");
const CurrentHotelHook_1 = require("./CurrentHotelHook");
const CurrencyProvider_1 = __importDefault(require("../providers/CurrencyProvider"));
/**
 * Public hook for displaying a converted, formatted currency value.
 *
 * Resolves decimal visibility by reconciling the caller's `hideDecimals`
 * prop with the hotel-level `alwaysShowDecimals` override from
 * `HotelOverridesContext`, then delegates to the internal `useCurrency` hook.
 *
 * @param props - See {@link UseCurrencyDisplayProps}
 * @returns `{ isLoading, symbol, prettyValue, value }` — see `useCurrency`
 *
 * @example
 * const { symbol, prettyValue } = useCurrencyDisplay({ children: 1200, hideDecimals: true });
 * // → { symbol: '$', prettyValue: '1,200' }
 */
const useCurrencyDisplay = (props) => {
    const { hideDecimals, forceHideDecimals } = props, rest = __rest(props, ["hideDecimals", "forceHideDecimals"]);
    const hotelOverridesContext = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const hideEntryDecimals = (0, react_1.useMemo)(() => {
        // First we check if decimals must absolutely not be displayed
        if (forceHideDecimals === true) {
            return true;
        }
        // Then we check if the API overrides the default currency display state
        if (hotelOverridesContext.alwaysShowDecimals === true) {
            return false;
        }
        // Then we rely on how developers origally intened the UI to funciton.
        return hideDecimals;
    }, [forceHideDecimals, hideDecimals, hotelOverridesContext.alwaysShowDecimals]);
    return useCurrency(Object.assign(Object.assign({}, rest), { hideDecimals: hideEntryDecimals }));
};
exports.useCurrencyDisplay = useCurrencyDisplay;
const useCurrency = (props) => {
    var _a;
    const { currentCurrency } = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, CurrentHotelHook_1.useCurrentHotel)();
    const toCurrencyCode = props.toCurrencyCode ? props.toCurrencyCode : currentCurrency;
    const fromCurrencyCode = (_a = props.fromCurrencyCode) !== null && _a !== void 0 ? _a : hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency;
    const [value, setValue] = (0, react_1.useState)(0);
    const [isLoading, setIsLoading] = (0, react_1.useState)(false);
    const toLocalString = (original, toFixed) => {
        if (props.disableLocalization) {
            return original;
        }
        if (original) {
            return original.toLocaleString(undefined, { minimumFractionDigits: toFixed ? 2 : 0 });
        }
        return original;
    };
    (0, react_1.useEffect)(() => {
        let isCancelled = false;
        setIsLoading(true);
        setTimeout(() => {
            if (!isCancelled) {
                setIsLoading(false);
                if (isNaN(props.children)) {
                    setValue(props.children);
                }
                else {
                    const totalValue = CurrencyProvider_1.default.convertPrice(props.children, !props.hideDecimals, toCurrencyCode, fromCurrencyCode);
                    if (!isCancelled) {
                        setValue(totalValue);
                    }
                }
            }
        }, props.disableLoading ? 0 : 200);
        return () => {
            setIsLoading(false);
            isCancelled = true;
        };
    }, [props.children, props.hideDecimals, toCurrencyCode, fromCurrencyCode, CurrencyProvider_1.default.getRatesForCurrencyCode(fromCurrencyCode)]);
    return (0, react_1.useMemo)(() => {
        const stringVal = props.disableLocalization ? value.toString() : value === null || value === void 0 ? void 0 : value.toLocaleString();
        let symbol = CurrencyProvider_1.default.getSymbol(toCurrencyCode);
        if (props.removeCurrencyAfterChar) {
            const priceLength = String(value).length;
            if (priceLength + symbol.length > props.removeCurrencyAfterChar) {
                symbol = CurrencyProvider_1.default.getShortSymbol(toCurrencyCode);
                if (priceLength + symbol.length > props.removeCurrencyAfterChar) {
                    symbol = '';
                }
            }
        }
        let prettyValue = toLocalString(value, !(props.hideDecimals || typeof value !== 'number'));
        if (props.shortenThousandsAfter && props.shortenThousandsAfter <= String(value).length && props.hideDecimals) {
            prettyValue = stringVal.substring(0, stringVal.length - 3) + '…';
        }
        return {
            isLoading,
            symbol,
            prettyValue,
            value,
        };
    }, [value, isLoading]);
};
exports.default = useCurrency;
//# sourceMappingURL=Currency.js.map