<?php

namespace PostenBringCheckout\includes;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Webhook loader class
 *
 * Class responsible for registering all action and filter callbacks
 *
 * @since 0.0.1
 */
class Loader {

	/**
	 * Actions.
	 *
	 * @since 0.0.1
	 * @var array $plugin_name Actions
	 */
	protected array $actions;
	/**
	 * Filters.
	 *
	 * @since 0.0.1
	 * @var array $plugin_name Filters
	 */
	protected array $filters;

	/**
	 * Constructor
	 *
	 * Will initialize action and filter arrays
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		$this->actions = array();
		$this->filters = array();
	}

	/**
	 * Add action callback
	 *
	 * This function will add the provided action to the action list
	 *
	 * @param string $hook Hook name.
	 * @param mixed  $component Instance where callback function is located.
	 * @param string $callback Callback function name.
	 * @param int    $priority Priority.
	 * @param int    $accepted_args Number of input args expected.
	 * @since 0.0.1
	 */
	public function add_action( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void {
		$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
	}

	/**
	 * Add filter callback
	 *
	 * This function will add the provided filter to the filter list
	 *
	 * @param string $hook Hook name.
	 * @param mixed  $component Instance where callback function is located.
	 * @param string $callback Callback function name.
	 * @param int    $priority Priority.
	 * @param int    $accepted_args Number of input args expected.
	 * @since 0.0.1
	 */
	public function add_filter( string $hook, $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void {
		$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
	}

	/**
	 * Add filter callback
	 *
	 * This function will add the provided filter to the filter list
	 *
	 * @param array  $hooks Array of all hooks.
	 * @param string $hook Hook name.
	 * @param mixed  $component Instance where callback function is located.
	 * @param string $callback Callback function name.
	 * @param int    $priority Priority.
	 * @param int    $accepted_args Number of input args expected.
	 * @return array Array of hooks
	 * @since 0.0.1
	 */
	private function add( array $hooks, string $hook, $component, string $callback, int $priority, int $accepted_args ): array {
		$hooks[] = array(
			'hook'          => $hook,
			'component'     => $component,
			'callback'      => $callback,
			'priority'      => $priority,
			'accepted_args' => $accepted_args,
		);

		return $hooks;
	}

	/**
	 * Register all action and filter callbacks
	 *
	 * This function will register all the provided action and filter callbacks
	 *
	 * @since 0.0.1
	 */
	public function run() {
		foreach ( $this->filters as $hook ) {
			add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
		}

		foreach ( $this->actions as $hook ) {
			add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] );
		}
	}
}
