<?php
/**
 * Validation and sanitization helper functions.
 */

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

/**
 * Sanitize template ID.
 *
 * @param string $template_id The raw template ID.
 * @return string Sanitized template ID.
 */
function hundred_prints_sanitize_template_id( $template_id ) {
	return preg_replace( '/[^a-zA-Z0-9_-]/', '', $template_id );
}

/**
 * Validate variable map structure.
 *
 * @param array $variable_map The raw variable map.
 * @return array Validated and sanitized variable map.
 */
function hundred_prints_validate_variable_map( $variable_map ) {
	if ( ! is_array( $variable_map ) ) {
		return [];
	}

	$sanitized_map = [];
	$is_list_map = array_is_list( $variable_map );
	$registry = function_exists( 'hundred_prints_get_variable_source_registry' ) ? hundred_prints_get_variable_source_registry() : null;

	foreach ( $variable_map as $var_name => $rule ) {
		if ( ! is_array( $rule ) ) {
			continue;
		}

		$clean_var_name = $is_list_map && isset( $rule['key'] ) ? sanitize_text_field( $rule['key'] ) : sanitize_text_field( $var_name );
		if ( '' === $clean_var_name ) {
			continue;
		}

		$rule['key'] = $clean_var_name;

		if ( $registry ) {
			$sanitized_rule = $registry->sanitize_rule( $rule );
		} else {
			$source_key = isset( $rule['source'] ) ? sanitize_key( $rule['source'] ) : 'manual';
			$sanitized_rule = [
				'key'         => $clean_var_name,
				'source'      => $source_key,
				'sub_key'     => isset( $rule['sub_key'] ) ? sanitize_text_field( $rule['sub_key'] ) : '',
				'staticValue' => isset( $rule['staticValue'] ) ? sanitize_text_field( $rule['staticValue'] ) : '',
			];
		}

		$source_key = isset( $sanitized_rule['source'] ) ? sanitize_key( $sanitized_rule['source'] ) : '';
		$source_definition = function_exists( 'hundred_prints_get_variable_mapping_source' ) ? hundred_prints_get_variable_mapping_source( $source_key ) : null;

		if ( ! $registry && $source_definition && ! empty( $source_definition['fields'] ) && is_array( $source_definition['fields'] ) ) {
			$sanitized_rule = hundred_prints_sanitize_rule_from_source_definition( $sanitized_rule, $rule, $source_definition );
		} elseif ( $registry && ! $registry->get_source( $source_key ) && $source_definition && ! empty( $source_definition['fields'] ) && is_array( $source_definition['fields'] ) ) {
			$sanitized_rule = hundred_prints_sanitize_rule_from_source_definition( $sanitized_rule, $rule, $source_definition );
		}

		if ( $is_list_map ) {
			$sanitized_map[] = $sanitized_rule;
		} else {
			$sanitized_map[ $clean_var_name ] = $sanitized_rule;
		}
	}

	return $sanitized_map;
}

/**
 * Preserve third-party metadata-filter fields for sources without source objects.
 *
 * @param array $sanitized_rule    Sanitized rule.
 * @param array $raw_rule          Raw rule.
 * @param array $source_definition Source metadata.
 * @return array
 */
function hundred_prints_sanitize_rule_from_source_definition( array $sanitized_rule, array $raw_rule, array $source_definition ) {
	foreach ( $source_definition['fields'] as $field ) {
		$field_key = isset( $field['key'] ) ? sanitize_key( $field['key'] ) : '';
		if ( '' === $field_key || ! array_key_exists( $field_key, $raw_rule ) ) {
			continue;
		}

		$sanitized_rule[ $field_key ] = hundred_prints_sanitize_variable_mapping_field_value( $field, $raw_rule[ $field_key ] );
	}

	return $sanitized_rule;
}
