"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = StepConfirmationForm;
const contexts_1 = require("../../../contexts/index.js");
const hooks_1 = require("../../../hooks/index.js");
const ConfirmationStepContext_1 = require("contexts/ConfirmationStepContext/ConfirmationStepContext");
const HistoryConsistentPush_1 = require("hooks/HistoryConsistentPush");
const react_1 = __importStar(require("react"));
const react_hook_form_1 = require("react-hook-form");
const react_i18next_1 = require("react-i18next");
const Translation_1 = require("translations/Translation");
const AutoAutoHeight_1 = __importDefault(require("../../../animations/AutoAutoHeight"));
const Alert_1 = __importStar(require("../../generic/Alert"));
const SSLSecureBadge_1 = __importDefault(require("../../generic/badging/SSLSecureBadge"));
const BEButton_1 = __importDefault(require("../../generic/BEButton"));
const Headline_1 = __importDefault(require("../../generic/Headline"));
const Icon_1 = __importStar(require("../../generic/Icon/Icon"));
const LineBreak_1 = __importDefault(require("../../generic/LineBreak"));
const LargeLoader_1 = __importDefault(require("../../generic/loader/LargeLoader"));
const PromotionalCodeInput_1 = require("../../generic/PromotionalCodeInput");
const ScrollToTop_1 = __importDefault(require("../../generic/ScrollToTop"));
const Text_1 = __importStar(require("../../generic/Text"));
const TextBox_1 = __importDefault(require("../../generic/TextBox"));
const GiftCardRedemption_1 = __importDefault(require("./GiftCardRedemption"));
const PaymentInformation_1 = __importDefault(require("./PaymentInformation"));
const RoomContactDetails_1 = __importDefault(require("./RoomContactDetails"));
const StepConfirmationAcknowledgement_1 = __importDefault(require("./StepConfirmationAcknowledgement"));
const StepConfirmationCommentsComponent_1 = require("./StepConfirmationCommentsComponent");
const StepConfirmationPoliciesSection_1 = __importDefault(require("./StepConfirmationPoliciesSection"));
const StepConfirmationPolicyBlock_1 = __importDefault(require("./StepConfirmationPolicyBlock"));
const Currency_1 = require("../../../hooks/Currency");
const CurrencyProvider_1 = __importDefault(require("../../../providers/CurrencyProvider"));
const ConfirmationVerifyFeature_1 = __importDefault(require("../../../providers/feature/ConfirmationVerifyFeature"));
const ShowIATANumberOnCheckoutFeature_1 = __importDefault(require("../../../providers/feature/ShowIATANumberOnCheckoutFeature"));
const Color_1 = require("../../../util/Color");
const GetStepRoom_1 = __importDefault(require("../../../util/GetStepRoom"));
const usePaymentHelper_1 = require("../../../util/usePaymentHelper");
function StepConfirmationForm() {
    var _a;
    const { t } = (0, react_i18next_1.useTranslation)();
    const { showPromocodeFieldInCheckout } = (0, react_1.useContext)(contexts_1.HotelOverridesContext);
    const formContext = (0, react_hook_form_1.useFormContext)();
    const { isInPaymentMode, isBackFromPlanpay, bookingSource } = (0, ConfirmationStepContext_1.useConfirmationStep)();
    const basketContext = (0, contexts_1.useBasket)();
    const historyConsistentPush = (0, HistoryConsistentPush_1.useHistoryConsistentPush)();
    const { exclusivePayment } = (0, usePaymentHelper_1.usePaymentHelper)();
    const { currentCurrency } = (0, react_1.useContext)(contexts_1.CompanyContext);
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const returnToPickRooms = () => __awaiter(this, void 0, void 0, function* () {
        historyConsistentPush((0, GetStepRoom_1.default)().getStepUrl());
    });
    // Check if gift card fully covers the booking
    const giftCardAmount = Math.abs(formContext.watch('giftCardAmount') || 0);
    const totalBookingAmount = basketContext.getTotalPrice() || 0;
    const isFullyCoveredByGiftCard = giftCardAmount > 0 && giftCardAmount >= totalBookingAmount;
    // Ensure paymentMethod is set when gift card covers everything
    (0, react_1.useEffect)(() => {
        if (isFullyCoveredByGiftCard && !formContext.getValues('paymentMethod')) {
            formContext.setValue('paymentMethod', 'VGS');
        }
    }, [isFullyCoveredByGiftCard]);
    const currencyDisplayEnhancements = (0, Currency_1.useCurrencyDisplay)({
        children: basketContext.getTotalPrice(),
        toCurrencyCode: hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency,
    });
    const prePaymentInfo = (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
            react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Step.Confirmation.GuestDetails))),
            react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Success },
                react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Confirmation.AlmostDone, { required: '*' })))),
        react_1.default.createElement(RoomContactDetails_1.default, null),
        bookingSource === 'inline' && basketContext.canAddAnotherRoom ? (react_1.default.createElement(Alert_1.default, { type: Alert_1.AlertType.Accent2, heavyPadding: true },
            react_1.default.createElement("div", { className: "u-flex u-w-100 flex-wrap flex-xl-nowrap" },
                react_1.default.createElement("div", null,
                    react_1.default.createElement(Text_1.default, { color: Color_1.Color.Accent, bold: true }, t(Translation_1.Translation.Step.Confirmation.WantMoreRooms)),
                    react_1.default.createElement(Text_1.default, { color: Color_1.Color.Navy }, t(Translation_1.Translation.Step.Confirmation.YouCanAddAndEditRooms))),
                react_1.default.createElement("div", { className: "d-flex align-items-center u-marg-top@xl-" },
                    react_1.default.createElement(BEButton_1.default, { filled: true, primary: true, icon: Icon_1.IconType.Add, iconPosition: "left", onClick: returnToPickRooms }, t(Translation_1.Translation.Step.Confirmation.AddMoreRooms)))))) : (react_1.default.createElement(react_1.default.Fragment, null)),
        ShowIATANumberOnCheckoutFeature_1.default.isActive() && (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                react_1.default.createElement("div", { className: "u-marg-bottom" },
                    react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Step.Confirmation.TravelAgent))),
                react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small },
                        react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Confirmation.Inputs.IATANumber)))),
                react_1.default.createElement("div", { className: "u-marg-bottom--light" },
                    react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small }, t(Translation_1.Translation.Step.Confirmation.Inputs.IATANumberGuide))),
                react_1.default.createElement(TextBox_1.default, Object.assign({ placeholder: t(Translation_1.Translation.Step.Confirmation.Inputs.IATANumber), wide: true }, formContext.register('IATANumber')))),
            react_1.default.createElement(LineBreak_1.default, null))),
        react_1.default.createElement(StepConfirmationCommentsComponent_1.StepConfirmationCommentsComponent, null),
        react_1.default.createElement(LineBreak_1.default, null),
        react_1.default.createElement(GiftCardRedemption_1.default, null),
        !exclusivePayment && (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement("div", { className: "u-marg-bottom--heavy" },
                react_1.default.createElement("div", { className: "u-marg-bottom" },
                    react_1.default.createElement(Headline_1.default, { bold: true }, t(Translation_1.Translation.Navigation.Menu.PaymentInformation))),
                currentCurrency !== (hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency) && (hotel === null || hotel === void 0 ? void 0 : hotel.defaultCurrency) && (react_1.default.createElement("div", { className: "u-marg-bottom--light rs-confirmation-currency-display-enhancements" },
                    react_1.default.createElement(Alert_1.default, { iconAlignCenter: true, icon: Icon_1.IconType.Money, iconColor: Color_1.Color.Navy, type: Alert_1.AlertType.LightGray },
                        react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Navy },
                            react_1.default.createElement(react_i18next_1.Trans, { i18nKey: Translation_1.Translation.Step.General.PaymentChargeNotice, values: {
                                    amount: `${CurrencyProvider_1.default.getShortSymbol(hotel.defaultCurrency)}${currencyDisplayEnhancements.prettyValue}`,
                                    currency: hotel.defaultCurrency,
                                }, components: [react_1.default.createElement("span", { key: "0" }), react_1.default.createElement("u", { key: "1" })] }))))),
                showPromocodeFieldInCheckout && (react_1.default.createElement("div", { className: "u-marg-bottom rs-promotional-code-wrapper" },
                    react_1.default.createElement(PromotionalCodeInput_1.PromotionalCodeInput, null))),
                isFullyCoveredByGiftCard ? (react_1.default.createElement(Alert_1.default, { type: Alert_1.AlertType.Success },
                    react_1.default.createElement("div", null,
                        react_1.default.createElement(Text_1.default, { bold: true }, "No payment required"),
                        react_1.default.createElement(Text_1.default, null, "Your gift card covers the full booking amount. No additional payment is required.")))) : (react_1.default.createElement(react_1.default.Fragment, null,
                    react_1.default.createElement(PaymentInformation_1.default, null),
                    react_1.default.createElement("div", { className: "u-marg-top u-flex align-items-center" },
                        react_1.default.createElement("div", { className: "u-flex align-items-center justify-content-start" },
                            react_1.default.createElement(Icon_1.default, { icon: Icon_1.IconType.Lock, color: Color_1.Color.Success }),
                            react_1.default.createElement("div", { className: "u-marg-left--light" },
                                react_1.default.createElement(Text_1.default, { type: Text_1.TextType.Small, color: Color_1.Color.Success },
                                    react_1.default.createElement("strong", null, t(Translation_1.Translation.Step.Confirmation.Secured))))),
                        react_1.default.createElement(SSLSecureBadge_1.default, null))))),
            react_1.default.createElement(LineBreak_1.default, null))),
        react_1.default.createElement(StepConfirmationPoliciesSection_1.default, null),
        react_1.default.createElement("div", { className: "u-pad-top--light u-marg-bottom" },
            react_1.default.createElement(StepConfirmationPolicyBlock_1.default, null)),
        ConfirmationVerifyFeature_1.default.isActive() && (react_1.default.createElement(react_1.default.Fragment, null,
            react_1.default.createElement(LineBreak_1.default, null),
            react_1.default.createElement("div", null,
                react_1.default.createElement(StepConfirmationAcknowledgement_1.default, null))))));
    return (react_1.default.createElement("form", null, isBackFromPlanpay ? (react_1.default.createElement(LargeLoader_1.default, null)) : (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(AutoAutoHeight_1.default, { open: !exclusivePayment || !isInPaymentMode }, prePaymentInfo),
        exclusivePayment && (react_1.default.createElement(AutoAutoHeight_1.default, { open: isInPaymentMode },
            react_1.default.createElement(ScrollToTop_1.default, null), (_a = exclusivePayment.renderPaymentInput) === null || _a === void 0 ? void 0 :
            _a.call(exclusivePayment)))))));
}
//# sourceMappingURL=StepConfirmationForm.js.map