"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = GiftCardRedemption;
const contexts_1 = require("../../../contexts/index.js");
const hooks_1 = require("../../../hooks/index.js");
const gc_frontend_1 = require("@roomstay/gc-frontend");
const react_1 = __importDefault(require("react"));
const react_hook_form_1 = require("react-hook-form");
const Headline_1 = __importDefault(require("../../generic/Headline"));
const LineBreak_1 = __importDefault(require("../../generic/LineBreak"));
const GiftCardFeature_1 = __importDefault(require("../../../providers/feature/GiftCardFeature"));
function GiftCardRedemption() {
    var _a, _b, _c, _d;
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const basketContext = (0, contexts_1.useBasket)();
    const formContext = (0, react_hook_form_1.useFormContext)();
    const totalBookingAmount = basketContext.getTotalPrice() || 0;
    const handleGiftCardApplied = (preauth) => {
        // Store preauth data in form context
        formContext.setValue('giftCardPreauth', preauth);
        formContext.setValue('giftCardApplied', true);
        formContext.setValue('giftCardAmount', Math.abs(preauth.amount));
    };
    const handleGiftCardCancelled = () => {
        // Clear gift card data from form context
        formContext.setValue('giftCardPreauth', undefined);
        formContext.setValue('giftCardApplied', false);
        formContext.setValue('giftCardAmount', 0);
    };
    if (!GiftCardFeature_1.default.isActive() || !GiftCardFeature_1.default.config) {
        return null;
    }
    if (totalBookingAmount <= 0) {
        console.warn('GiftCardRedemption - Total booking amount is 0 or less, not rendering. Amount:', totalBookingAmount);
        return null;
    }
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
            react_1.default.createElement(Headline_1.default, { bold: true }, "Gift Card"),
            react_1.default.createElement(gc_frontend_1.RedeemGiftCardEngine, { companyId: GiftCardFeature_1.default.config.companyId, companyName: (_a = hotel === null || hotel === void 0 ? void 0 : hotel.name) !== null && _a !== void 0 ? _a : '', logoUrl: (_c = (_b = GiftCardFeature_1.default.config.logoUrl) !== null && _b !== void 0 ? _b : hotel === null || hotel === void 0 ? void 0 : hotel.logo.toString()) !== null && _c !== void 0 ? _c : '', homeUrl: (_d = hotel === null || hotel === void 0 ? void 0 : hotel.hotelUrl) !== null && _d !== void 0 ? _d : '', apiUrl: GIFT_CARD_API_URL, assetUrl: GIFT_CARD_ASSET_URL, amount: totalBookingAmount, description: "Pay with a Gift Card.", allowSplitPayment: true, callback: handleGiftCardApplied, onCancel: handleGiftCardCancelled })),
        react_1.default.createElement(LineBreak_1.default, null)));
}
//# sourceMappingURL=GiftCardRedemption.js.map