<?php
declare(strict_types=1);

/**
 * Customizer — Sanitize Callbacks.
 *
 * All sanitization functions used by Customizer settings.
 *
 * @package WTE_Business_Card
 * @since   1.0.0
 */

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

/**
 * Sanitize checkbox (returns boolean).
 */
function wte_sanitize_checkbox( mixed $val ): bool {
	return (bool) $val;
}

/**
 * Sanitize email address.
 */
function wte_sanitize_email( string $val ): string {
	return sanitize_email( $val );
}

/**
 * Sanitize telephone number.
 */
function wte_sanitize_tel( string $val ): string {
	return preg_replace( '/[^0-9\-+() ]/', '', $val ) ?? '';
}

/**
 * Sanitize font choice.
 */
function wte_sanitize_font_choice( string $val ): string {
	$allowed = [
		'noto_sans_jp',
		'noto_serif_jp',
		'zen_kaku_gothic',
		'shippori_mincho',
		'm_plus_1p',
	];
	return in_array( $val, $allowed, true ) ? $val : 'noto_sans_jp';
}

/**
 * Sanitize business type (schema.org).
 */
function wte_sanitize_business_type( string $val ): string {
	$allowed = [
		'ProfessionalService',
		'LegalService',
		'Accountant',
		'FinancialPlanningService',
		'InsuranceAgency',
		'Dentist',
		'Physician',
		'Architect',
		'ITConsultant',
		'MarketingConsultant',
		'PhotographStudio',
		'TravelAgency',
		'LocalBusiness',
	];
	return in_array( $val, $allowed, true ) ? $val : 'ProfessionalService';
}

/**
 * Sanitize year (4-digit string).
 */
function wte_sanitize_year( string $val ): string {
	return preg_match( '/^\d{4}$/', $val ) ? $val : '';
}

/**
 * Sanitize float range (0.1 — 1.0).
 */
function wte_sanitize_float_range( mixed $val ): float {
	$v = (float) $val;
	return max( 0.1, min( 1.0, $v ) );
}

/**
 * Sanitize hero height.
 */
function wte_sanitize_hero_height( string $val ): string {
	$allowed = [ 'full', 'large', 'medium', 'small' ];
	return in_array( $val, $allowed, true ) ? $val : 'full';
}

/**
 * Sanitize icon choice.
 */
function wte_sanitize_icon_choice( string $val ): string {
	$allowed = [
		'star',
		'check',
		'briefcase',
		'document',
		'chat',
		'gear',
		'lock',
		'chart',
		'heart',
		'lightbulb',
		'shield',
		'users',
	];
	return in_array( $val, $allowed, true ) ? $val : 'star';
}

/**
 * Sanitize select (generic).
 */
function wte_sanitize_select( string $val, \WP_Customize_Setting $setting ): string {
	$control = $setting->manager->get_control( $setting->id );
	if ( $control && isset( $control->choices[ $val ] ) ) {
		return $val;
	}
	return $setting->default;
}

/**
 * Sanitize price range.
 */
function wte_sanitize_price_range( string $val ): string {
	$allowed = [ '¥', '¥¥', '¥¥¥', '¥¥¥¥' ];
	return in_array( $val, $allowed, true ) ? $val : '¥¥';
}
