import analyticsService from './index';

export const bindAnalyticsEventDeliveryMethodSelected = () => {
    if (window.__novaPostAnalyticsListenerBound) {
        return;
    }
    window.__novaPostAnalyticsListenerBound = true;

    const DIVISION_PREFIXES = ['radio-control-0-nova_post_division', 'shipping_method_0_nova_post_division'];
    const ADDRESS_PREFIXES = ['radio-control-0-nova_post_address', 'shipping_method_0_nova_post_address'];

    const matchesPrefix = (id, prefixes) => prefixes.some(p => id && id.startsWith(p));

    const reported = { division: false, address: false };

    const clickHandler = (event) => {
        const target = event.target;
        if (!target || target.tagName !== 'INPUT') {
            return;
        }
        const targetId = target.id;
        if (matchesPrefix(targetId, DIVISION_PREFIXES)) {
            analyticsService.deliveryMethodSelected('division');
            reported.division = true;
        }
        if (matchesPrefix(targetId, ADDRESS_PREFIXES)) {
            analyticsService.deliveryMethodSelected('address');
            reported.address = true;
        }
    };
    document.addEventListener('click', clickHandler);

    const checkAndReport = () => {
        const divisionEl = Array.from(
            document.querySelectorAll(
                DIVISION_PREFIXES.map(p => `[id^="${p}"]`).join(',')
            )
        ).find(el => el && el.tagName === 'INPUT' && el.checked);
        if (divisionEl && !reported.division) {
            analyticsService.deliveryMethodSelected('division');
            reported.division = true;
        }

        const addressEl = Array.from(
            document.querySelectorAll(
                ADDRESS_PREFIXES.map(p => `[id^="${p}"]`).join(',')
            )
        ).find(el => el && el.tagName === 'INPUT' && el.checked);
        if (addressEl && !reported.address) {
            analyticsService.deliveryMethodSelected('address');
            reported.address = true;
        }
    };

    checkAndReport();

    const observer = new MutationObserver(() => {
        checkAndReport();
        if (reported.division && reported.address) {
            observer.disconnect();
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });
};
