(function () {
    var selectedClass = 'nnaxfuf-row-selected';
    var mobileTooltipQuery = window.matchMedia ? window.matchMedia('(max-width: 991px)') : null;
    var i18n = (window.nnaxFmAjax && window.nnaxFmAjax.i18n) ? window.nnaxFmAjax.i18n : {};
    function t(key, fallback) {
        return (i18n && typeof i18n[key] === 'string' && i18n[key] !== '') ? i18n[key] : fallback;
    }

    function clearSelection() {
        document.querySelectorAll('.nnaxfuf-item-row.' + selectedClass).forEach(function (row) {
            row.classList.remove(selectedClass);
        });
    }

    function selectRow(row) {
        if (!row) return;
        clearSelection();
        row.classList.add(selectedClass);
        row.focus({ preventScroll: true });
    }

    function openRow(row) {
        if (!row) return;

        var type = row.dataset.nufRowType || '';
        if (type === 'folder') {
            var url = row.dataset.nufOpenUrl;
            if (url) window.location.href = url;
            return;
        }

        var detailBtn = row.querySelector('.nnaxfuf-open-detail-modal');
        if (detailBtn) {
            detailBtn.click();
            return;
        }

        var itemId = row.dataset.nufId || '';
        if (itemId) {
            var syntheticBtn = document.createElement('button');
            syntheticBtn.type = 'button';
            syntheticBtn.className = 'nnaxfuf-open-detail-modal';
            syntheticBtn.setAttribute('data-nnaxfuf-item-id', itemId);
            syntheticBtn.style.display = 'none';
            document.body.appendChild(syntheticBtn);
            syntheticBtn.click();
            syntheticBtn.remove();
            if (window.UIkit && typeof window.UIkit.modal === 'function') {
                window.UIkit.modal('#nnaxfuf-detail-modal').show();
            }
            return;
        }

        // Intentionally no download fallback for files.
        // If no detail action is available, do nothing.
    }

    function syncMobileTooltipGroups() {
        var isMobile = mobileTooltipQuery ? mobileTooltipQuery.matches : (window.innerWidth <= 991);

        document.querySelectorAll('.nnaxfuf-item-row').forEach(function (row) {
            var group = row.querySelector('[data-nnaxfuf-mobile-tooltip-group]');
            var anchor = row.querySelector('[data-nnaxfuf-mobile-tooltip-anchor]');
            var slot = row.querySelector('[data-nnaxfuf-mobile-tooltip-slot]');
            if (!group || !anchor || !slot) return;

            if (isMobile) {
                if (group.parentNode !== slot) {
                    slot.appendChild(group);
                }
                return;
            }

            if (group.parentNode !== anchor.parentNode) {
                anchor.parentNode.insertBefore(group, anchor.nextSibling);
            }
        });
    }

    document.addEventListener('click', function (e) {
        var interactive = e.target.closest('a, button, input, textarea, select, form, label, .nnaxfuf-tooltip, [uk-tooltip], [data-uk-tooltip]');
        if (interactive) return;

        var row = e.target.closest('.nnaxfuf-item-row');
        if (!row) return;

        selectRow(row);
    });

    document.addEventListener('dblclick', function (e) {
        var interactive = e.target.closest('a, button, input, textarea, select, form, label, .nnaxfuf-tooltip, [uk-tooltip], [data-uk-tooltip]');
        if (interactive) return;

        var row = e.target.closest('.nnaxfuf-item-row');
        if (!row) return;
        if ((row.dataset.nufRowType || '') === 'folder') return;

        selectRow(row);
        openRow(row);
    });

    document.addEventListener('keydown', function (e) {
        var inField = e.target && (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT');
        if (inField) return;

        var focusedRow = document.activeElement && document.activeElement.classList && document.activeElement.classList.contains('nnaxfuf-item-row')
            ? document.activeElement
            : document.querySelector('.nnaxfuf-item-row.' + selectedClass);

        if (!focusedRow) return;

        if (e.key === 'Enter') {
            e.preventDefault();
            openRow(focusedRow);
        }

        if (e.key === 'Backspace') {
            var backBtn = document.querySelector('.nnaxfuf-folder-bar .nnaxfuf-back-button');
            if (!backBtn) {
                backBtn = document.querySelector('.nnaxfuf-folder-bar a[title="' + t('back_one_level', 'Back') + '"]');
            }
            if (backBtn && backBtn.href) {
                e.preventDefault();
                window.location.href = backBtn.href;
            }
        }
    });

    document.addEventListener('click', function (e) {
        var toggleBtn = e.target.closest('.nnaxfuf-row-mobile-toggle');
        if (!toggleBtn) return;

        e.preventDefault();

        var row = toggleBtn.closest('.nnaxfuf-item-row');
        if (!row) return;

        var isOpen = row.classList.toggle('nnaxfuf-row-details-open');
        toggleBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
    });

    if (mobileTooltipQuery && typeof mobileTooltipQuery.addEventListener === 'function') {
        mobileTooltipQuery.addEventListener('change', syncMobileTooltipGroups);
    } else {
        window.addEventListener('resize', syncMobileTooltipGroups);
    }

    syncMobileTooltipGroups();
})();
