(() => {
    const { __ } = window.wp.i18n;
    const { createElement } = window.wp.element;

    function BayarcashGatewayFields({ id }) {

        let message = '';
        switch(id) {
            case 'bayarcash_block':
                message = __('Pay with online banking Maybank2u, CIMB Clicks, Bank Islam GO and more banks from Malaysia.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_duitnow_block':
                message = __('Pay with online banking Maybank2u, CIMB Clicks, Bank Islam GO and more banks from Malaysia via DuitNow.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_linecredit_block':
                message = __('Pay with Visa or Mastercard credit card.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_duitnowqr_block':
                message = __('Pay using DuitNow QR with any supported mobile banking apps.', 'bayarcash-for-givewp');
                break;
            // case 'bayarcash_duitnowshopee_block': // Temporarily disabled
            //     message = __('Pay using ShopeePay e-wallet.', 'bayarcash-for-givewp');
            //     break;
            case 'bayarcash_duitnowboost_block':
                message = __('Pay using Boost PayFlex.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_duitnowqris_block':
                message = __('Pay with Indonesian online banking services.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_duitnowqriswallet_block':
                message = __('Pay with Indonesian e-wallet services.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_nets_block':
                message = __('Pay with NETS.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_alipay_block':
                message = __('Pay with Alipay.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_wechatpay_block':
                message = __('Pay with WeChat Pay.', 'bayarcash-for-givewp');
                break;
            case 'bayarcash_promptpay_block':
                message = __('Pay with PromptPay.', 'bayarcash-for-givewp');
                break;
            default:
                message = __(`You will be redirected to Bayarcash Payment Gateway.`, 'bayarcash-for-givewp');
        }

        return createElement(
            "div",
            { className: "bayarcash-gateway-fields" },
            createElement(
                "p",
                { className: "give-field-description" },
                message
            )
        );
    }

    const gatewayIds = [
        'bayarcash_block',
        'bayarcash_duitnow_block',
        'bayarcash_linecredit_block',
        'bayarcash_duitnowqr_block',
        // 'bayarcash_duitnowshopee_block', // Temporarily disabled
        'bayarcash_duitnowboost_block',
        'bayarcash_duitnowqris_block',
        'bayarcash_duitnowqriswallet_block',
        'bayarcash_nets_block',
        'bayarcash_alipay_block',
        'bayarcash_wechatpay_block',
        'bayarcash_promptpay_block'
    ];

    // Register each gateway
    gatewayIds.forEach(gatewayId => {
        const gateway = {
            id: gatewayId,
            async beforeCreatePayment(values) {
                return {
                    ...values,
                    gatewayId: gatewayId,
                    bayarcashGatewayIntent: 'bayarcash-gateway-intent',
                };
            },
            Fields: () => createElement(BayarcashGatewayFields, { id: gatewayId })
        };

        try {
            window.givewp.gateways.register(gateway);
        } catch (error) {
            // Gateway not registered - might not be enabled in backend
        }
    });
})();