<?php
/**
 * RSM Editor asset loading.
 *
 * @package Rsmspark_Editor
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Rich_Text_Assets
 */
final class Rsmspark_Rich_Text_Assets {

	public const STYLE_HANDLE  = 'rsmspark-rich-text';
	public const SCRIPT_HANDLE = 'rsmspark-rich-text-tinymce';
	public const CONFIG_HANDLE = 'rsmspark-rich-text-config';
	public const ADMIN_STYLE   = 'rsmspark-rich-text-admin';
	public const PREVIEW_STYLE = 'rsmspark-rich-text-preview';

	/**
	 * @return void
	 */
	public static function init() {
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_front' ), 5 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_admin' ), 5 );
		add_action( 'rsmspark_content_widgets_enqueue_widget_assets', array( __CLASS__, 'maybe_enqueue_for_widget' ), 10, 1 );
	}

	/**
	 * @return void
	 */
	public static function register_front() {
		wp_register_style(
			self::STYLE_HANDLE,
			self::stylesheet_url(),
			array(),
			RSMSPARK_RICH_TEXT_VERSION
		);
	}

	/**
	 * @return void
	 */
	public static function register_admin() {
		self::register_front();

		// TinyMCE plugin URL only — loaded by mce_external_plugins, not wp_enqueue_script.
		wp_register_script(
			self::SCRIPT_HANDLE,
			self::script_url(),
			array(),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);

		wp_register_script(
			self::CONFIG_HANDLE,
			false,
			array(),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);

		wp_register_style(
			self::PREVIEW_STYLE,
			self::preview_stylesheet_url(),
			array( self::STYLE_HANDLE ),
			RSMSPARK_RICH_TEXT_VERSION
		);

		wp_register_style(
			self::ADMIN_STYLE,
			self::admin_stylesheet_url(),
			array(),
			RSMSPARK_RICH_TEXT_VERSION
		);
	}

	/**
	 * @return void
	 */
	public static function enqueue_front() {
		self::register_front();
		wp_enqueue_style( self::STYLE_HANDLE );
	}

	/**
	 * SiteOrigin widgets call enqueue_frontend_scripts directly — not the shortcode asset hook.
	 *
	 * @return void
	 */
	public static function enqueue_siteorigin_frontend() {
		self::enqueue_front();
	}

	/**
	 * @return void
	 */
	public static function enqueue_admin() {
		self::register_admin();
		// Token + preview styles load only inside TinyMCE via content_css — not on wp-admin body.
		wp_enqueue_style( self::ADMIN_STYLE );
		self::enqueue_config_script();

		wp_enqueue_script(
			'rsmspark-rich-text-ui',
			self::ui_script_url(),
			array( 'jquery', self::CONFIG_HANDLE ),
			RSMSPARK_RICH_TEXT_VERSION,
			true
		);
	}

	/**
	 * Classic TinyMCE stack for Gutenberg — use wp_enqueue_editor() on admin_enqueue_scripts.
	 *
	 * @deprecated 1.3.6 Use Rsmspark_Rich_Text_TinyMCE::enqueue_gutenberg_editor().
	 * @return void
	 */
	public static function enqueue_block_editor_tinymce() {
		self::enqueue_gutenberg_editor( 'post.php' );
	}

	/**
	 * TinyMCE init keys safe to merge from admin JS (SiteOrigin wp-before-tinymce-init).
	 *
	 * @param string $profile Profile key.
	 * @return array<string, mixed>
	 */
	private static function tinymce_init_for_js( $profile ) {
		$settings = Rsmspark_Rich_Text_Registry::tinymce_settings( $profile );
		$external = array(
			'rsm_rich_text' => self::script_url(),
		);

		if ( ! empty( $settings['external_plugins'] ) && is_string( $settings['external_plugins'] ) ) {
			$decoded = json_decode( $settings['external_plugins'], true );
			if ( is_array( $decoded ) ) {
				$external = array_merge( $decoded, $external );
			}
		}

		$settings['external_plugins'] = $external;

		return $settings;
	}

	/**
	 * Inline profile config for admin screens (TinyMCE init also carries rsm_rt_profiles).
	 *
	 * @return void
	 */
	public static function enqueue_config_script() {
		if ( wp_script_is( self::CONFIG_HANDLE, 'enqueued' ) ) {
			return;
		}

		wp_enqueue_script( self::CONFIG_HANDLE );

		wp_add_inline_script(
			self::CONFIG_HANDLE,
			'window.rsmRichTextEditor = ' . wp_json_encode(
				array(
					'profiles'  => Rsmspark_Rich_Text_Registry::js_profiles(),
					'toolbars'  => array(
						Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE => Rsmspark_Rich_Text_Registry::toolbar1( Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE ),
						Rsmspark_Rich_Text_Registry::PROFILE_BODY     => Rsmspark_Rich_Text_Registry::toolbar1( Rsmspark_Rich_Text_Registry::PROFILE_BODY ),
					),
					'tinymceInit' => array(
						Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE => self::tinymce_init_for_js( Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE ),
						Rsmspark_Rich_Text_Registry::PROFILE_BODY     => self::tinymce_init_for_js( Rsmspark_Rich_Text_Registry::PROFILE_BODY ),
					),
					'contentCss'  => array(
						Rsmspark_Rich_Text_Registry::PROFILE_HEADLINE => Rsmspark_Rich_Text_Assets::preview_stylesheet_url() . ',' . Rsmspark_Rich_Text_Assets::stylesheet_url(),
						Rsmspark_Rich_Text_Registry::PROFILE_BODY     => Rsmspark_Rich_Text_Assets::preview_stylesheet_url() . ',' . Rsmspark_Rich_Text_Assets::stylesheet_url(),
					),
					'standards' => Rsmspark_Rich_Text_Standards::get(),
					'editorUi'  => array(
						'mediaRow'    => class_exists( 'Rsmspark_Content_Widgets_Settings', false )
							? Rsmspark_Content_Widgets_Settings::editor_media_row_enabled()
							: false,
						'useRsmMedia' => class_exists( 'Rsmspark_Content_Widgets_Settings', false )
							? Rsmspark_Content_Widgets_Settings::editor_use_rsm_media_picker()
							: false,
					),
					'pluginUrl' => self::script_url(),
				)
			) . ';',
			'before'
		);
	}

	/**
	 * @param string $registry_id Widget registry id.
	 * @return void
	 */
	public static function maybe_enqueue_for_widget( $registry_id ) {
		$registry_id = sanitize_key( (string) $registry_id );
		$with_editor = array( 'headline', 'features', 'tabs', 'accordion' );

		if ( in_array( $registry_id, $with_editor, true ) ) {
			self::enqueue_front();
		}
	}

	/**
	 * @return string
	 */
	public static function stylesheet_url() {
		return RSMSPARK_RICH_TEXT_URL . 'assets/css/rsm-editor.css';
	}

	/**
	 * @return string
	 */
	public static function preview_stylesheet_url() {
		return RSMSPARK_RICH_TEXT_URL . 'assets/css/rsm-editor-preview.css';
	}

	/**
	 * @return string
	 */
	public static function script_url() {
		return add_query_arg(
			'ver',
			RSMSPARK_RICH_TEXT_VERSION,
			RSMSPARK_RICH_TEXT_URL . 'assets/js/rsm-editor-tinymce.js'
		);
	}

	/**
	 * @return string
	 */
	public static function media_script_url() {
		return RSMSPARK_RICH_TEXT_URL . 'assets/js/rsm-editor-media.js';
	}

	/**
	 * @return string
	 */
	public static function ui_script_url() {
		return RSMSPARK_RICH_TEXT_URL . 'assets/js/rsm-editor-ui.js';
	}

	/**
	 * @return string
	 */
	public static function admin_stylesheet_url() {
		return RSMSPARK_RICH_TEXT_URL . 'assets/css/rsm-editor-admin.css';
	}
}
