<?php
/**
 * Best-effort theme button color detection for new-button defaults.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Buttons_Theme_Defaults
 */
class Rsmspark_Buttons_Theme_Defaults {

	/**
	 * @return array<string, string>
	 */
	public static function get() {
		$detected = array_merge(
			self::from_global_styles(),
			self::from_theme_mods()
		);

		$detected = array_filter(
			$detected,
			static function ( $value ) {
				return is_string( $value ) && '' !== trim( $value );
			}
		);

		return apply_filters( 'rsmspark_buttons_theme_defaults', $detected );
	}

	/**
	 * Human-readable summary for the settings screen.
	 *
	 * @return array{source: string, colors: array<string, string>}
	 */
	public static function get_summary() {
		$colors = self::get();
		$source = __( 'Active theme', 'rsm-content-widgets' );

		if ( function_exists( 'wp_get_global_styles' ) ) {
			$source = __( 'Theme global styles (theme.json)', 'rsm-content-widgets' );
		}

		return array(
			'source' => $source,
			'colors' => $colors,
		);
	}

	/**
	 * WordPress 6+ theme.json / global styles — most reliable when present.
	 *
	 * @return array<string, string>
	 */
	private static function from_global_styles() {
		if ( ! function_exists( 'wp_get_global_styles' ) ) {
			return array();
		}

		$styles = wp_get_global_styles();
		if ( ! is_array( $styles ) ) {
			return array();
		}

		$button = is_array( $styles['elements']['button'] ?? null ) ? $styles['elements']['button'] : array();
		$hover  = is_array( $button[':hover'] ?? null ) ? $button[':hover'] : array();

		$out = array();

		$bg = self::resolve_style_color( $button['color']['background'] ?? '' );
		if ( $bg ) {
			$out['bg_color'] = $bg;
		}

		$text = self::resolve_style_color( $button['color']['text'] ?? '' );
		if ( $text ) {
			$out['text_color'] = $text;
		}

		$bg_hover = self::resolve_style_color( $hover['color']['background'] ?? '' );
		if ( $bg_hover ) {
			$out['bg_hover_color'] = $bg_hover;
		}

		$text_hover = self::resolve_style_color( $hover['color']['text'] ?? '' );
		if ( $text_hover ) {
			$out['text_hover_color'] = $text_hover;
		}

		$border = self::resolve_style_color( $button['border']['color'] ?? '' );
		if ( $border ) {
			$out['border_color'] = $border;
		}

		return $out;
	}

	/**
	 * A few common customizer keys — low coverage, filterable risk.
	 *
	 * @return array<string, string>
	 */
	private static function from_theme_mods() {
		$out  = array();
		$mods = array(
			'button_background_color' => 'bg_color',
			'button_text_color'       => 'text_color',
			'accent_color'            => 'bg_color',
			'primary_color'           => 'bg_color',
			'link_color'              => 'text_color',
		);

		foreach ( $mods as $mod_key => $config_key ) {
			if ( isset( $out[ $config_key ] ) ) {
				continue;
			}

			$value = get_theme_mod( $mod_key );
			$color = self::normalize_color( $value );
			if ( $color ) {
				$out[ $config_key ] = $color;
			}
		}

		return $out;
	}

	/**
	 * @param mixed $value Style color value.
	 * @return string
	 */
	private static function resolve_style_color( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return '';
		}

		if ( preg_match( '/^var:preset\|color\|([a-z0-9_-]+)$/i', $value, $match ) ) {
			$slug = sanitize_key( $match[1] );
			if ( function_exists( 'wp_get_global_settings' ) ) {
				$settings = wp_get_global_settings();
				$palette  = is_array( $settings['color']['palette']['theme'] ?? null ) ? $settings['color']['palette']['theme'] : array();
				foreach ( $palette as $entry ) {
					if ( is_array( $entry ) && $slug === sanitize_key( (string) ( $entry['slug'] ?? '' ) ) ) {
						return self::normalize_color( $entry['color'] ?? '' );
					}
				}
			}
			return '';
		}

		return self::normalize_color( $value );
	}

	/**
	 * @param mixed $value Raw color.
	 * @return string
	 */
	private static function normalize_color( $value ) {
		$value = trim( (string) $value );
		if ( '' === $value ) {
			return '';
		}

		if ( in_array( strtolower( $value ), array( 'transparent', 'none' ), true ) ) {
			return 'transparent';
		}

		$hex = sanitize_hex_color( $value );
		return $hex ? $hex : '';
	}
}
