<?php
/**
 * Suite-wide SiteOrigin Page Builder widget icon branding.
 *
 * Fallback when Rsmspark_Panels_Branding::ensure_suite_branding() is unavailable (older rsm-shared copy).
 *
 * @package Rsmspark_Suite
 */

defined( 'ABSPATH' ) || exit;

if ( class_exists( 'Rsmspark_Suite_SiteOrigin_Branding', false ) ) {
	return;
}

/**
 * Rsmspark_Suite_SiteOrigin_Branding
 */
final class Rsmspark_Suite_SiteOrigin_Branding {

	public const BRAND_ORANGE = '#FF6B35';

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

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

		if (
			! class_exists( 'SiteOrigin_Panels', false )
			&& ! defined( 'SITEORIGIN_PANELS_VERSION' )
			&& ! class_exists( 'SiteOrigin_Widget', false )
		) {
			return;
		}

		self::$initialized = true;
		delete_transient( 'siteorigin_panels_widgets' );
		add_filter( 'siteorigin_panels_widgets', array( __CLASS__, 'brand_widgets' ), 14 );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
	}

	/**
	 * @param array<string, mixed> $widgets Panels widgets.
	 * @return array<string, mixed>
	 */
	public static function brand_widgets( $widgets ) {
		if ( ! is_array( $widgets ) ) {
			return $widgets;
		}

		foreach ( $widgets as $class => $widget ) {
			$class = (string) $class;
			if ( ! is_array( $widget ) || ! self::is_rsm_widget( $class, $widget ) ) {
				continue;
			}

			$icon = (string) ( $widgets[ $class ]['icon'] ?? 'dashicons dashicons-admin-generic' );
			if ( false === strpos( $icon, 'rsm-panels-icon' ) ) {
				$widgets[ $class ]['icon'] = 'rsm-panels-icon ' . $icon;
			}
		}

		return $widgets;
	}

	/**
	 * @param string               $class  Widget PHP class.
	 * @param array<string, mixed> $widget Widget metadata.
	 * @return bool
	 */
	private static function is_rsm_widget( $class, array $widget ) {
		if ( preg_match( '/(?:^|\\\\)RSM[_\\\\]/', (string) $class ) ) {
			return true;
		}

		$title = (string) ( $widget['title'] ?? '' );
		if ( '' === $title ) {
			return false;
		}

		return (bool) preg_match( '/^RSM(?:\s|:|$)/i', $title ) || 0 === stripos( $title, 'RSMMenu' );
	}

	/**
	 * @param string $hook Admin hook.
	 * @return void
	 */
	public static function enqueue_styles( $hook ) {
		if ( ! in_array( $hook, array( 'post.php', 'post-new.php', 'widgets.php' ), true ) ) {
			return;
		}

		$css = sprintf(
			':root{--rsm-brand-orange:%1$s;}
			.so-panels-dialog .rsm-panels-icon::before,
			.so-panels-dialog .widget-icon.rsm-panels-icon::before,
			.so-panels-dialog .widget-icon.rsm-panels-icon,
			.so-available-widgets-list .rsm-panels-icon::before,
			.so-available-widgets-list .widget-icon.rsm-panels-icon::before,
			#so-panels-add-widgets-dialog .rsm-panels-icon::before,
			#so-panels-add-widgets-dialog .widget-icon.rsm-panels-icon::before,
			.so-panels-add-widget-dialog .rsm-panels-icon::before,
			.so-panels-add-widget-dialog .widget-icon.rsm-panels-icon::before,
			.so-panels-dialog .rsm-cw-panels-icon::before,
			.so-panels-dialog .widget-icon.rsm-cw-panels-icon::before,
			.so-available-widgets-list .rsm-cw-panels-icon::before,
			#so-panels-add-widgets-dialog .rsm-cw-panels-icon::before,
			.so-panels-add-widget-dialog .rsm-cw-panels-icon::before,
			.so-panels-dialog .widget[class*="RSM_"] .widget-icon,
			.so-panels-dialog .widget[class*="RSM_"] .widget-icon::before,
			.so-available-widgets-list .widget[class*="RSM_"] .widget-icon::before{color:var(--rsm-brand-orange)!important;}
			.rsm-license-tier-label{display:inline-block;margin-left:6px;padding:1px 6px;font-size:11px;font-weight:600;text-transform:uppercase;color:#fff;background:%1$s;border-radius:3px;vertical-align:middle;}',
			esc_attr( self::BRAND_ORANGE )
		);

		$version = defined( 'RSMSPARK_SUITE_VERSION' ) ? RSMSPARK_SUITE_VERSION : '1.6.2';
		wp_register_style( 'rsmspark-suite-siteorigin-branding', false, array(), $version );
		wp_enqueue_style( 'rsmspark-suite-siteorigin-branding' );
		wp_add_inline_style( 'rsmspark-suite-siteorigin-branding', $css );
	}
}
