<?php
/**
 * Gutenberg block inserter branding for the RSM suite.
 *
 * @package Rsmspark_Suite
 */

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Rsmspark_Block_Editor_Branding', false ) ) :

/**
 * Rsmspark_Block_Editor_Branding
 */
final class Rsmspark_Block_Editor_Branding {

	/**
	 * Block inserter category slug.
	 */
	public const BLOCK_CATEGORY = 'rsm-spark';

	/**
	 * RSMSpark orange (matches Rsmspark_Panels_Branding).
	 */
	public const BRAND_ORANGE = '#FF6B35';

	/**
	 * Script handle.
	 */
	public const SCRIPT_HANDLE = 'rsmspark-block-editor-branding';

	/**
	 * Style handle.
	 */
	public const STYLE_HANDLE = 'rsmspark-block-editor-branding';

	/**
	 * Shared block preview / canvas layout script.
	 */
	public const PREVIEW_SCRIPT_HANDLE = 'rsmspark-block-preview';

	/**
	 * Shared block editor shell styles (preview button, embed pickers).
	 */
	public const SHELL_STYLE_HANDLE = 'rsmspark-block-editor-shell';

	/**
	 * @var bool
	 */
	private static $hooks_registered = false;

	/**
	 * rsm-shared directory for the plugin copy that loaded this class.
	 *
	 * @return string
	 */
	private static function bundle_dir() {
		return trailingslashit( dirname( __DIR__ ) );
	}

	/**
	 * Public URL for assets in this plugin's rsm-shared bundle (not global RSMSPARK_SUITE_URL).
	 *
	 * @param string $relative_path Path under rsm-shared/.
	 * @return string
	 */
	private static function bundle_url( $relative_path ) {
		return trailingslashit(
			plugins_url( '/', self::bundle_dir() . 'bootstrap.php' )
		) . ltrim( (string) $relative_path, '/' );
	}

	/**
	 * @return void
	 */
	public static function init() {
		if ( self::$hooks_registered ) {
			return;
		}

		$script_path = self::bundle_dir() . 'assets/js/block-editor-branding.js';
		if ( ! is_readable( $script_path ) ) {
			return;
		}

		self::$hooks_registered = true;

		add_action( 'init', array( __CLASS__, 'register_assets' ), 5 );
		add_filter( 'block_categories_all', array( __CLASS__, 'register_block_category' ), 5, 2 );
		add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_assets' ), 1 );
	}

	/**
	 * @return void
	 */
	public static function register_assets() {
		$script_path = self::bundle_dir() . 'assets/js/block-editor-branding.js';
		if ( is_readable( $script_path ) ) {
			wp_register_script(
				self::SCRIPT_HANDLE,
				self::bundle_url( 'assets/js/block-editor-branding.js' ),
				array( 'wp-blocks', 'wp-element', 'wp-hooks', 'wp-data', 'wp-i18n' ),
				(string) filemtime( $script_path ),
				true
			);
		}

		$style_path = self::bundle_dir() . 'assets/css/block-editor-branding.css';
		if ( is_readable( $style_path ) ) {
			wp_register_style(
				self::STYLE_HANDLE,
				self::bundle_url( 'assets/css/block-editor-branding.css' ),
				array( 'dashicons' ),
				(string) filemtime( $style_path )
			);
		}

		$preview_script = self::bundle_dir() . 'assets/js/rsm-block-preview.js';
		if ( is_readable( $preview_script ) ) {
			$preview_deps = array(
				'wp-blocks',
				'wp-element',
				'wp-components',
				'wp-block-editor',
				'wp-data',
				'wp-i18n',
				'wp-server-side-render',
				self::SCRIPT_HANDLE,
			);
			$preview_ver = (string) filemtime( $preview_script );
			$preview_url = self::bundle_url( 'assets/js/rsm-block-preview.js' );

			wp_register_script(
				self::PREVIEW_SCRIPT_HANDLE,
				$preview_url,
				$preview_deps,
				$preview_ver,
				true
			);

			// Legacy handle used by Careers/Calendar/etc. before the suite prefix hard-cut.
			if ( ! wp_script_is( 'rsm-block-preview', 'registered' ) ) {
				wp_register_script(
					'rsm-block-preview',
					$preview_url,
					$preview_deps,
					$preview_ver,
					true
				);
			}
		}

		$shell_style = self::bundle_dir() . 'assets/css/rsm-block-editor-shell.css';
		if ( is_readable( $shell_style ) ) {
			$shell_ver = (string) filemtime( $shell_style );
			$shell_url = self::bundle_url( 'assets/css/rsm-block-editor-shell.css' );

			wp_register_style(
				self::SHELL_STYLE_HANDLE,
				$shell_url,
				array(),
				$shell_ver
			);

			if ( ! wp_style_is( 'rsm-block-editor-shell', 'registered' ) ) {
				wp_register_style(
					'rsm-block-editor-shell',
					$shell_url,
					array(),
					$shell_ver
				);
			}
		}
	}

