/**
 * @file assets/js/adminjs/mbs-admin-filter.js
 * Live search filter and A-Z collapse/expand for the Authors, Series, Collections, and Universes settings tabs.
 * @package MyBookShowroom
 * @since   1.0.0
 */

( function () {
	'use strict';

	document.addEventListener( 'DOMContentLoaded', function () {

		// Support both Authors and Series tabs on the same page load.
		var roots = document.querySelectorAll( '.mbs-tab-authors, .mbs-tab-series, .mbs-tab-collections, .mbs-tab-universes' );

		Array.prototype.forEach.call( roots, function ( root ) {
			initTab( root );
		} );
	} );

	/**
	 * Wire up filter + collapse/expand for one tab root element.
	 *
	 * @param {Element} root  The .mbs-tab-authors or .mbs-tab-series wrapper.
	 */
	function initTab( root ) {
		var input       = root.querySelector( '.mbs-list-filter-input' );
		var groups      = Array.prototype.slice.call( root.querySelectorAll( '.mbs-letter-group' ) );
		var collapseBtn = root.querySelector( '.mbs-collapse-all' );
		var expandBtn   = root.querySelector( '.mbs-expand-all' );
		var noResults   = root.querySelector( '.mbs-filter-no-results' );

		if ( ! input || ! groups.length ) {
			return;
		}

		// ── Live filter ───────────────────────────────────────────────────────
		input.addEventListener( 'input', function () {
			var query = input.value.trim().toLowerCase();
			applyFilter( query, groups, noResults );
		} );

		// ── Collapse All ──────────────────────────────────────────────────────
		if ( collapseBtn ) {
			collapseBtn.addEventListener( 'click', function () {
				groups.forEach( function ( g ) {
					if ( ! g.hidden ) {
						g.open = false;
					}
				} );
			} );
		}

		// ── Expand All ────────────────────────────────────────────────────────
		if ( expandBtn ) {
			expandBtn.addEventListener( 'click', function () {
				groups.forEach( function ( g ) {
					if ( ! g.hidden ) {
						g.open = true;
					}
				} );
			} );
		}
	}

	/**
	 * Apply (or clear) the filter across all letter groups.
	 *
	 * @param {string}    query     Lowercased search string (empty = show all).
	 * @param {Element[]} groups    Array of .mbs-letter-group <details> elements.
	 * @param {Element}   noResults The "no results" <p> element (may be null).
	 */
	function applyFilter( query, groups, noResults ) {
		var anyVisible = false;

		groups.forEach( function ( group ) {
			var rows         = Array.prototype.slice.call( group.querySelectorAll( '[data-name]' ) );
			var visibleCount = 0;

			rows.forEach( function ( row ) {
				var name  = ( row.dataset.name || '' ).toLowerCase();
				var match = ! query || name.indexOf( query ) !== -1;
				row.hidden = ! match;
				if ( match ) {
					visibleCount++;
				}
			} );

			// Hide the whole letter group when none of its rows match.
			group.hidden = ( visibleCount === 0 );

			if ( ! group.hidden ) {
				anyVisible = true;
				// Force-open visible groups while a filter is active so
				// the user can see results without manually expanding.
				if ( query ) {
					group.open = true;
				} else {
					// Filter cleared — restore default (all open).
					group.open = true;
				}
			}
		} );

		// "No results" message
		if ( noResults ) {
			noResults.hidden = anyVisible || ! query;
		}
	}

} )();
