<?php
/**
 * RSM Media picker integration for Content Widgets admin surfaces.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Media_Picker
 */
final class Rsmspark_Content_Widgets_Media_Picker {

	/**
	 * @return bool
	 */
	public static function should_use() {
		if ( ! class_exists( 'Rsmspark_Content_Widgets_Settings', false ) ) {
			return false;
		}

		if ( ! Rsmspark_Content_Widgets_Settings::editor_use_rsm_media_picker() ) {
			return false;
		}

		return class_exists( 'RSM_Media_Picker', false ) && RSM_Media_Picker::is_enabled();
	}

	/**
	 * @return void
	 */
	public static function register_script() {
		if ( wp_script_is( 'rsmspark-cw-media-picker', 'registered' ) ) {
			return;
		}

		wp_register_script(
			'rsmspark-cw-media-picker',
			RSMSPARK_CONTENT_WIDGETS_URL . 'assets/js/rsm-cw-media-picker.js',
			array( 'jquery', 'rsm-media-picker-bridge' ),
			RSMSPARK_CONTENT_WIDGETS_VERSION,
			true
		);
	}

	/**
	 * @param string $hook Admin hook suffix.
	 * @return void
	 */
	public static function enqueue_admin( $hook = '' ) {
		if ( ! is_admin() || ! current_user_can( 'upload_files' ) ) {
			return;
		}

		if ( class_exists( 'RSM_Media_Picker', false ) ) {
			RSM_Media_Picker::enqueue( $hook ? $hook : 'post.php' );
		}

		self::register_script();

		wp_enqueue_script( 'rsmspark-cw-media-picker' );
		wp_localize_script(
			'rsmspark-cw-media-picker',
			'rsmsparkCwMediaPickerConfig',
			array(
				'useRsmMedia' => self::should_use(),
				'i18n'        => array(
					'selectImage' => __( 'Select image', 'rsm-content-widgets' ),
					'replaceImage' => __( 'Replace image', 'rsm-content-widgets' ),
					'removeImage'  => __( 'Remove image', 'rsm-content-widgets' ),
					'setImage'     => __( 'Set image', 'rsm-content-widgets' ),
				),
			)
		);
	}

	/**
	 * @param string $hook Admin hook suffix.
	 * @return bool
	 */
	public static function is_siteorigin_admin_context( $hook ) {
		$hook = (string) $hook;
		if ( in_array( $hook, array( 'post.php', 'post-new.php', 'widgets.php', 'site-editor.php' ), true ) ) {
			return true;
		}

		if (
			'' !== $hook
			&& (
				false !== strpos( $hook, 'siteorigin' )
				|| false !== strpos( $hook, 'page-builder' )
			)
		) {
			return true;
		}

		return false;
	}

	/**
	 * @return void
	 */
	public static function register() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_siteorigin' ), 25 );
		add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor' ), 15 );
		add_action( 'admin_init', array( __CLASS__, 'register_siteorigin_widget_hooks' ), 20 );
	}

	/**
	 * Ensure picker scripts load in the block editor even when block UI is split across frames.
	 *
	 * @return void
	 */
	public static function enqueue_block_editor() {
		if ( ! is_admin() || ! self::should_use() ) {
			return;
		}

		self::enqueue_admin( 'post.php' );
	}

	/**
	 * Enqueue picker when any RSM Content Widgets SO form loads (including AJAX).
	 *
	 * @return void
	 */
	public static function register_siteorigin_widget_hooks() {
		if ( ! self::should_use() ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_SiteOrigin', false ) || ! Rsmspark_Content_Widgets_SiteOrigin::is_available() ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_Registry', false ) ) {
			return;
		}

		foreach ( array_keys( Rsmspark_Content_Widgets_Registry::definitions() ) as $registry_id ) {
			$so_id = Rsmspark_Content_Widgets_Registry::siteorigin_folder_id( $registry_id );
			add_action( 'siteorigin_widgets_enqueue_admin_scripts_' . $so_id, array( __CLASS__, 'enqueue_admin_for_so_widget' ) );
		}

		// Legacy SiteOrigin image widget id before rsm-image rename.
		add_action( 'siteorigin_widgets_enqueue_admin_scripts_image', array( __CLASS__, 'enqueue_admin_for_so_widget' ) );
	}

	/**
	 * @return void
	 */
	public static function enqueue_admin_for_so_widget() {
		self::enqueue_admin( 'post.php' );
	}

	/**
	 * @param string $hook Admin hook suffix.
	 * @return void
	 */
	public static function maybe_enqueue_siteorigin( $hook ) {
		if ( ! self::should_use() ) {
			return;
		}

		if ( ! class_exists( 'Rsmspark_Content_Widgets_SiteOrigin', false ) || ! Rsmspark_Content_Widgets_SiteOrigin::is_available() ) {
			return;
		}

		if ( ! self::is_siteorigin_admin_context( $hook ) ) {
			return;
		}

		self::enqueue_admin( $hook );
	}
}
