<?php
/**
 * Hook registration manager.
 *
 * Collects actions and filters, then registers them in bulk via run().
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Core;

defined( 'ABSPATH' ) || exit;
class Loader {

	/**
	 * Registered actions.
	 *
	 * @var array<int, array{hook: string, callback: callable, priority: int, accepted_args: int}>
	 */
	private array $actions = array();

	/**
	 * Registered filters.
	 *
	 * @var array<int, array{hook: string, callback: callable, priority: int, accepted_args: int}>
	 */
	private array $filters = array();

	/**
	 * Queue a WordPress action.
	 *
	 * @param string   $hook          The action hook name.
	 * @param callable $callback      The callback to invoke.
	 * @param int      $priority      Hook priority. Default 10.
	 * @param int      $accepted_args Number of arguments the callback accepts. Default 1.
	 * @return void
	 */
	public function addAction( string $hook, callable $callback, int $priority = 10, int $accepted_args = 1 ): void {
		$this->actions[] = array(
			'hook'          => $hook,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);
	}

	/**
	 * Queue a WordPress filter.
	 *
	 * @param string   $hook          The filter hook name.
	 * @param callable $callback      The callback to invoke.
	 * @param int      $priority      Hook priority. Default 10.
	 * @param int      $accepted_args Number of arguments the callback accepts. Default 1.
	 * @return void
	 */
	public function addFilter( string $hook, callable $callback, int $priority = 10, int $accepted_args = 1 ): void {
		$this->filters[] = array(
			'hook'          => $hook,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);
	}

	/**
	 * Register all queued actions and filters with WordPress.
	 *
	 * @return void
	 */
	public function run(): void {
		foreach ( $this->actions as $action ) {
			add_action(
				$action['hook'],
				$action['callback'],
				$action['priority'],
				$action['accepted_args']
			);
		}

		foreach ( $this->filters as $filter ) {
			add_filter(
				$filter['hook'],
				$filter['callback'],
				$filter['priority'],
				$filter['accepted_args']
			);
		}
	}
}
