"use strict";
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.vgsMemberPaymentHandler = void 0;
const contexts_1 = require("../../contexts/index.js");
const hooks_1 = require("../../hooks/index.js");
const core_1 = require("@roomstay/core");
const react_1 = __importDefault(require("react"));
const InlinePaymentOption_1 = __importDefault(require("../../components/generic/PaymentCard/InlinePaymentOption"));
const vgsMemberPaymentHandler = () => {
    var _a;
    const { hotel, isMemberPaymentInformationAvailable } = (0, hooks_1.useCurrentHotel)();
    const { cards, roomstayMember } = (0, contexts_1.useMemberContext)();
    // In this instance Passthrough means that the card details are forwarded via VGS.
    const isPassthrough = (hotel === null || hotel === void 0 ? void 0 : hotel.cardProcessor) ? hotel.cardProcessor === core_1.EHotelCardProcessor.Passthrough : true;
    let selectedCard = cards === null || cards === void 0 ? void 0 : cards[0];
    if (roomstayMember === null || roomstayMember === void 0 ? void 0 : roomstayMember.defaultCard) {
        selectedCard = cards === null || cards === void 0 ? void 0 : cards.find((card) => card.cardId === (roomstayMember === null || roomstayMember === void 0 ? void 0 : roomstayMember.defaultCard));
    }
    return {
        name: 'VGSMember',
        isAvailable: isPassthrough && !!((_a = hotel === null || hotel === void 0 ? void 0 : hotel.paymentMethods) === null || _a === void 0 ? void 0 : _a.includes(core_1.EBookingPaymentMethod.Card)) && !!(cards === null || cards === void 0 ? void 0 : cards.length) && isMemberPaymentInformationAvailable,
        isExclusive: false,
        requireBookingPlaced: false,
        bookingPaymentMethod: core_1.EBookingPaymentMethod.Card,
        subOptions: cards === null || cards === void 0 ? void 0 : cards.map((card) => ({
            label: react_1.default.createElement(InlinePaymentOption_1.default, { card: card }),
            value: card.cardId,
        })),
        onConfirm: () => __awaiter(void 0, void 0, void 0, function* () {
            return { success: true, message: 'Using saved card' };
        }),
        getBookingPaymentRequestObject: () => __awaiter(void 0, void 0, void 0, function* () {
            return {
                Method: core_1.EBookingPaymentMethod.SavedCard,
                SavedCardID: selectedCard === null || selectedCard === void 0 ? void 0 : selectedCard.cardId,
            };
        }),
        renderInput: () => react_1.default.createElement(InlinePaymentOption_1.default, { card: selectedCard }),
        renderPaymentInput: undefined,
        onSubOptionChange: (value) => {
            selectedCard = cards === null || cards === void 0 ? void 0 : cards.find((card) => card.cardId === value);
        },
    };
};
exports.vgsMemberPaymentHandler = vgsMemberPaymentHandler;
//# sourceMappingURL=VGSMemberPaymentHandler.js.map