<?php
function handmade_craft_sanitize_select( $handmade_craft_input, $handmade_craft_setting ) {
	$handmade_craft_input = sanitize_key( $handmade_craft_input );
	$handmade_craft_choices = $handmade_craft_setting->manager->get_control( $handmade_craft_setting->id )->choices;
	return ( array_key_exists( $handmade_craft_input, $handmade_craft_choices ) ? $handmade_craft_input : $handmade_craft_setting->default );
}

function handmade_craft_sanitize_switch( $handmade_craft_input ) {
	if ( true === $handmade_craft_input ) {
		return true;
	} else {
		return false;
	}
}

function handmade_craft_sanitize_google_fonts( $handmade_craft_input, $handmade_craft_setting ) {
	$handmade_craft_choices = $handmade_craft_setting->manager->get_control( $handmade_craft_setting->id )->choices;
	return ( array_key_exists( $handmade_craft_input, $handmade_craft_choices ) ? $handmade_craft_input : $handmade_craft_setting->default );
}
/**
 * Sanitize HTML input.
 *
 * @param string $handmade_craft_input HTML input to sanitize.
 * @return string Sanitized HTML.
 */
function handmade_craft_sanitize_html( $handmade_craft_input ) {
    return wp_kses_post( $handmade_craft_input );
}

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

function handmade_craft_sanitize_choices( $handmade_craft_input, $handmade_craft_setting ) {
	global $wp_customize; 
	$control = $wp_customize->get_control( $handmade_craft_setting->id ); 
	if ( array_key_exists( $handmade_craft_input, $control->choices ) ) {
		return $handmade_craft_input;
	} else {
		return $handmade_craft_setting->default;
	}
}

function handmade_craft_sanitize_range_value( $handmade_craft_number, $handmade_craft_setting ) {

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

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

	// Get minimum number in the range.
	$handmade_craft_min = ( isset( $handmade_craft_atts['min'] ) ? $handmade_craft_atts['min'] : $handmade_craft_number );

	// Get maximum number in the range.
	$handmade_craft_max = ( isset( $handmade_craft_atts['max'] ) ? $handmade_craft_atts['max'] : $handmade_craft_number );

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $handmade_craft_min <= $handmade_craft_number && $handmade_craft_number <= $handmade_craft_max && is_int( $handmade_craft_number / $handmade_craft_step ) ? $handmade_craft_number : $handmade_craft_setting->default );
}