<?php
/**
 * Block registry — instantiates and registers all plugin blocks.
 *
 * Single entry point so `Plugin::registerBlocks()` only needs to call
 * `( new BlockRegistry() )->register();`. Order is the inserter's display
 * order — group the most useful blocks first.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Blocks;

defined( 'ABSPATH' ) || exit;
/**
 * Holds the canonical list of plugin blocks for v1.
 */
final class BlockRegistry {

	/**
	 * FQNs of every block class to instantiate. Order = inserter order.
	 *
	 * @return list<class-string<AbstractBlock>>
	 */
	private static function blocks(): array {
		return array(
			SingleListingBlock::class,
			SearchFormBlock::class,
			VehicleGridBlock::class,
			VehicleCardBlock::class,
			MediaSliderBlock::class,
			SpecsHighlightsBlock::class,
			SpecsTableBlock::class,
			SimilarVehiclesBlock::class,
			RecentlyViewedBlock::class,
			ContactFormBlock::class,
			DealerInfoBlock::class,
			CompareLinkBlock::class,
			ComparisonTableBlock::class,
			FavoritesListBlock::class,
			FeaturedCarouselBlock::class,
			MapViewBlock::class,
		);
	}

	/**
	 * Register every block + the plugin's block category.
	 */
	public function register(): void {
		add_filter( 'block_categories_all', array( $this, 'registerCategory' ), 10, 1 );

		foreach ( self::blocks() as $fqn ) {
			( new $fqn() )->register();
		}
	}

	/**
	 * Inject the "Auto Dealer" category at the top of the inserter.
	 *
	 * @param array<int, array<string, string>> $categories
	 * @return array<int, array<string, string>>
	 */
	public function registerCategory( array $categories ): array {
		array_unshift(
			$categories,
			array(
				'slug'  => 'trx-auto-dealer',
				'title' => __( 'Auto Dealer', 'trx-auto-dealer' ),
				'icon'  => 'car',
			)
		);
		return $categories;
	}
}
