<?php
/**
 * Canonical module category tab order (Modules admin).
 *
 * @package WP_PowerSuite\Core
 */

namespace WP_PowerSuite\Core;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class ModuleCategories {

	/**
	 * Category tabs after All and Favorites.
	 *
	 * @var list<string>
	 */
	public const ORDER = [
		'Security',
		'Login & Users',
		'Admin',
		'Content',
		'Media',
		'SEO & Visibility',
		'Speed',
		'Site Tools',
	];

	/**
	 * Sort discovered categories: known slugs first (ORDER), then unknown A–Z.
	 *
	 * @param list<string> $discovered
	 * @return list<string>
	 */
	public static function sort( array $discovered ): array {
		$discovered = array_values( array_unique( array_filter( array_map( 'strval', $discovered ) ) ) );
		$sorted     = [];
		foreach ( self::ORDER as $slug ) {
			if ( in_array( $slug, $discovered, true ) ) {
				$sorted[] = $slug;
			}
		}
		$unknown = [];
		foreach ( $discovered as $category ) {
			if ( ! in_array( $category, self::ORDER, true ) ) {
				$unknown[] = $category;
			}
		}
		sort( $unknown );
		return array_merge( $sorted, $unknown );
	}
}
