"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 useProductPrice = (product) => {
    return react_1.default.useMemo(() => {
        var _a, _b, _c, _d;
        const variant = (_a = product.variants) === null || _a === void 0 ? void 0 : _a[0];
        if (!variant) {
            return {
                onSale: false,
                price: undefined,
                currencyCode: "AUD",
                compareAtPrice: undefined,
            };
        }
        // @todo select the appropriate currency
        const priceSet = (_b = variant.presentment_prices) === null || _b === void 0 ? void 0 : _b[0];
        if (!priceSet || !priceSet.price) {
            return {
                onSale: false,
                price: undefined,
                currencyCode: "AUD",
                compareAtPrice: undefined,
            };
        }
        const currencyCode = (_c = priceSet.price.currency_code) !== null && _c !== void 0 ? _c : "AUD";
        const price = priceSet.price.amount
            ? (0, currency_js_1.default)(priceSet.price.amount)
            : undefined;
        const formattedPrice = price ? formatPrice(price, currencyCode) : undefined;
        const compareAtPrice = ((_d = priceSet.compare_at_price) === null || _d === void 0 ? void 0 : _d.amount)
            ? (0, currency_js_1.default)(priceSet.compare_at_price.amount)
            : undefined;
        const formattedCompareAtPrice = compareAtPrice
            ? formatPrice(compareAtPrice, currencyCode)
            : undefined;
        const onSale = !!compareAtPrice && !!price && compareAtPrice.intValue > price.intValue;
        return {
            price,
            onSale,
            currencyCode,
            compareAtPrice,
            formattedPrice,
            formattedCompareAtPrice,
        };
    }, [product]);
};
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