"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.WindcavePaymentOption = exports.CardNameMapping = exports.EWindcavePaymentNetwork = void 0;
const react_1 = __importStar(require("react"));
const contexts_1 = require("../../../../contexts");
const hooks_1 = require("../../../../hooks");
const Hotel_1 = require("../../../../models/Client/Hotel/Hotel");
const RoomstayThemeEngine_1 = __importDefault(require("../../../../providers/RoomstayThemeEngine"));
const WindcavePaymentOption_module_scss_1 = __importDefault(require("./WindcavePaymentOption.module.scss"));
var EWindcavePaymentNetwork;
(function (EWindcavePaymentNetwork) {
    EWindcavePaymentNetwork["Visa"] = "visa";
    EWindcavePaymentNetwork["Mastercard"] = "mastercard";
    EWindcavePaymentNetwork["JCB"] = "jcb";
    EWindcavePaymentNetwork["DinersClub"] = "diners";
    EWindcavePaymentNetwork["AmericanExpress"] = "amex";
    EWindcavePaymentNetwork["UnionPay"] = "unionpay";
})(EWindcavePaymentNetwork || (exports.EWindcavePaymentNetwork = EWindcavePaymentNetwork = {}));
exports.CardNameMapping = {
    [Hotel_1.EPaymentCardType.Visa]: EWindcavePaymentNetwork.Visa,
    [Hotel_1.EPaymentCardType.Mastercard]: EWindcavePaymentNetwork.Mastercard,
    [Hotel_1.EPaymentCardType.JCB]: EWindcavePaymentNetwork.JCB,
    [Hotel_1.EPaymentCardType.AmericanExpress]: EWindcavePaymentNetwork.AmericanExpress,
    [Hotel_1.EPaymentCardType.DinersClub]: EWindcavePaymentNetwork.DinersClub,
    [Hotel_1.EPaymentCardType.UnionPay]: EWindcavePaymentNetwork.UnionPay,
};
const WINDCAVE_SCRIPTS = [
    { id: 'windcave-drop-in-lib', src: 'https://sec.windcave.com/js/lib/drop-in-v1.js' },
    { id: 'windcave-drop-in-wrapper', src: 'https://sec.windcave.com/js/windcavepayments-dropin-v1.js' },
    { id: 'windcave-hosted-fields-lib', src: 'https://sec.windcave.com/js/lib/hosted-fields-v1.js' },
    { id: 'windcave-hosted-fields-wrapper', src: 'https://sec.windcave.com/js/windcavepayments-hostedfields-v1.js' },
];
function loadWindcaveScripts() {
    return Promise.all(WINDCAVE_SCRIPTS.map(({ id, src }) => RoomstayThemeEngine_1.default.loadScript(src, id)));
}
const WindcavePaymentOption = () => {
    var _a, _b;
    const basketContext = (0, react_1.useContext)(contexts_1.BasketContext);
    const totalPayNow = basketContext.getTotalPayNow();
    const windcaveCheckoutResponse = (_b = (_a = basketContext.reservationData) === null || _a === void 0 ? void 0 : _a.payment) === null || _b === void 0 ? void 0 : _b.windcave;
    const { hotel } = (0, hooks_1.useCurrentHotel)();
    const onSubmit = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
        if (windcaveCheckoutResponse === null || windcaveCheckoutResponse === void 0 ? void 0 : windcaveCheckoutResponse.successUrl) {
            window.location.href = windcaveCheckoutResponse.successUrl;
            window.location.reload();
        }
    }), [windcaveCheckoutResponse === null || windcaveCheckoutResponse === void 0 ? void 0 : windcaveCheckoutResponse.successUrl]);
    (0, react_1.useEffect)(() => {
        if (!(windcaveCheckoutResponse === null || windcaveCheckoutResponse === void 0 ? void 0 : windcaveCheckoutResponse.successUrl))
            return;
        let cancelled = false;
        loadWindcaveScripts()
            .then(() => {
            var _a, _b, _c, _d, _e, _f;
            if (cancelled)
                return;
            if (!((_b = (_a = window.WindcavePayments) === null || _a === void 0 ? void 0 : _a.DropIn) === null || _b === void 0 ? void 0 : _b.create)) {
                console.error('Windcave scripts loaded, but DropIn.create is not available.');
                return;
            }
            window.WindcavePayments.DropIn.create(Object.assign({ container: 'windcave-payment-container', links: windcaveCheckoutResponse.links, card: {
                    supportedCards: (_d = (_c = hotel === null || hotel === void 0 ? void 0 : hotel.paymentCardTypes) === null || _c === void 0 ? void 0 : _c.map((card) => exports.CardNameMapping[card])) !== null && _d !== void 0 ? _d : ['visa', 'mastercard', 'amex'],
                    sideIcons: (_f = (_e = hotel === null || hotel === void 0 ? void 0 : hotel.paymentCardTypes) === null || _e === void 0 ? void 0 : _e.map((card) => exports.CardNameMapping[card])) !== null && _f !== void 0 ? _f : ['visa', 'mastercard', 'amex'],
                }, env: windcaveCheckoutResponse.useTesting ? 'qa' : 'sec', onSuccess: onSubmit, onError: onSubmit }, (totalPayNow && { totalValue: totalPayNow })), () => console.log('Drop-In component was successfully constructed'), () => console.error('Drop-In component has failed'));
        })
            .catch((e) => console.error('Windcave script failed to load!', e));
        return () => {
            cancelled = true;
        };
    }, [windcaveCheckoutResponse === null || windcaveCheckoutResponse === void 0 ? void 0 : windcaveCheckoutResponse.successUrl, windcaveCheckoutResponse === null || windcaveCheckoutResponse === void 0 ? void 0 : windcaveCheckoutResponse.links, onSubmit, hotel]);
    return react_1.default.createElement("div", { id: "windcave-payment-container", className: WindcavePaymentOption_module_scss_1.default.root });
};
exports.WindcavePaymentOption = WindcavePaymentOption;
//# sourceMappingURL=WindcavePaymentOption.js.map