import { useSelect } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { PAYMENT_STORE_KEY } from '@woocommerce/block-data';
import { extensionCartUpdate } from '@woocommerce/blocks-checkout';
import { registerPlugin } from '@wordpress/plugins';

const checkoutObserver = () => {

    const activePaymentMethod = useSelect((select) => select(PAYMENT_STORE_KEY).getActivePaymentMethod());

    useEffect(() => {
        extensionCartUpdate({
            namespace: 'wc-koin-gateway-update-total',
            data: {
                payment_method: activePaymentMethod
            },
        });
    }, [activePaymentMethod]);
}

registerPlugin('checkout-observer', {
    render: checkoutObserver,
    scope: 'woocommerce-checkout',
});

