(() => {
    'use strict';
    const root = document.querySelector('.h4bs-staff-home');
    if (!root) return;
    const search = root.querySelector('#h4bs-page-search');
    const rows = [...root.querySelectorAll('.h4bs-staff-home__page')];
    const buttons = [...root.querySelectorAll('[data-h4bs-page-filter]')];
    const count = root.querySelector('.h4bs-staff-home__result-count');
    const empty = root.querySelector('.h4bs-staff-home__empty');
    const pageList = root.querySelector('.h4bs-staff-home__page-list');
    const more = document.createElement('button');
    more.type = 'button'; more.className = 'button h4bs-staff-home__more'; more.textContent = 'Show more content';
    pageList?.after(more);
    let filter = 'all';
    let limit = 12;
    const update = () => {
        const query = (search?.value || '').trim().toLowerCase();
        let visible = 0;
        rows.forEach((row) => {
            const status = row.dataset.status || '';
            const matchesFilter = filter === 'all' || (filter === 'favorite' && row.dataset.favorite === '1') || status === filter;
            const matched = matchesFilter && (!query || (row.dataset.title || '').includes(query));
            const show = matched && (query || filter !== 'all' || visible < limit);
            row.hidden = !show;
            if (matched) visible += 1;
        });
        const displayed = rows.filter(row => !row.hidden).length;
        if (count) count.textContent = displayed < visible ? `Showing ${displayed} recent items of ${visible}. Search or filter to find anything else.` : `${visible} ${visible === 1 ? 'item' : 'items'} shown`;
        if (empty) empty.hidden = visible !== 0;
        more.hidden = Boolean(query || filter !== 'all' || visible <= limit);
    };
    search?.addEventListener('input', update);
    buttons.forEach((button) => button.addEventListener('click', () => {
        filter = button.dataset.h4bsPageFilter || 'all';
        buttons.forEach((item) => {
            const active = item === button;
            item.classList.toggle('is-active', active);
            item.setAttribute('aria-pressed', active ? 'true' : 'false');
        });
        update();
    }));
    more.addEventListener('click', () => { limit += 12; update(); });
    update();
})();