	/**
	 * @return void
	 */
	public static function register_script() {
		self::register_assets();
	}

	/**
	 * Ensure the suite category exists when RSM Spark is not active.
	 *
	 * @param array<int, array<string, mixed>> $categories Block categories.
	 * @param \WP_Block_Editor_Context          $context    Editor context.
	 * @return array<int, array<string, mixed>>
	 */
	public static function register_block_category( $categories, $context ) {
		unset( $context );

		$slug = self::BLOCK_CATEGORY;
		foreach ( $categories as $category ) {
			if ( is_array( $category ) && ( $category['slug'] ?? '' ) === $slug ) {
				return $categories;
			}
		}

		array_unshift(
			$categories,
			array(
				'slug'  => $slug,
				'title' => __( 'RSM Spark', 'rsm-content-widgets' ),
				'icon'  => null,
			)
		);

		return $categories;
	}

	/**
	 * @return void
	 */
	public static function enqueue_assets() {
		if ( ! wp_script_is( self::SCRIPT_HANDLE, 'registered' ) ) {
			self::register_assets();
		}

		wp_enqueue_style( 'dashicons' );

		if ( wp_style_is( self::STYLE_HANDLE, 'registered' ) ) {
			wp_enqueue_style( self::STYLE_HANDLE );
		}

		if ( wp_style_is( self::SHELL_STYLE_HANDLE, 'registered' ) ) {
			wp_enqueue_style( self::SHELL_STYLE_HANDLE );
		} elseif ( wp_style_is( 'rsm-block-editor-shell', 'registered' ) ) {
			wp_enqueue_style( 'rsm-block-editor-shell' );
		}

		if ( wp_script_is( self::SCRIPT_HANDLE, 'registered' ) ) {
			wp_enqueue_script( self::SCRIPT_HANDLE );
		}

		if ( wp_script_is( self::PREVIEW_SCRIPT_HANDLE, 'registered' ) ) {
			wp_enqueue_script( self::PREVIEW_SCRIPT_HANDLE );
		} elseif ( wp_script_is( 'rsm-block-preview', 'registered' ) ) {
			wp_enqueue_script( 'rsm-block-preview' );
		}
	}

	/**
	 * @return void
	 */
	public static function enqueue_script() {
		self::enqueue_assets();
	}

	/**
	 * Enqueue shared preview chrome plus a plugin block script/styles.
	 *
	 * Ensures rsm-block-preview.js is available even when blocks parse as
	 * core/missing before per-block auto-enqueue runs.
	 *
	 * @param string              $script_handle Block editor script handle.
	 * @param array<int, string>  $style_handles Optional editor style handles.
	 * @return void
	 */
	public static function enqueue_block_editor_stack( $script_handle = '', $style_handles = array() ) {
		self::enqueue_assets();

		if ( $script_handle && wp_script_is( $script_handle, 'registered' ) ) {
			wp_enqueue_script( $script_handle );
		}

		foreach ( (array) $style_handles as $handle ) {
			if ( $handle && wp_style_is( $handle, 'registered' ) ) {
				wp_enqueue_style( $handle );
			}
		}
	}
}

endif;
