(function () {
    'use strict';

    // Filtert den News-Feed (/news) nach Quelle (Meldung/Ergebnis/Instagram).
    // Ohne JS sind alle Einträge sichtbar (Default-Filter "all"), der
    // Empty-State bleibt verborgen — die Tabs sind reine Progressive
    // Enhancement. Gleiches Muster wie position-filter.js (/mitmachen).
    function init(bar) {
        var section = bar.closest('section');
        if (!section) { return; }

        var buttons = [].slice.call(bar.querySelectorAll('[data-filter]'));
        var entries = [].slice.call(section.querySelectorAll('[data-source]'));
        var empty   = section.querySelector('[data-news-empty]');
        if (!buttons.length || !entries.length) { return; }

        function apply(filter) {
            var visible = 0;
            entries.forEach(function (entry) {
                var match = filter === 'all' || entry.getAttribute('data-source') === filter;
                entry.hidden = !match;
                if (match) { visible++; }
            });
            if (empty) { empty.hidden = visible !== 0; }
        }

        buttons.forEach(function (btn, i) {
            btn.addEventListener('click', function () {
                buttons.forEach(function (b) { b.setAttribute('aria-pressed', 'false'); });
                btn.setAttribute('aria-pressed', 'true');
                apply(btn.getAttribute('data-filter'));
            });
            btn.addEventListener('keydown', function (e) {
                var next = null;
                if (e.key === 'ArrowRight') { next = (i + 1) % buttons.length; }
                if (e.key === 'ArrowLeft')  { next = (i - 1 + buttons.length) % buttons.length; }
                if (e.key === 'Home')       { next = 0; }
                if (e.key === 'End')        { next = buttons.length - 1; }
                if (next !== null) { e.preventDefault(); buttons[next].focus(); }
            });
        });
    }

    document.querySelectorAll('[data-news-filter]').forEach(init);
}());
