<?php
/**
 * Focal point and aspect-ratio helpers for Image and Hero media.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

/**
 * Rsmspark_Content_Widgets_Focal_Point
 */
final class Rsmspark_Content_Widgets_Focal_Point {

	/**
	 * Named focal presets → object-position values.
	 *
	 * @return array<string, string>
	 */
	public static function preset_options() {
		return array(
			'center'       => __( 'Center', 'rsm-content-widgets' ),
			'top'          => __( 'Top', 'rsm-content-widgets' ),
			'bottom'       => __( 'Bottom', 'rsm-content-widgets' ),
			'left'         => __( 'Left', 'rsm-content-widgets' ),
			'right'        => __( 'Right', 'rsm-content-widgets' ),
			'top-left'     => __( 'Top left', 'rsm-content-widgets' ),
			'top-right'    => __( 'Top right', 'rsm-content-widgets' ),
			'bottom-left'  => __( 'Bottom left', 'rsm-content-widgets' ),
			'bottom-right' => __( 'Bottom right', 'rsm-content-widgets' ),
			'custom'       => __( 'Custom', 'rsm-content-widgets' ),
		);
	}

	/**
	 * Aspect ratio presets for cover crops.
	 *
	 * @return array<string, string>
	 */
	public static function aspect_ratio_options() {
		return array(
			'auto'   => __( 'Natural (no crop)', 'rsm-content-widgets' ),
			'16-9'   => __( '16:9', 'rsm-content-widgets' ),
			'4-3'    => __( '4:3', 'rsm-content-widgets' ),
			'1-1'    => __( '1:1 square', 'rsm-content-widgets' ),
			'3-4'    => __( '3:4 portrait', 'rsm-content-widgets' ),
			'21-9'   => __( '21:9 ultrawide', 'rsm-content-widgets' ),
		);
	}

	/**
	 * @param mixed $value Raw preset.
	 * @return string
	 */
	public static function sanitize_preset( $value ) {
		$key = sanitize_key( (string) $value );
		return array_key_exists( $key, self::preset_options() ) ? $key : 'center';
	}

	/**
	 * @param mixed $value Raw aspect key.
	 * @return string
	 */
	public static function sanitize_aspect_ratio( $value ) {
		$key = sanitize_key( (string) $value );
		return array_key_exists( $key, self::aspect_ratio_options() ) ? $key : 'auto';
	}

	/**
	 * @param mixed $value Raw coordinate 0–100.
	 * @return int
	 */
	public static function sanitize_coord( $value ) {
		return max( 0, min( 100, (int) $value ) );
	}

	/**
	 * Resolve object-position / background-position CSS value.
	 *
	 * @param string $preset  Preset key.
	 * @param int    $x       Custom X (0–100) when preset is custom.
	 * @param int    $y       Custom Y (0–100) when preset is custom.
	 * @return string
	 */
	public static function resolve_position( $preset, $x = 50, $y = 50 ) {
		$preset = self::sanitize_preset( $preset );

		if ( 'custom' === $preset ) {
			return self::sanitize_coord( $x ) . '% ' . self::sanitize_coord( $y ) . '%';
		}

		$map = array(
			'center'       => 'center center',
			'top'          => 'center top',
			'bottom'       => 'center bottom',
			'left'         => 'left center',
			'right'        => 'right center',
			'top-left'     => 'left top',
			'top-right'    => 'right top',
			'bottom-left'  => 'left bottom',
			'bottom-right' => 'right bottom',
		);

		return $map[ $preset ] ?? 'center center';
	}

	/**
	 * CSS aspect-ratio value for a preset key.
	 *
	 * @param string $preset Aspect preset key.
	 * @return string Empty when auto.
	 */
	public static function aspect_ratio_css( $preset ) {
		$preset = self::sanitize_aspect_ratio( $preset );

		$map = array(
			'16-9' => '16 / 9',
			'4-3'  => '4 / 3',
			'1-1'  => '1 / 1',
			'3-4'  => '3 / 4',
			'21-9' => '21 / 9',
		);

		return $map[ $preset ] ?? '';
	}
}
