(function () {
    'use strict';

    function activateTopic(guide, topicId) {
        guide.querySelectorAll('[data-guide-topic]').forEach(function (button) {
            var active = button.dataset.guideTopic === topicId;
            button.classList.toggle('active', active);
            button.setAttribute('aria-selected', active ? 'true' : 'false');
        });
        guide.querySelectorAll('[data-guide-panel]').forEach(function (panel) {
            panel.classList.toggle('active', panel.dataset.guidePanel === topicId);
        });
    }

    function filterGuide(guide, query) {
        var normalized = query.trim().toLowerCase();
        var matches = [];
        guide.querySelectorAll('[data-guide-panel]').forEach(function (panel) {
            var searchable = (panel.dataset.guideSearch || '') + ' ' + panel.textContent;
            var match = !normalized || searchable.toLowerCase().includes(normalized);
            var button = guide.querySelector('[data-guide-topic="' + panel.dataset.guidePanel + '"]');
            if (button) button.hidden = !match;
            if (match) matches.push(panel.dataset.guidePanel);
        });

        var empty = guide.querySelector('[data-guide-empty]');
        if (empty) empty.classList.toggle('show', matches.length === 0);
        if (matches.length && normalized) activateTopic(guide, matches[0]);
        if (!normalized) {
            var defaultTopic = guide.dataset.guideDefault || matches[0];
            activateTopic(guide, defaultTopic);
        }
    }

    function initGuide(guide) {
        if (guide.dataset.guideReady === 'true') return;
        guide.dataset.guideReady = 'true';

        var search = guide.querySelector('[data-guide-search]');
        guide.addEventListener('click', function (event) {
            var button = event.target.closest('[data-guide-topic]');
            if (!button || button.hidden) return;
            activateTopic(guide, button.dataset.guideTopic);
        });

        if (search) {
            search.addEventListener('input', function () {
                filterGuide(guide, search.value);
            });
        }

        guide.addEventListener('shown.bs.modal', function () {
            if (search) search.focus();
        });
        guide.addEventListener('hidden.bs.modal', function () {
            if (search) search.value = '';
            filterGuide(guide, '');
        });

        filterGuide(guide, '');
    }

    document.addEventListener('DOMContentLoaded', function () {
        document.querySelectorAll('[data-module-guide]').forEach(initGuide);
    });

    document.addEventListener('keydown', function (event) {
        if (event.key !== '/' || event.metaKey || event.ctrlKey || event.altKey) return;
        var openGuide = document.querySelector('.pp-module-guide.show [data-guide-search]');
        if (!openGuide) return;
        var tag = document.activeElement && document.activeElement.tagName;
        if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT') return;
        event.preventDefault();
        openGuide.focus();
    });
}());
