(() => {
    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':
                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;
            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',
        'bayarcash_duitnowboost_block',
        'bayarcash_duitnowqris_block',
        'bayarcash_duitnowqriswallet_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);
            console.log(`Successfully registered gateway: ${gatewayId}`);
        } catch (error) {
            console.debug(`Gateway ${gatewayId} not registered - might not be enabled in backend`);
        }
    });
})();