<?php
/**
 * SVG Icon block — registered with Content Widgets; render/assets from RSM Focus (RSF).
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Widget_Svg_Icon
 */
class Rsmspark_Content_Widgets_Widget_Svg_Icon {

	const REGISTRY_ID = 'svg_icon';

	const STYLE_HANDLE = 'rsmspark-rsf-svg-icon';

	const EDITOR_STYLE_HANDLE = 'rsmspark-rsf-svg-icon-editor';

	const EDITOR_SCRIPT_HANDLE = 'rsmspark-rsf-svg-icon-block';

	/**
	 * @return void
	 */
	public static function register() {
		if ( ! class_exists( 'RSMSPARK_RSF_Svg_Icon', false ) ) {
			return;
		}

		add_action( 'init', array( __CLASS__, 'register_assets' ), 8 );

		Rsmspark_Content_Widgets_Blocks::add(
			'svg-icon',
			self::block_registration_args()
		);
	}

	/**
	 * Register front/editor assets from the Focus bundle (idempotent).
	 *
	 * @return void
	 */
	public static function register_assets() {
		if ( class_exists( 'RSMSPARK_RSF_Svg_Icon_Block', false ) && method_exists( 'RSMSPARK_RSF_Svg_Icon_Block', 'register_assets' ) ) {
			RSMSPARK_RSF_Svg_Icon_Block::register_assets();
			return;
		}

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

		if ( ! defined( 'RSMSPARK_FOCUS_URL' ) || ! defined( 'RSMSPARK_FOCUS_VERSION' ) ) {
			return;
		}

		wp_register_style(
			self::STYLE_HANDLE,
			RSMSPARK_FOCUS_URL . 'assets/css/rsf-svg-icon.css',
			array(),
			RSMSPARK_FOCUS_VERSION
		);

		wp_register_style(
			self::EDITOR_STYLE_HANDLE,
			RSMSPARK_FOCUS_URL . 'assets/css/rsf-svg-icon-editor.css',
			array( 'wp-edit-blocks' ),
			RSMSPARK_FOCUS_VERSION
		);

		$script_deps = array(
			'wp-blocks',
			'wp-element',
			'wp-components',
			'wp-block-editor',
			'wp-i18n',
			'wp-server-side-render',
		);

		if ( wp_script_is( 'rsmspark-block-editor-branding', 'registered' ) ) {
			array_unshift( $script_deps, 'rsmspark-block-editor-branding' );
		}

		if ( wp_script_is( 'rsmspark-content-widgets-blocks-editor', 'registered' ) ) {
			$script_deps[] = 'rsmspark-content-widgets-blocks-editor';
		}

		wp_register_script(
			self::EDITOR_SCRIPT_HANDLE,
			RSMSPARK_FOCUS_URL . 'assets/js/svg-icon-block.js',
			$script_deps,
			RSMSPARK_FOCUS_VERSION,
			true
		);
	}

	/**
	 * @return array<string, mixed>
	 */
	public static function block_registration_args() {
		return array(
			'api_version'     => 3,
			'title'           => __( 'RSM SVG Icon', 'rsm-content-widgets' ),
			'description'     => __( 'Upload or paste a sanitized SVG icon with size, color, hover, link, and alignment.', 'rsm-content-widgets' ),
			'category'        => Rsmspark_Content_Widgets_Blocks::BLOCK_CATEGORY,
			'icon'            => 'art',
			'keywords'        => array( 'svg', 'icon', 'vector', 'image', 'rsm', 'rsf', 'focus' ),
			'style'           => self::STYLE_HANDLE,
			'editor_style'    => self::EDITOR_STYLE_HANDLE,
			'editor_script'   => self::EDITOR_SCRIPT_HANDLE,
			'render_callback' => array( __CLASS__, 'render_block' ),
			'attributes'      => self::block_attributes(),
			'example'         => array(
				'viewportWidth' => 400,
				'attributes'    => array(
					'size'  => 48,
					'color' => '#ff6b35',
					'align' => 'center',
				),
			),
			'supports'        => array(
				'html' => false,
			),
		);
	}

	/**
	 * @return array<string, array<string, mixed>>
	 */
	public static function block_attributes() {
		if ( class_exists( 'RSMSPARK_RSF_Svg_Icon_Block', false ) && method_exists( 'RSMSPARK_RSF_Svg_Icon_Block', 'block_attributes' ) ) {
			return RSMSPARK_RSF_Svg_Icon_Block::block_attributes();
		}

		return array(
			'svg_id'       => array( 'type' => 'number', 'default' => 0 ),
			'svg_src'      => array( 'type' => 'string', 'default' => '' ),
			'size'         => array( 'type' => 'number', 'default' => 48 ),
			'color'        => array( 'type' => 'string', 'default' => '#ff6b35' ),
			'hover_color'  => array( 'type' => 'string', 'default' => '' ),
			'link_url'     => array( 'type' => 'string', 'default' => '' ),
			'link_new_tab' => array( 'type' => 'boolean', 'default' => false ),
			'align'        => array( 'type' => 'string', 'default' => 'center' ),
			'aria_label'   => array( 'type' => 'string', 'default' => '' ),
		);
	}

	/**
	 * @param array<string, mixed> $attributes Block attributes.
	 * @return string
	 */
	public static function render_block( $attributes ) {
		if ( ! class_exists( 'RSMSPARK_RSF_Svg_Icon', false ) ) {
			return '';
		}

		return RSMSPARK_RSF_Svg_Icon::render( is_array( $attributes ) ? $attributes : array() );
	}
}
