"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.StripePaymentForm = void 0;
const react_stripe_js_1 = require("@stripe/react-stripe-js");
const classnames_1 = __importDefault(require("classnames"));
const react_1 = __importStar(require("react"));
const StripePaymentForm_module_scss_1 = __importDefault(require("./StripePaymentForm.module.scss"));
exports.StripePaymentForm = (0, react_1.forwardRef)((_props, ref) => {
    const stripe = (0, react_stripe_js_1.useStripe)();
    const elements = (0, react_stripe_js_1.useElements)();
    const [message, setMessage] = (0, react_1.useState)();
    const [cardholderName, setCardholderName] = (0, react_1.useState)('');
    (0, react_1.useImperativeHandle)(ref, () => ({
        submitPayment: (redirectUrl) => __awaiter(void 0, void 0, void 0, function* () {
            var _a, _b;
            setMessage(undefined);
            if (!stripe || !elements) {
                return { success: false, message: 'Stripe has not loaded yet.' };
            }
            if (!cardholderName.trim()) {
                setMessage('Cardholder name is required.');
                return { success: false, message: 'Cardholder name is required.' };
            }
            const { error } = yield stripe.confirmPayment({
                elements,
                confirmParams: {
                    return_url: redirectUrl,
                    payment_method_data: {
                        billing_details: {
                            name: cardholderName,
                        },
                    },
                },
            });
            if (error) {
                setMessage((_a = error.message) !== null && _a !== void 0 ? _a : 'Something has gone wrong.');
                return { success: false, message: (_b = error.message) !== null && _b !== void 0 ? _b : 'Something has gone wrong.' };
            }
            return { success: true, message: 'Payment successful' };
        }),
    }));
    const paymentElementOptions = {
        layout: 'accordion',
        fields: {
            billingDetails: {
                name: 'never',
            },
        },
    };
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { className: (0, classnames_1.default)(StripePaymentForm_module_scss_1.default['stripe-field-wrapper'], 'mb-3') },
            react_1.default.createElement("label", { htmlFor: "cardholder-name", className: (0, classnames_1.default)(StripePaymentForm_module_scss_1.default['stripe-label'], { [StripePaymentForm_module_scss_1.default['stripe-label--empty']]: !cardholderName }) }, "Cardholder name"),
            react_1.default.createElement("input", { type: "text", id: "cardholder-name", className: (0, classnames_1.default)(StripePaymentForm_module_scss_1.default['stripe-input'], { [StripePaymentForm_module_scss_1.default['stripe-input--empty']]: !cardholderName }), placeholder: "Full name on card", value: cardholderName, onChange: (e) => setCardholderName(e.target.value), autoComplete: "cc-name", required: true })),
        react_1.default.createElement(react_stripe_js_1.PaymentElement, { id: "payment-element", options: paymentElementOptions }),
        message && (react_1.default.createElement("div", { id: "payment-message", className: "alert alert-danger mt-3" }, message))));
});
exports.StripePaymentForm.displayName = 'StripePaymentForm';
//# sourceMappingURL=StripePaymentForm.js.map