<?php
/**
 * AdminMediaHook class.
 *
 * Loads WordPress Media Library on PowerWidget admin pages.
 *
 * @package Ilabs\Firevert\Modules\PowerWidget\Hooks\Admin
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\PowerWidget\Hooks\Admin;

use Ilabs\Firevert\Core\Services\Resolver\PageContextResolver;
use Ilabs\Firevert\Modules\PowerWidget\Services\Type\ModuleType;

/**
 * Handles loading of WordPress Media Library for PowerWidget.
 */
class AdminMediaHook {

	/**
	 * Registers the hook for loading media library.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'admin_enqueue_scripts', array( $this, 'maybe_enqueue_media' ) );
	}

	/**
	 * Conditionally loads WordPress Media Library.
	 *
	 * @return void
	 */
	public function maybe_enqueue_media(): void {
		$page_context = new PageContextResolver();

		if ( ! $page_context->is_firevert_settings_page() ) {
			return;
		}

		if ( 'modules' !== $page_context->get_tab() ) {
			return;
		}

		if ( ModuleType::MODULE_SLUG !== $page_context->get_module() ) {
			return;
		}

		wp_enqueue_media();
	}
}
