<?php
/**
 * Simple class autoloader for RC_* classes.
 *
 * @package RestaurantCosting
 */

declare(strict_types=1);

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

/**
 * Autoload RC_* classes from includes/ and admin/.
 */
final class RC_Autoloader {

	/**
	 * Register spl autoload.
	 */
	public static function register(): void {
		spl_autoload_register( array( self::class, 'load' ) );
	}

	/**
	 * Load class file.
	 *
	 * @param string $class Class name.
	 */
	public static function load( string $class ): void {
		if ( ! str_starts_with( $class, 'RC_' ) ) {
			return;
		}

		$relative = 'class-' . strtolower( str_replace( '_', '-', $class ) ) . '.php';
		$paths    = array(
			RC_PLUGIN_DIR . 'includes/' . $relative,
			RC_PLUGIN_DIR . 'admin/' . $relative,
		);

		foreach ( $paths as $path ) {
			if ( is_readable( $path ) ) {
				require_once $path;
				return;
			}
		}
	}
}
