<?php
/**
 * WP-CLI command registration.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\WpCli;

/**
 * Registers JJ\'s Post Scheduler WP-CLI commands.
 */
final class CliServiceProvider {
	/** @var array<string,callable> */
	private array $commands;
	/**
	 * Creates the CLI service provider.
	 *
	 * @param array<string,callable> $commands Selected commands keyed by subcommand.
	 */
	public function __construct( array $commands ) {
		$this->commands = $commands;
	}

	/**
	 * Registers the root command when WP-CLI is active.
	 */
	public function register(): void {
		if ( ! defined( 'WP_CLI' ) || ! WP_CLI || ! class_exists( '\WP_CLI' ) ) {
			return;
		}

		\WP_CLI::add_command(
			'jjs-post-scheduler',
			new CliCommandNamespace()
		);

		foreach ( $this->commands as $name => $command ) {
			\WP_CLI::add_command( 'scheduled-changes ' . $name, $command );
		}
	}
}
