<?php

function cat_groomer_sanitize_select( $cat_groomer_input, $cat_groomer_setting ) {
	$cat_groomer_input = sanitize_key( $cat_groomer_input );
	$cat_groomer_choices = $cat_groomer_setting->manager->get_control( $cat_groomer_setting->id )->choices;
	return ( array_key_exists( $cat_groomer_input, $cat_groomer_choices ) ? $cat_groomer_input : $cat_groomer_setting->default );
}

function cat_groomer_sanitize_switch( $cat_groomer_input ) {
	if ( true === $cat_groomer_input ) {
		return true;
	} else {
		return false;
	}
}

function cat_groomer_sanitize_google_fonts( $cat_groomer_input, $cat_groomer_setting ) {
	$cat_groomer_choices = $cat_groomer_setting->manager->get_control( $cat_groomer_setting->id )->choices;
	return ( array_key_exists( $cat_groomer_input, $cat_groomer_choices ) ? $cat_groomer_input : $cat_groomer_setting->default );
}

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

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

function cat_groomer_sanitize_choices( $cat_groomer_input, $cat_groomer_setting ) {
    global $wp_customize; 
    $control = $wp_customize->get_control( $cat_groomer_setting->id ); 
    if ( array_key_exists( $cat_groomer_input, $control->choices ) ) {
        return $cat_groomer_input;
    } else {
        return $cat_groomer_setting->default;
    }
}

function cat_groomer_sanitize_range_value( $number, $cat_groomer_setting ) {

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

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

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

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

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

	// If the number is within the valid range, return it; otherwise, return the default.
	return ( $min <= $number && $number <= $max && is_int( $number / $step ) ? $number : $cat_groomer_setting->default );
}