<?php

namespace Yuvayana\AcadlixImporter\REST;


/**
 * API Manager class
 * 
 * All API classes would be registered here
 */
// Admin API Controller
use Yuvayana\AcadlixImporter\REST\Admin\AdminWpAdvQuizController;
use Yuvayana\AcadlixImporter\REST\Admin\AdminWpProQuizController;


defined( 'ABSPATH' ) || exit();

#[\AllowDynamicProperties] class Api {

    protected static $_instance = null;

    /**
     * Class dir and class name mapping.
     *
     * @var array
     *
     */
    protected $class_map;

    /**
     * Constructor used to register all apis.
     */
    public function __construct() {
        if ( ! class_exists( 'WP_REST_Server' ) ) {
            return;
        }

        $this->class_map = apply_filters(
            'acadlix_importer_rest_api_class_map',
            [
                // Admin Controllers
                AdminWpProQuizController::class,
                AdminWpAdvQuizController::class,
            ]
        );

        // Init REST API routes.
        add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
    }

    /**
     * Register rest API route function
     *
     * @return void
     */
    public function register_rest_routes(): void {
        foreach ( $this->class_map as $controller ) {
            if(method_exists($controller, 'register_routes')){
                $this->$controller = new $controller();
                $this->$controller->register_routes();
            }
        }
    }

    public static function instance() {
        if ( ! self::$_instance ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
}