"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useProductPrice = void 0;
const react_1 = __importDefault(require("react"));
const currency_js_1 = __importDefault(require("currency.js"));
const useReactifySearchContext_1 = require("./useReactifySearchContext");
const useProductPrice = (product) => {
    const reactifySearchContext = (0, useReactifySearchContext_1.useReactifySearchContext)();
    const value = react_1.default.useMemo(() => {
        const getCurrencyCode = () => {
            var _a, _b, _c;
            const currencyCode = (_b = (_a = product.presentment_price_ranges) === null || _a === void 0 ? void 0 : _a.min_variant_price[0].currency_code) !== null && _b !== void 0 ? _b : "AUD";
            const currencyCodeMarket = (_c = reactifySearchContext.config.markets.find((market) => market.id === reactifySearchContext.options.market)) === null || _c === void 0 ? void 0 : _c.currencyCode;
            return currencyCodeMarket !== null && currencyCodeMarket !== void 0 ? currencyCodeMarket : currencyCode;
        };
        const getPriceDefault = () => {
            var _a, _b, _c, _d;
            if ((_a = product.variants) === null || _a === void 0 ? void 0 : _a[0].price) {
                return {
                    price: (_b = product.variants) === null || _b === void 0 ? void 0 : _b[0].price,
                    compareAtPrice: (_d = (_c = product.variants) === null || _c === void 0 ? void 0 : _c[0].compare_at_price) !== null && _d !== void 0 ? _d : undefined,
                };
            }
            return;
        };
        const getPriceMarket = () => {
            var _a, _b, _c, _d;
            const currencyCode = getCurrencyCode();
            if (!currencyCode) {
                return;
            }
            const presentmentPrice = (_b = (_a = product.variants) === null || _a === void 0 ? void 0 : _a[0].presentment_prices) === null || _b === void 0 ? void 0 : _b.find((item) => item.price.currency_code === currencyCode);
            if (!presentmentPrice) {
                return;
            }
            return {
                price: presentmentPrice.price.amount,
                compareAtPrice: (_d = (_c = presentmentPrice.compare_at_price) === null || _c === void 0 ? void 0 : _c.amount) !== null && _d !== void 0 ? _d : undefined,
            };
        };
        const getPrice = () => {
            var _a;
            return (_a = getPriceMarket()) !== null && _a !== void 0 ? _a : getPriceDefault();
        };
        const priceData = getPrice();
        if (!priceData) {
            return {
                onSale: false,
                price: undefined,
                currencyCode: "AUD",
                compareAtPrice: undefined,
            };
        }
        const currencyCode = getCurrencyCode();
        const onSale = Boolean(priceData.compareAtPrice && priceData.compareAtPrice > priceData.price);
        const priceAsCurrency = (0, currency_js_1.default)(priceData.price);
        const compareAtPriceAsCurrency = priceData.compareAtPrice ? (0, currency_js_1.default)(priceData.compareAtPrice) : undefined;
        return {
            onSale: onSale,
            price: priceAsCurrency,
            compareAtPrice: compareAtPriceAsCurrency,
            formattedPrice: formatPrice(priceAsCurrency, currencyCode),
            formattedCompareAtPrice: compareAtPriceAsCurrency ? formatPrice(compareAtPriceAsCurrency, currencyCode) : undefined,
            currencyCode: currencyCode,
        };
    }, [product, reactifySearchContext.options.market, reactifySearchContext.config.markets]);
    return value;
};
exports.useProductPrice = useProductPrice;
const formatPrice = (price, currencyCode) => {
    let priceString = new Intl.NumberFormat("en-AU", {
        style: "currency",
        currency: currencyCode,
    }).format(price.value);
    if ("AUD" === currencyCode) {
        priceString = `AU ${priceString}`;
    }
    return priceString;
};
//# sourceMappingURL=useProductPrice.js.map