<?php

namespace SugarCalendarBookings\Support\DataProcessing;

use WP_Error;

/**
 * InputProcessor class.
 *
 * @since 1.0.0
 */
class InputProcessor {

	/**
	 * Process the input data.
	 *
	 * The input data is normalized, validated, and sanitized.
	 *
	 * @since 1.0.0
	 *
	 * @param array $data  The input data.
	 * @param array $rules The rules to process the data.
	 *
	 * @return array|WP_Error The processed data or a WP_Error if validation fails.
	 */
	public static function process( $data, $rules ) {

		$normalized_data = Normalizer::normalize( $data, $rules );

		$is_valid = Validator::validate( $normalized_data, $rules );

		if ( is_wp_error( $is_valid ) ) {
			return $is_valid;
		}

		$sanitized_data = Sanitizer::sanitize( $normalized_data, $rules );

		return $sanitized_data;
	}

	/**
	 * Process request data with normalization and sanitization (no validation).
	 *
	 * Similar to process() but skips validation step.
	 * Useful for processing $_REQUEST data in list tables where validation
	 * is not needed and empty/invalid values should fall back to defaults.
	 *
	 * @since 1.2.0
	 *
	 * @param array $data  The request data.
	 * @param array $rules Processing rules (normalize + sanitize).
	 *
	 * @return array Processed data.
	 */
	public static function process_request( $data, $rules ) {

		// Step 1: Normalize (trim whitespace, lowercase, etc.).
		$normalized = Normalizer::normalize( $data, $rules );

		// Step 2: Sanitize (type-cast, clean).
		$sanitized = Sanitizer::sanitize( $normalized, $rules );

		return $sanitized;
	}
}
