document.addEventListener('DOMContentLoaded', function () {
    const live = document.getElementById('fixture_iframe');
    const loader = document.getElementById('fake_iframeFixture');
    const fakeIframe = document.getElementById('appHome');

    if (live) {
        const observer = new MutationObserver(function (mutationsList) {
            for (const mutation of mutationsList) {
                for (const node of mutation.addedNodes) {
                    if (node.nodeType === 1) {
                        // Kiểm tra nếu node hoặc con cháu của node có class 'league-item'
                        if (
                            node.classList.contains('load-more-btn') ||
                            node.querySelector?.('.load-more-btn') ||
                            node.querySelector?.('#tr_upper-live-titlez') ||
                            node.querySelector?.('.table-no-tioem')
                        ) {
                            if (loader) {
                                live.style.opacity = 1;
                                setTimeout(() => {
                                    fakeIframe.classList.add('hide-fake');
                                    loader.style.transition = 'opacity 0.5s ease';
                                    loader.style.opacity = 0;
                                    loader.style.display = 'none';
                                    // loader.remove();
                                }, 1000);
                            }
                            observer.disconnect(); // Ngừng theo dõi sau khi tìm thấy
                            return;
                        }
                    }
                }
            }
        });

        observer.observe(live, {
            childList: true,
            subtree: true // nếu div nằm sâu bên trong
        });
    }

    document.addEventListener('click', function(e) {
        const icon = e.target.closest('.Detail-icon');
        const homeTeamLink = e.target.closest('td.name1 a');
        const guestTeamLink = e.target.closest('td.name2 a');

        if (!icon && !homeTeamLink && !guestTeamLink) return;

        let clickedElement;
        let fullId; 
        let matchId;

        if (icon) {
            clickedElement = icon;
            fullId = icon.id;
            const parts = fullId.split('_');
            if (parts.length < 3) return;
            matchId = parts[2];
        } else if (homeTeamLink || guestTeamLink) {

            clickedElement = homeTeamLink || guestTeamLink;

            let teamContainer = clickedElement.closest('[id^="ht_"], [id^="gt_"]');

            if (!teamContainer) {
                const row = clickedElement.closest('tr');
                if (row) {
                    teamContainer = row.querySelector('[id^="ht_"], [id^="gt_"]');
                }
            }

            if (!teamContainer) return;
            matchId = teamContainer.id.split('_').pop();
        } else {
            return;
        }

        if (!matchId) return;

        const homeTeamElem = document.querySelector(`#ht_${matchId} .homeTeam`);
        const guestTeamElem = document.querySelector(`#gt_${matchId} .guestTeam`);

        const homeTeam = homeTeamElem ? homeTeamElem.textContent.trim() : '';
        const guestTeam = guestTeamElem ? guestTeamElem.textContent.trim() : '';

        if (window.match_detail_url && !window.Checkbox_hide_action) {
            let slugDt = `${toThaiSlug(homeTeam)}-${toThaiSlug(guestTeam)}-${matchId}`;
            window.open(window.match_detail_url + "/" + slugDt);
        }
    });

    let leagueIdsFilter = window.league_ids && typeof window.league_ids === typeof [] && window.league_ids.length > 0
    let countryIdsFilter = window.nation_ids && typeof window.nation_ids === typeof [] && window.nation_ids.length > 0
    if (leagueIdsFilter || countryIdsFilter) {
        const selectLeague = document.getElementById("selectLeague");
        const el = document.querySelector("#scrollIframe .all-hot-filter > .button-df:first-child");
        if (el) {
            el.remove();
        }

        if (selectLeague && selectLeague.parentNode) {
            selectLeague.parentNode.remove(); // ❗Xóa cha 1 cấp
        }
    }
});
