<?php

namespace SugarCalendarBookings\Support\Ajax;

use SugarCalendarBookings\Plugin;
use SugarCalendarBookings\Support\Bootable;
use SugarCalendarBookings\Support\RequestType;

/**
 * AJAX routes.
 *
 * Class for registering AJAX routes.
 *
 * @since 1.0.0
 */
abstract class AbstractRoutes implements Bootable {

	/**
	 * The route registry.
	 *
	 * @since 1.0.0
	 *
	 * @var RouteRegistry
	 */
	protected $route_registry;

	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @param RouteRegistry $routes_registry The route registry.
	 */
	public function __construct( $routes_registry ) {

		$this->route_registry = $routes_registry;
	}

	/**
	 * Initialize the routes.
	 *
	 * @since 1.0.0
	 */
	public function init() {

		$this->register();
	}

	/**
	 * Initialize only on particular AJAX request.
	 *
	 * @since 1.0.0
	 *
	 * @return bool
	 */
	public function allow_load() {

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return Plugin::get( RequestType::class )->is_ajax() && isset( $_GET['action'] ) && $_GET['action'] === 'sugar_calendar_bookings';
	}

	/**
	 * Register a route helper.
	 *
	 * @since 1.0.0
	 *
	 * @param string                      $task     The task.
	 * @param array{0: object, 1: string} $callback The callback.
	 *
	 * @return Route
	 */
	protected function route( $task, $callback ) {

		return $this->route_registry->route( $task, $callback );
	}

	/**
	 * Register the routes.
	 *
	 * @since 1.0.0
	 */
	abstract protected function register();
}
