(function () {
    'use strict';

    const $ = (sel, root = document) => root.querySelector(sel);
    const $$ = (sel, root = document) => Array.from(root.querySelectorAll(sel));

    function bindErrorsFilter() {
        const search = $('#errors-search');
        if (!search) return;
        const items = $$('#errorsAccordion .accordion-item');

        const apply = () => {
            const q = search.value.toLowerCase().trim();
            items.forEach(it => {
                const text = it.textContent.toLowerCase();
                it.style.display = q && !text.includes(q) ? 'none' : '';
            });
        };

        search.addEventListener('input', apply);
        apply();
    }

    function bindEndpointsFilter() {
        const tbody = $('#endpoints-tbody');
        if (!tbody) return;

        const input = $('#endpoints-search');
        const empty = $('#endpoints-empty');

        const applyFilter = () => {
            const q = (input?.value || '').toLowerCase().trim();
            let visible = 0;

            $$('#endpoints-tbody tr').forEach(tr => {
                const txt = tr.textContent.toLowerCase();
                const show = !q || txt.includes(q);
                tr.style.display = show ? '' : 'none';
                if (show) visible++;
            });

            empty?.classList.toggle('d-none', visible !== 0);
        };

        input?.addEventListener('input', applyFilter);
        applyFilter();
    }

    function init() {
        bindErrorsFilter();
        bindEndpointsFilter();
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
