// Ensure the Analytics ID is available and gtag is initialized
function istkr_sendComponentEvent(component) {
    if (!istkr_backend_data || !istkr_backend_data.anatylics_id) {
        console.error("Analytics ID is missing.");
        return;
    }
    
    // Push events to Google Analytics
    window.dataLayer = window.dataLayer || [];
    gtag('js', new Date());
    gtag('config', istkr_backend_data.anatylics_id);
    gtag('event', 'page_open', {
        event_category: 'Components events',
        event_label: `Opened ${component}`,
    });
}

// Main logic for handling page-specific events
(function () {
    import("./init.js").then(() => {
        try {
            const page = new URL(location.href).searchParams.get('page');
            switch (page) {
                case 'issues-tracker-dashboard':
                    import("./components/dashboard.js");
                    istkr_sendComponentEvent('dashboard');
                    break;
                case 'issues-tracker-log-viewer':
                    import("./components/log-view.js");
                    istkr_sendComponentEvent('log view');
                    break;
                case 'issues-tracker-advisor':
                    import("./components/advisor.js");
                    istkr_sendComponentEvent('advisor');
                    break;
                case 'issues-tracker-404':
                    import("./components/404.js");
                    istkr_sendComponentEvent('404');
                    break;
                case 'issues-tracker-server-info':
                    import("./components/server-info.js");
                    istkr_sendComponentEvent('server-info');
                    break;
                default:
                    console.warn("No matching page for analytics event.");
            }
        } catch (error) {
            console.error("Error in page-specific event handling:", error);
        }
    });

    // Use delegated event binding for dynamically loaded buttons
    jQuery(document).on('click', 'button[data-bs-target="#isrkr_updateMessage"]', () => {
        istkr_sendComponentEvent('migration_banner');
    });
})();
