<?php
/**
 * Lazy class loader for AI Alt Text module.
 *
 * @package AIAltTextGenerator
 * @since 2.4.0
 */

namespace AIAltTextGenerator;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Defers loading integration and tab-specific classes until needed.
 *
 * @since 2.4.0
 */
class ModuleLoader {

	private const BASE = __DIR__;

	/** @var array<string, bool> */
	private static array $loaded = [];

	public static function integrations(): void {
		self::load( 'class-media-columns.php' );
		self::load( 'class-block-editor.php' );
		self::load( 'class-scheduler.php' );

		if ( class_exists( 'WooCommerce' ) ) {
			self::load( 'class-woocommerce-integration.php' );
		}
		if ( defined( 'ELEMENTOR_VERSION' ) ) {
			self::load( 'class-elementor-integration.php' );
		}
	}

	public static function tab( string $tab ): void {
		switch ( $tab ) {
			case 'review':
				self::load( 'class-review-list-table.php' );
				self::load( 'class-admin-actions.php' );
				break;
			case 'history':
				self::load( 'class-history-list-table.php' );
				self::load( 'class-admin-actions.php' );
				break;
			case 'bulk':
				self::load( 'class-bulk-generator.php' );
				break;
			case 'settings':
				self::load( 'class-provider-models.php' );
				self::load( 'class-prompt-styles.php' );
				self::load( 'class-languages.php' );
				break;
		}
	}

	public static function adminActions(): void {
		self::load( 'class-admin-actions.php' );
	}

	public static function rest(): void {
		self::load( 'class-rest-controller.php' );
	}

	public static function wpCli(): void {
		self::load( 'class-wp-cli.php' );
	}

	public static function contentScanner(): void {
		self::load( 'class-content-scanner.php' );
	}

	public static function importExport(): void {
		self::load( 'class-importer.php' );
		self::load( 'class-exporter.php' );
	}

	private static function load( string $file ): void {
		if ( isset( self::$loaded[ $file ] ) ) {
			return;
		}

		$path = self::BASE . '/' . $file;
		if ( is_readable( $path ) ) {
			require_once $path;
		}

		self::$loaded[ $file ] = true;
	}
}
