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

namespace ScheduledChanges\Admin;

use ScheduledChanges\Extension\BundledModuleIds;

/** Maps active module action hooks to neutral shell callback methods. */
final class AdminActionRegistry {
	/** @var array<string,string> */
	private array $actions;

	/**
	 * @param array<string,string> $actions Hook-to-method registrations.
	 */
	public function __construct( array $actions ) {
		$this->actions = $actions;
	}

	/**
	 * @param string[] $module_ids Selected module IDs.
	 */
	public static function forModules( array $module_ids ): self {
		$selected = static fn ( string $id ): bool => in_array( $id, $module_ids, true );
		$actions  = array();
		if ( $selected( BundledModuleIds::ADMIN_RECORDING ) ) {
			$actions += array(
				'admin_post_scheduled_changes_cancel_recording'   => 'cancelRecording',
				'admin_post_scheduled_changes_start_dashboard_recording' => 'startDashboardRecording',
			);
		}

		/** Filters action registrations contributed by active modules. */
		$filtered = apply_filters( 'scheduled_changes_admin_actions', $actions, $module_ids );

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

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