<?php
function wheel_alignment_service_sanitize_select( $wheel_alignment_service_input, $wheel_alignment_service_setting ) {
	$wheel_alignment_service_input = sanitize_key( $wheel_alignment_service_input );
	$wheel_alignment_service_choices = $wheel_alignment_service_setting->manager->get_control( $wheel_alignment_service_setting->id )->choices;
	return ( array_key_exists( $wheel_alignment_service_input, $wheel_alignment_service_choices ) ? $wheel_alignment_service_input : $wheel_alignment_service_setting->default );
}

function wheel_alignment_service_sanitize_switch( $wheel_alignment_service_input ) {
	if ( true === $wheel_alignment_service_input ) {
		return true;
	} else {
		return false;
	}
}

function wheel_alignment_service_sanitize_google_fonts( $wheel_alignment_service_input, $wheel_alignment_service_setting ) {
	$wheel_alignment_service_choices = $wheel_alignment_service_setting->manager->get_control( $wheel_alignment_service_setting->id )->choices;
	return ( array_key_exists( $wheel_alignment_service_input, $wheel_alignment_service_choices ) ? $wheel_alignment_service_input : $wheel_alignment_service_setting->default );
}

function wheel_alignment_service_sanitize_choices( $wheel_alignment_service_input, $wheel_alignment_service_setting ) {
    global $wp_customize;
    $control = $wp_customize->get_control( $wheel_alignment_service_setting->id );
    if ( array_key_exists( $wheel_alignment_service_input, $control->choices ) ) {
        return $wheel_alignment_service_input;
    } else {
        return $wheel_alignment_service_setting->default;
    }
}

/**
 * Sanitize HTML input.
 *
 * @param string $wheel_alignment_service_input HTML input to sanitize.
 * @return string Sanitized HTML.
 */
function wheel_alignment_service_sanitize_html( $wheel_alignment_service_input ) {
    return wp_kses_post( $wheel_alignment_service_input );
}

/**
 * Sanitize URL input.
 *
 * @param string $wheel_alignment_service_input URL input to sanitize.
 * @return string Sanitized URL.
 */
function wheel_alignment_service_sanitize_url( $wheel_alignment_service_input ) {
    return esc_url_raw( $wheel_alignment_service_input );
}

// Sanitize Scroll Top Position
function wheel_alignment_service_sanitize_scroll_top_position( $wheel_alignment_service_input ) {
    $valid_positions = array( 'bottom-right', 'bottom-left', 'bottom-center' );
    if ( in_array( $wheel_alignment_service_input, $valid_positions ) ) {
        return $wheel_alignment_service_input;
    } else {
        return 'bottom-right'; // Default to bottom-right if invalid value
    }
}

function wheel_alignment_service_sanitize_range_value( $wheel_alignment_service_number, $wheel_alignment_service_setting ) {

	// Ensure input is an absolute integer.
	$wheel_alignment_service_number = absint( $wheel_alignment_service_number );

	// Get the input attributes associated with the setting.
	$wheel_alignment_service_atts = $wheel_alignment_service_setting->manager->get_control( $wheel_alignment_service_setting->id )->input_attrs;

	// Get minimum number in the range.
	$wheel_alignment_service_min = ( isset( $wheel_alignment_service_atts['min'] ) ? $wheel_alignment_service_atts['min'] : $wheel_alignment_service_number );

	// Get maximum number in the range.
	$wheel_alignment_service_max = ( isset( $wheel_alignment_service_atts['max'] ) ? $wheel_alignment_service_atts['max'] : $wheel_alignment_service_number );

	// Get step.
	$wheel_alignment_service_step = ( isset( $wheel_alignment_service_atts['step'] ) ? $wheel_alignment_service_atts['step'] : 1 );

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $wheel_alignment_service_min <= $wheel_alignment_service_number && $wheel_alignment_service_number <= $wheel_alignment_service_max && is_int( $wheel_alignment_service_number / $wheel_alignment_service_step ) ? $wheel_alignment_service_number : $wheel_alignment_service_setting->default );
}