<?php
/**
 * Admin asset registry.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Admin;

use ScheduledChanges\Extension\BundledModuleIds;

/** Provides a declarative extension point for shell assets. */
final class AdminAssetRegistry {
	/** @var array<string,array<string,mixed>> */
	private array $assets;

	/**
	 * @param array<string,array<string,mixed>> $assets Asset definitions.
	 */
	public function __construct( array $assets ) {
		$this->assets = $assets;
	}

	/**
	 * @param string[] $module_ids Selected module IDs.
	 */
	public static function forModules( array $module_ids ): self {
		$assets = array();
		if ( in_array( BundledModuleIds::ADMIN_SHELL, $module_ids, true ) ) {
			$base_url = defined( 'SCHEDULED_CHANGES_PLUGIN_URL' ) ? (string) SCHEDULED_CHANGES_PLUGIN_URL : '';
			$assets   = array(
				'scheduled-changes-admin-style' => array(
					'type' => 'style',
					'src'  => $base_url . 'assets/build/admin.css',
				),
				'scheduled-changes-admin'       => array(
					'type' => 'script',
					'src'  => $base_url . 'assets/build/admin.js',
				),
			);
		}

		$filtered = apply_filters( 'scheduled_changes_admin_assets', $assets, $module_ids );

		return new self( is_array( $filtered ) ? $filtered : $assets );
	}

	/** @return array<string,array<string,mixed>> */
	public function all(): array {
		return $this->assets;
	}
}
