<?php
/**
 * Font choices aligned with RSMSpark theme / RSM Forms Customizer options.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Resolves widget font keys to CSS font-family stacks.
 */
final class Rsmspark_Content_Widgets_Features_Fonts {

	/**
	 * Font pulldown choices (same families as RSM Forms headline fonts).
	 *
	 * @return array<int, array{value: string, label: string}>
	 */
	public static function get_choices() {
		return array(
			array(
				'value' => 'inherit',
				'label' => __( 'Theme default (Customizer)', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'system_ui',
				'label' => __( 'System UI', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'georgia_serif',
				'label' => __( 'Georgia / Times (serif)', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'arial_sans',
				'label' => __( 'Arial / Helvetica (sans-serif)', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'verdana',
				'label' => __( 'Verdana / Geneva', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'tahoma',
				'label' => __( 'Tahoma', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'trebuchet',
				'label' => __( 'Trebuchet MS', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'courier',
				'label' => __( 'Courier New (monospace)', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'palatino',
				'label' => __( 'Palatino / Book Antiqua', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'comic',
				'label' => __( 'Comic Sans MS', 'rsm-content-widgets' ),
			),
			array(
				'value' => 'impact',
				'label' => __( 'Impact', 'rsm-content-widgets' ),
			),
		);
	}

	/**
	 * SiteOrigin select field options (value => label).
	 *
	 * @return array<string, string>
	 */
	public static function get_select_options() {
		$options = array();
		foreach ( self::get_choices() as $row ) {
			$options[ $row['value'] ] = $row['label'];
		}
		return $options;
	}

	/**
	 * CSS font-family stack for a stored font key.
	 *
	 * @param string $key Sanitized key.
	 * @return string
	 */
	public static function get_stack( $key ) {
		$key = sanitize_key( (string) $key );
		$map = array(
			'inherit'       => 'inherit',
			'system_ui'     => 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif',
			'georgia_serif' => 'Georgia, "Times New Roman", Times, serif',
			'arial_sans'    => 'Arial, Helvetica, sans-serif',
			'verdana'       => 'Verdana, Geneva, sans-serif',
			'tahoma'        => 'Tahoma, Geneva, sans-serif',
			'trebuchet'     => '"Trebuchet MS", "Lucida Grande", Lucida, Arial, sans-serif',
			'courier'       => '"Courier New", Courier, monospace',
			'palatino'      => 'Palatino, "Palatino Linotype", "Book Antiqua", Georgia, serif',
			'comic'         => '"Comic Sans MS", "Comic Sans", cursive',
			'impact'        => 'Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif',
		);

		return isset( $map[ $key ] ) ? $map[ $key ] : $map['inherit'];
	}

	/**
	 * Sanitize a font key against known choices.
	 *
	 * @param mixed $value Raw value.
	 * @return string
	 */
	public static function sanitize_key( $value ) {
		$key = sanitize_key( (string) $value );
		foreach ( self::get_choices() as $row ) {
			if ( isset( $row['value'] ) && $row['value'] === $key ) {
				return $key;
			}
		}
		return 'inherit';
	}
}
