<?php

function auto_mechanic_sanitize_select( $auto_mechanic_input, $auto_mechanic_setting ) {
	$auto_mechanic_input = sanitize_key( $auto_mechanic_input );
	$auto_mechanic_choices = $auto_mechanic_setting->manager->get_control( $auto_mechanic_setting->id )->choices;
	return ( array_key_exists( $auto_mechanic_input, $auto_mechanic_choices ) ? $auto_mechanic_input : $auto_mechanic_setting->default );
}

function auto_mechanic_sanitize_switch( $auto_mechanic_input ) {
	if ( true === $auto_mechanic_input ) {
		return true;
	} else {
		return false;
	}
}
function auto_mechanic_sanitize_choices( $auto_mechanic_input, $auto_mechanic_setting ) {
    global $wp_customize;

    $auto_mechanic_control = $wp_customize->get_control( $auto_mechanic_setting->id );
    if ( array_key_exists( $auto_mechanic_input, $auto_mechanic_control->choices ) ) {
        return $auto_mechanic_input;
    } else {
        return $auto_mechanic_setting->default;
    }
}

function auto_mechanic_sanitize_google_fonts( $auto_mechanic_input, $auto_mechanic_setting ) {
    $choices = $auto_mechanic_setting->manager->get_control( $auto_mechanic_setting->id )->choices;
    return ( array_key_exists( $auto_mechanic_input, $choices ) ? $auto_mechanic_input : $auto_mechanic_setting->default );
}

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

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

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

function auto_mechanic_sanitize_range_value( $auto_mechanic_number, $auto_mechanic_setting ) {

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

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

	// Get minimum number in the range.
	$auto_mechanic_min = ( isset( $auto_mechanic_atts['min'] ) ? $auto_mechanic_atts['min'] : $auto_mechanic_number );

	// Get maximum number in the range.
	$auto_mechanic_max = ( isset( $auto_mechanic_atts['max'] ) ? $auto_mechanic_atts['max'] : $auto_mechanic_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $auto_mechanic_min <= $auto_mechanic_number && $auto_mechanic_number <= $auto_mechanic_max && is_int( $auto_mechanic_number / $auto_mechanic_step ) ? $auto_mechanic_number : $auto_mechanic_setting->default );
}