"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TrueRewardsModal;
const core_1 = require("@roomstay/core");
const react_1 = __importDefault(require("react"));
const react_hook_form_1 = require("react-hook-form");
const react_i18next_1 = require("react-i18next");
const BEButton_1 = __importDefault(require("../../../generic/BEButton"));
const SimpleModal_1 = __importStar(require("../../../generic/modal/SimpleModal"));
const Text_1 = __importDefault(require("../../../generic/Text"));
const contexts_1 = require("../../../../contexts");
const ConfirmationStepContext_1 = require("../../../../contexts/ConfirmationStepContext/ConfirmationStepContext");
const index_1 = require("../../../../index");
const Translation_1 = require("../../../../translations/Translation");
const TrueRewardsModal_module_scss_1 = __importDefault(require("./TrueRewardsModal.module.scss"));
function TrueRewardsModal() {
    var _a, _b, _c, _d;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { reservationData } = (0, contexts_1.useBasket)();
    const { isLoading, apiErrorResponse } = (0, ConfirmationStepContext_1.useConfirmationStep)();
    const { watch } = (0, react_hook_form_1.useFormContext)();
    const paymentMethod = watch('paymentMethod');
    const redirectUrl = (_b = (_a = reservationData === null || reservationData === void 0 ? void 0 : reservationData.payment) === null || _a === void 0 ? void 0 : _a.external) === null || _b === void 0 ? void 0 : _b.redirectUrl;
    const isSplitPayment = (_c = reservationData === null || reservationData === void 0 ? void 0 : reservationData.payment) === null || _c === void 0 ? void 0 : _c.isSplitPayment;
    const totalAuthorizedAmount = (_d = reservationData === null || reservationData === void 0 ? void 0 : reservationData.payment) === null || _d === void 0 ? void 0 : _d.totalAuthorizedAmount;
    const isPayingWithGiftCard = !isSplitPayment || totalAuthorizedAmount === 0;
    const isOpen = !!redirectUrl && !isLoading && !apiErrorResponse && (paymentMethod === core_1.EBookingPaymentMethod.TrueRewards || isSplitPayment);
    const cancelOnClickHandler = () => {
        const params = new URLSearchParams(window.location.search);
        window.history.replaceState({}, '', `${window.location.pathname}?${params}`);
        window.location.reload();
    };
    const onConfirmationClick = () => {
        if (redirectUrl) {
            window.location.href = redirectUrl;
        }
    };
    return (react_1.default.createElement(SimpleModal_1.default, { open: isOpen, onClose: cancelOnClickHandler, alignCenter: true, size: SimpleModal_1.SimpleModalSize.ExtraSmall },
        react_1.default.createElement("div", { className: TrueRewardsModal_module_scss_1.default.container },
            react_1.default.createElement("div", { className: TrueRewardsModal_module_scss_1.default.content },
                react_1.default.createElement("div", { className: TrueRewardsModal_module_scss_1.default.headline },
                    react_1.default.createElement(index_1.Headline, { bold: true },
                        " ",
                        t(Translation_1.Translation.Step.Confirmation.TrueRewardsRedirectExternalPage),
                        " ")),
                react_1.default.createElement("div", { className: TrueRewardsModal_module_scss_1.default.text },
                    react_1.default.createElement(Text_1.default, null, isPayingWithGiftCard
                        ? isSplitPayment
                            ? t(Translation_1.Translation.Step.Confirmation.TrueRewardsEnterFirstGiftCard)
                            : t(Translation_1.Translation.Step.Confirmation.TrueRewardsEnterGiftCard)
                        : t(Translation_1.Translation.Step.Confirmation.TrueRewardsEnterCreditCard))),
                react_1.default.createElement("div", { className: TrueRewardsModal_module_scss_1.default.buttons },
                    react_1.default.createElement(BEButton_1.default, { size: "large", className: TrueRewardsModal_module_scss_1.default.cancelButton, onClick: cancelOnClickHandler }, t(Translation_1.Translation.Misc.Cancel)),
                    react_1.default.createElement(BEButton_1.default, { size: "large", className: TrueRewardsModal_module_scss_1.default.continueButton, filled: true, primary: true, onClick: onConfirmationClick }, isPayingWithGiftCard
                        ? t(Translation_1.Translation.Step.Confirmation.TrueRewardsContinueToGiftCardPayment)
                        : t(Translation_1.Translation.Step.Confirmation.TrueRewardsContinueToRemainingPayment)))))));
}
//# sourceMappingURL=TrueRewardsModal.js.map