"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const contexts_1 = require("../contexts");
const CurrentHotelHook_1 = require("./CurrentHotelHook");
const CurrencyProvider_1 = __importDefault(require("../providers/CurrencyProvider"));
const useCurrency = (props) => {
    var _a;
    const { currentCurrency } = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, CurrentHotelHook_1.useCurrentHotel)();
    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(() => {
            var _a;
            if (!isCancelled) {
                setIsLoading(false);
                if (isNaN(props.children)) {
                    setValue(props.children);
                }
                else {
                    const totalValue = CurrencyProvider_1.default.convertPrice(props.children, !props.hideDecimals, currentCurrency, (_a = props.originalCurrencyCode) !== null && _a !== void 0 ? _a : hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency);
                    if (!isCancelled) {
                        setValue(totalValue);
                    }
                }
            }
        }, props.disableLoading ? 0 : 200);
        return () => {
            setIsLoading(false);
            isCancelled = true;
        };
    }, [props.children, currentCurrency, props.originalCurrencyCode, CurrencyProvider_1.default.getRatesForCurrencyCode((_a = props.originalCurrencyCode) !== null && _a !== void 0 ? _a : hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency)]);
    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(currentCurrency);
        if (props.removeCurrencyAfterChar) {
            const priceLength = String(value).length;
            if (priceLength + symbol.length > props.removeCurrencyAfterChar) {
                symbol = CurrencyProvider_1.default.getShortSymbol(currentCurrency);
                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