<?php
/**
 * Admin Color Customizer – shared admin UI helpers.
 *
 * @package WP_PowerSuite\AdminColorCustomizer
 */

namespace WP_PowerSuite\AdminColorCustomizer;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Admin_Helpers {

	/**
	 * Normalize a hex color for picker + text inputs.
	 *
	 * @return array{picker: string, hex: string}
	 */
	public static function normalize_hex_color( string $raw, string $fallback ): array {
		$color = Scheme_Registry::sanitize_hex_color( $raw ) ?? Scheme_Registry::sanitize_hex_color( $fallback ) ?? '#000000';

		return [
			'picker' => $color,
			'hex'    => $color,
		];
	}

	/**
	 * Render a scheme builder color field using the shared PowerSuite color picker row.
	 */
	public static function builder_color_field(
		string $field_key,
		string $label,
		string $value,
		string $default,
		bool $disabled = false
	): void {
		$picker_id = 'wpps-acc-color-' . $field_key;
		$colors    = self::normalize_hex_color( $value, $default );
		?>
		<div class="wpps-acc-color-field">
			<span class="wpps-acc-color-field__label"><?php echo esc_html( $label ); ?></span>
			<div class="wpps-color-field-row">
				<input
					type="color"
					id="<?php echo esc_attr( $picker_id ); ?>"
					value="<?php echo esc_attr( $colors['picker'] ); ?>"
					class="wpps-color-picker-circle"
					aria-label="<?php echo esc_attr( $label ); ?>"
					<?php disabled( $disabled ); ?>
				/>
				<div class="wpps-color-code-wrap">
					<input
						type="text"
						class="wpps-color-code-input regular-text"
						data-builder-field="<?php echo esc_attr( $field_key ); ?>"
						data-color-input="<?php echo esc_attr( $picker_id ); ?>"
						data-default-color="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
						value="<?php echo esc_attr( $colors['hex'] ); ?>"
						placeholder="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
						maxlength="7"
						aria-label="<?php echo esc_attr( $label ); ?>"
						<?php disabled( $disabled ); ?>
					/>
					<button
						type="button"
						class="wpps-color-reset button-link"
						data-default-color="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
						aria-label="<?php esc_attr_e( 'Reset to default', 'wp-powersuite' ); ?>"
						<?php disabled( $disabled ); ?>
					>
						<span class="dashicons dashicons-undo"></span>
					</button>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Render a settings color field (data-setting) with the shared PowerSuite color picker row.
	 */
	public static function setting_color_field(
		string $setting_key,
		string $label,
		string $value,
		string $default,
		string $input_id = ''
	): void {
		$picker_id = $input_id !== '' ? $input_id . '_picker' : 'wpps-acc-setting-color-' . sanitize_key( str_replace( '.', '-', $setting_key ) );
		$text_id   = $input_id !== '' ? $input_id : $picker_id . '_text';
		$colors    = self::normalize_hex_color( $value, $default );
		?>
		<div class="wpps-color-field-row">
			<input
				type="color"
				id="<?php echo esc_attr( $picker_id ); ?>"
				value="<?php echo esc_attr( $colors['picker'] ); ?>"
				class="wpps-color-picker-circle"
				aria-label="<?php echo esc_attr( $label ); ?>"
			/>
			<div class="wpps-color-code-wrap">
				<input
					type="text"
					id="<?php echo esc_attr( $text_id ); ?>"
					class="wpps-color-code-input regular-text"
					data-setting="<?php echo esc_attr( $setting_key ); ?>"
					data-color-input="<?php echo esc_attr( $picker_id ); ?>"
					data-default-color="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
					value="<?php echo esc_attr( $colors['hex'] ); ?>"
					placeholder="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
					maxlength="7"
					aria-label="<?php echo esc_attr( $label ); ?>"
				/>
				<button
					type="button"
					class="wpps-color-reset button-link"
					data-default-color="<?php echo esc_attr( self::normalize_hex_color( $default, '#000000' )['hex'] ); ?>"
					aria-label="<?php esc_attr_e( 'Reset to default', 'wp-powersuite' ); ?>"
				>
					<span class="dashicons dashicons-undo"></span>
				</button>
			</div>
		</div>
		<?php
	}
}
