/**
 * Pay4All myPOS - WooCommerce Blocks integration (Cart / Checkout block).
 *
 * Registers the « Pay4All myPOS » payment method with the WC Blocks
 * payment-method registry so it appears in the new block-based checkout
 * (wp:woocommerce/checkout). The classic checkout (shortcode) does not
 * use this script and gets its registration from `Bootstrap` directly.
 *
 * At submit time, WC Blocks calls our PHP `process_payment()` via the
 * REST endpoint /wc/store/v1/checkout. The PHP gateway sends the customer
 * to the WC "Pay for order" page, which renders the auto-submitting
 * IPCPurchase form. No third-party JS is loaded inside the block itself.
 */
( function () {
    'use strict';

    if (
        ! window.wc ||
        ! window.wc.wcBlocksRegistry ||
        ! window.wc.wcSettings ||
        ! window.wp ||
        ! window.wp.element
    ) {
        return;
    }

    var registry = window.wc.wcBlocksRegistry;
    var settings = window.wc.wcSettings.getSetting( 'pay4all_for_mypos_data', {} );
    var createEl = window.wp.element.createElement;

    var label       = settings.title       || 'Pay4All myPOS';
    var description = settings.description || '';
    var iconUrl     = settings.icon_url    || '';

    var Label = createEl( 'span', { className: 'pay4all-for-mypos-blocks-label' }, label );

    var Content = createEl(
        'div',
        {
            className: 'pay4all-for-mypos-blocks-content',
            style: { display: 'flex', gap: '8px', alignItems: 'center' }
        },
        iconUrl
            ? createEl( 'img', {
                  src:   iconUrl,
                  alt:   '',
                  style: { height: '24px', width: 'auto' }
              } )
            : null,
        createEl( 'span', null, description )
    );

    registry.registerPaymentMethod( {
        name:            'pay4all_for_mypos',
        label:           Label,
        ariaLabel:       label,
        content:         Content,
        edit:            Content,
        canMakePayment:  function () { return true; },
        paymentMethodId: 'pay4all_for_mypos',
        supports: {
            features: settings.supports || [ 'products' ]
        }
    } );
} )();
