"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentGatewayFactory = void 0;
const database_1 = __importDefault(require("../../../config/database"));
const razorpay_gateway_1 = require("../gateways/razorpay.gateway");
const phonepe_gateway_1 = require("../gateways/phonepe.gateway");
const payu_gateway_1 = require("../gateways/payu.gateway");
const stripe_gateway_1 = require("../gateways/stripe.gateway");
const paypal_gateway_1 = require("../gateways/paypal.gateway");
const upi_gateway_1 = require("../gateways/upi.gateway");
const encryption_utils_1 = require("../../../utils/encryption.utils");
class PaymentGatewayFactory {
    static async getGateway(gatewayName) {
        const name = gatewayName.toUpperCase();
        // Query configuration dynamically from Database
        const setting = await database_1.default.paymentGatewaySetting.findUnique({
            where: { gatewayName: name },
        });
        // Decrypt credentials if database settings exist
        const config = setting
            ? {
                keyId: setting.keyId || undefined,
                keySecret: setting.keySecret ? (0, encryption_utils_1.decrypt)(setting.keySecret) : undefined,
                clientId: setting.clientId || undefined,
                clientSecret: setting.clientSecret ? (0, encryption_utils_1.decrypt)(setting.clientSecret) : undefined,
                merchantId: setting.merchantId || undefined,
                saltKey: setting.saltKey ? (0, encryption_utils_1.decrypt)(setting.saltKey) : undefined,
                saltIndex: setting.saltIndex || undefined,
                webhookSecret: setting.webhookSecret ? (0, encryption_utils_1.decrypt)(setting.webhookSecret) : undefined,
            }
            : {};
        switch (name) {
            case 'RAZORPAY':
                return new razorpay_gateway_1.RazorpayGateway(config);
            case 'PHONEPE':
                return new phonepe_gateway_1.PhonePeGateway(config);
            case 'PAYU':
                return new payu_gateway_1.PayUGateway(config);
            case 'STRIPE':
                return new stripe_gateway_1.StripeGateway(config);
            case 'PAYPAL':
                return new paypal_gateway_1.PayPalGateway(config);
            case 'UPI':
                return new upi_gateway_1.DynamicUpiGateway(config);
            default:
                throw new Error(`Unsupported payment gateway: ${gatewayName}`);
        }
    }
}
exports.PaymentGatewayFactory = PaymentGatewayFactory;
//# sourceMappingURL=payment-gateway.factory.js.map