<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

if ( ! class_exists( 'SftcyWordformFormSubmission' ) ) {
	class SftcyWordformFormSubmission {
		public static $sc_wordform_submission_readable_data = array();
		public static $sc_wordform_submission_array_data    = array();
		public static $sc_wordform_processed_forms_raw_data = array();
		public static $sc_wordform_return_processed_raw_data = false;

		/**
		 * Build a normalized processed field payload for reports, exports, and integrations.
		 *
		 * @param string $label       Public field label.
		 * @param string $value       Submitted value.
		 * @param string $admin_label Internal/admin field label.
		 * @return array
		 */
		private static function build_processed_field_payload( $label = '', $value = '', $admin_label = '', $field_type = '', $raw_value = '' ) {
			return array(
				'formLabelName'   => sanitize_text_field( $label ),
				'formFieldValues' => is_string( $value ) ? $value : sanitize_text_field( (string) $value ),
				'formAdminLabel'  => sanitize_key( $admin_label ),
				'formFieldType'   => sanitize_key( $field_type ),
				'formRawValue'    => is_string( $raw_value ) ? $raw_value : sanitize_text_field( (string) $raw_value ),
			);
		}

		private static function wordform_extract_scalar_submission_value( $values = array() ) {
			if ( is_array( $values ) ) {
				$flattened = array();

				array_walk_recursive(
					$values,
					function( $item ) use ( &$flattened ) {
						if ( is_scalar( $item ) ) {
							$item = trim( (string) $item );
							if ( '' !== $item ) {
								$flattened[] = $item;
							}
						}
					}
				);

				return isset( $flattened[0] ) ? (string) $flattened[0] : '';
			}

			return is_scalar( $values ) ? trim( (string) $values ) : '';
		}

		private static function wordform_replace_format_tokens( $format = '', $mapping = array() ) {
			$format = (string) $format;
			if ( '' === $format || empty( $mapping ) ) {
				return $format;
			}

			uksort(
				$mapping,
				function( $left, $right ) {
					return strlen( $right ) <=> strlen( $left );
				}
			);

			$placeholders = array();
			$index        = 0;

			foreach ( $mapping as $token => $replacement ) {
				$placeholder                 = '__wf_token_' . $index . '__';
				$format                      = str_replace( $token, $placeholder, $format );
				$placeholders[ $placeholder ] = $replacement;
				$index++;
			}

			return strtr( $format, $placeholders );
		}

		private static function wordform_convert_jquery_date_format_to_php( $format = '' ) {
			$format = trim( (string) $format );
			if ( '' === $format ) {
				$format = 'yy-mm-dd';
			}

			return self::wordform_replace_format_tokens(
				$format,
				array(
					'DD' => 'l',
					'D'  => 'D',
					'MM' => 'F',
					'M'  => 'M',
					'yy' => 'Y',
					'y'  => 'y',
					'mm' => 'm',
					'm'  => 'n',
					'dd' => 'd',
					'd'  => 'j',
				)
			);
		}

		private static function wordform_convert_jquery_time_format_to_php( $format = '' ) {
			$format = trim( (string) $format );
			if ( '' === $format ) {
				$format = 'HH:mm';
			}

			return self::wordform_replace_format_tokens(
				$format,
				array(
					'TT' => 'A',
					'T'  => 'a',
					'HH' => 'H',
					'H'  => 'G',
					'hh' => 'h',
					'h'  => 'g',
					'mm' => 'i',
					'm'  => 'i',
					'ss' => 's',
					's'  => 's',
				)
			);
		}

		private static function wordform_split_combined_datetime_format( $format = '' ) {
			$format = trim( preg_replace( '/\s+/', ' ', (string) $format ) );
			if ( '' === $format ) {
				return array(
					'date' => 'yy-mm-dd',
					'time' => 'HH:mm',
				);
			}

			if ( preg_match( '/([Hh]{1,2}:[m]{2}(?::[s]{2})?(?:\s?[Tt]{1,2})?)$/', $format, $matches, PREG_OFFSET_CAPTURE ) ) {
				$match     = $matches[1][0];
				$match_pos = $matches[1][1];
				$date_fmt  = trim( rtrim( substr( $format, 0, $match_pos ), ", \t\n\r\0\x0B" ) );

				return array(
					'date' => '' !== $date_fmt ? $date_fmt : 'yy-mm-dd',
					'time' => trim( $match ),
				);
			}

			return array(
				'date' => $format,
				'time' => 'HH:mm',
			);
		}

		private static function wordform_format_date_value_for_display( $raw_value = '', $format = '' ) {
			$raw_value = trim( (string) $raw_value );
			if ( '' === $raw_value ) {
				return '';
			}

			$date_obj = DateTime::createFromFormat( 'Y-m-d', $raw_value, wp_timezone() );
			if ( ! $date_obj instanceof DateTime ) {
				return sanitize_text_field( $raw_value );
			}

			$php_format = self::wordform_convert_jquery_date_format_to_php( $format );
			return $date_obj->format( $php_format );
		}

		private static function wordform_format_time_value_for_display( $raw_value = '', $time_display_mode = '24h' ) {
			$raw_value = trim( (string) $raw_value );
			if ( '' === $raw_value ) {
				return '';
			}

			$time_obj = DateTime::createFromFormat( 'H:i', $raw_value, wp_timezone() );
			if ( ! $time_obj instanceof DateTime ) {
				return sanitize_text_field( $raw_value );
			}

			$time_format = '12h' === trim( (string) $time_display_mode ) ? 'hh:mm TT' : 'HH:mm';
			return $time_obj->format( self::wordform_convert_jquery_time_format_to_php( $time_format ) );
		}

		private static function wordform_format_datetime_value_for_display( $raw_value = '', $combined_format = '' ) {
			$raw_value = trim( str_replace( 'T', ' ', (string) $raw_value ) );
			if ( '' === $raw_value ) {
				return '';
			}

			$datetime_obj = DateTime::createFromFormat( 'Y-m-d H:i', $raw_value, wp_timezone() );
			if ( ! $datetime_obj instanceof DateTime ) {
				return sanitize_text_field( $raw_value );
			}

			$split = self::wordform_split_combined_datetime_format( $combined_format );
			$php_date_format = self::wordform_convert_jquery_date_format_to_php( $split['date'] );
			$php_time_format = self::wordform_convert_jquery_time_format_to_php( $split['time'] );

			return trim( $datetime_obj->format( $php_date_format . ' ' . $php_time_format ) );
		}

		/**
		 * Process users form submission data
		 *
		 * @since 1.0.0
		 */
		public static function sc_wordform_process_submission_data() {
			self::$sc_wordform_submission_readable_data = array();

			foreach ( self::$sc_wordform_submission_array_data as $input_type => $input_data ) {
				switch ( $input_type ) {
					case 'text':
						self::process_text( $input_data );
						break;
					case 'firstlastname':
						self::process_firstlastname( $input_data );
						break;
					case 'number':
						self::process_number( $input_data );
						break;
					case 'textarea':
						self::process_textarea( $input_data );
						break;
					case 'richeditor':
						self::process_richeditor( $input_data );
						break;
					case 'range':
						self::process_range( $input_data );
						break;	
					case 'url':
						self::process_url( $input_data );
						break;	
					case 'tel':
						self::process_tel( $input_data );
						break;	
					case 'date':
						self::process_date( $input_data );
						break;
					case 'time':
						self::process_time( $input_data );
						break;
					case 'datetime':
						self::process_datetime( $input_data );
						break;
					case 'radio':
						self::process_radio( $input_data );
						break;
					case 'checkbox':
						self::process_checkbox( $input_data );
						break;
					case 'select':
						self::process_select( $input_data );
						break;
					case 'email':
						self::process_email( $input_data );
						break;
					case 'address':
						self::process_address( $input_data );
						break;
					case 'maskinput':
						self::process_maskinput( $input_data );
						break;
					case 'hidden':
						self::process_hidden( $input_data );
						break;
					case 'upload':
						self::process_upload( $input_data );
						break;
					case 'rating':
						self::process_rating( $input_data );
						break;
					case 'likert':
						self::process_likert( $input_data );
						break;
					case 'signature':
						self::process_signature( $input_data );
						break;
					case 'gdpr':
						self::process_gdpr( $input_data );
						break;
					case 'terms':
						self::process_terms( $input_data );
						break;
					case 'payment_item':
						self::process_payment_item( $input_data );
						break;
					case 'payment_coupon':
						self::process_payment_coupon( $input_data );
						break;
					case 'payment_method':
						self::process_payment_method( $input_data );
						break;
					case 'payment_custom_amount':
						self::process_payment_custom_amount( $input_data );
						break;
					case 'payment_quantity':
						self::process_payment_quantity( $input_data );
						break;
					case 'payment_summary':
						self::process_payment_summary( $input_data );
						break;
					case 'stripe':
						self::process_stripe( $input_data );
						break;
					case 'paypal':
						self::process_paypal( $input_data );
						break;
					case 'square':
						self::process_square( $input_data );
						break;
					case 'authorizenet':
						self::process_authorizenet( $input_data );
						break;
	
				} // switch
			} // foreach

			if ( self::$sc_wordform_return_processed_raw_data === true ) {
				return self::$sc_wordform_processed_forms_raw_data;
			}
			else {
				return implode( '<br/>', self::$sc_wordform_submission_readable_data );
			}
		}

		/**
		 * Process Text data
		 *
		 * @since 1.0.0
		 */
		public static function process_text( $input_data ) {
			// echo '<pre>';
			// print_r( $input_data );
			// echo '</pre>';
			// exit();

			foreach ( $input_data as $data ) {				
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabelFirst'] ) ? $data['adminLabelFirst'] : '' );

			} // foreach
		}

		/**
		 * Process First Last Name data
		 *
		 * @since 1.0.0
		 */
		public static function process_firstlastname( $input_data ) {
			// echo '<pre>';
			// print_r( $input_data );
			// echo '</pre>';
			// exit();
			
			foreach ( $input_data as $data ) {				
				$temp  = '';
				$temp .= isset( $data['labelFirst'] ) ? '<strong>' . sanitize_text_field( $data['labelFirst'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values']['first'] ) && array_filter( $data['values']['first'] ) ? sanitize_text_field( implode( ' ', $data['values']['first'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				$temp  = '';
				$temp .= isset( $data['labelLast'] ) ? '<strong>' . sanitize_text_field( $data['labelLast'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values']['last'] ) && array_filter( $data['values']['last'] ) ? sanitize_text_field( implode( ' ', $data['values']['last'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['labelFirst'] ) ? sanitize_text_field( $data['labelFirst'] ) : '';
				$form_field_values = isset( $data['values']['first'] ) && array_filter( $data['values']['first'] ) ? sanitize_text_field( implode( ' ', $data['values']['first'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabelFirst'] ) ? $data['adminLabelFirst'] : '', 'firstlastname' );

				$form_label_name = isset( $data['labelLast'] ) ? sanitize_text_field( $data['labelLast'] ) : '';
				$form_field_values = isset( $data['values']['last'] ) && array_filter( $data['values']['last'] ) ? sanitize_text_field( implode( ' ', $data['values']['last'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabelLast'] ) ? $data['adminLabelLast'] : '', 'firstlastname' );

			} // foreach
		}
		

		/**
		 * Process Number data
		 *
		 * @since 1.0.0
		 */
		public static function process_number( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'number' );
			} // foreach
		}

		/**
		 * Process Textarea data
		 *
		 * @since 1.0.0
		 */
		public static function process_textarea( $input_data ) {
			foreach ( $input_data as $data ) {				
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? nl2br( sanitize_textarea_field( implode( ',', $data['values'] ) ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Rich Text Editor data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_richeditor( $input_data ) {
			foreach ( $input_data as $data ) {
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$raw_html        = isset( $data['values'][0] ) ? wp_kses_post( (string) $data['values'][0] ) : '';
				$plain_text      = trim( wp_strip_all_tags( html_entity_decode( $raw_html, ENT_QUOTES, get_bloginfo( 'charset' ) ) ) );

				$temp  = '';
				$temp .= $form_label_name ? '<strong>' . $form_label_name . ' : </strong>' : '<strong>' . esc_html__( 'Rich Text Editor', 'wordform-pro' ) . ' : </strong>';
				$temp .= '' !== $plain_text ? nl2br( esc_html( $plain_text ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload(
					$form_label_name,
					$plain_text,
					isset( $data['adminLabel'] ) ? $data['adminLabel'] : '',
					'richeditor',
					$raw_html
				);
			}
		}

		/**
		 * Process Range data
		 *
		 * @since 1.0.0
		 */
		public static function process_range( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Rating data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_rating( $input_data ) {
			foreach ( $input_data as $data ) {
				$label        = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$rating_value = isset( $data['values'][0] ) ? absint( $data['values'][0] ) : 0;
				$rating_scale = isset( $data['scale'] ) ? max( 3, min( 10, absint( $data['scale'] ) ) ) : 5;
				$readable     = $rating_value > 0 ? sprintf( '%1$d / %2$d', $rating_value, $rating_scale ) : '';

				$temp  = '';
				$temp .= $label ? '<strong>' . $label . ' : </strong>' : '<strong>' . esc_html__( 'Rating', 'wordform-pro' ) . ' : </strong>';
				$temp .= '' !== $readable ? esc_html( $readable ) : esc_html__( 'No rating selected', 'wordform-pro' );
				self::$sc_wordform_submission_readable_data[] = $temp;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'rating' );
			}
		}

		/**
		 * Process Likert / Matrix data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_likert( $input_data ) {
			foreach ( $input_data as $data ) {
				$label         = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$row_labels    = isset( $data['rowLabels'] ) && is_array( $data['rowLabels'] ) ? array_values( array_filter( array_map( 'sanitize_text_field', $data['rowLabels'] ) ) ) : array();
				$selected_rows = isset( $data['values'] ) && is_array( $data['values'] ) ? $data['values'] : array();
				$readable_rows = array();

				foreach ( $row_labels as $row_index => $row_label ) {
					$row_value = isset( $selected_rows[ $row_index ] ) ? sanitize_text_field( $selected_rows[ $row_index ] ) : '';
					if ( '' !== $row_value ) {
						$readable_rows[] = $row_label . ': ' . $row_value;
					}
				}

				$readable_value = implode( ' | ', $readable_rows );
				$temp           = '';
				$temp          .= $label ? '<strong>' . $label . ' : </strong>' : '<strong>' . esc_html__( 'Likert Scale', 'wordform-pro' ) . ' : </strong>';
				$temp          .= '' !== $readable_value ? esc_html( $readable_value ) : esc_html__( 'No matrix answers selected', 'wordform-pro' );
				self::$sc_wordform_submission_readable_data[] = $temp;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'likert' );
			}
		}

		/**
		 * Process Url data
		 *
		 * @since 1.0.0
		 */
		public static function process_url( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? esc_url( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? esc_url( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Time data
		 *
		 * @since 1.0.0
		 */
		public static function process_time( $input_data ) {
			foreach ( $input_data as $data ) {
				$raw_value            = self::wordform_extract_scalar_submission_value( $data['values'] ?? array() );
				$display_value        = self::wordform_format_time_value_for_display( $raw_value, $data['timeDisplayMode'] ?? '24h' );
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				$form_label_name   = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'time', $raw_value );
			}
		}

		/**
		 * Process Tel data
		 *
		 * @since 1.0.0
		 */
		public static function process_tel( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Date data
		 *
		 * @since 1.0.0
		 */
		public static function process_date( $input_data ) {
			foreach ( $input_data as $data ) {
				$raw_value            = self::wordform_extract_scalar_submission_value( $data['values'] ?? array() );
				$display_value        = self::wordform_format_date_value_for_display( $raw_value, $data['defaultDateFormat'] ?? 'yy-mm-dd' );
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'date', $raw_value );
			} // foreach
		}

		/**
		 * Process DateTime data
		 *
		 * @since 1.0.0
		 */
		public static function process_datetime( $input_data ) {
			// echo '<pre>';
			// print_r( $input_data );			
			// echo '</pre>';
			// exit();

			foreach ( $input_data as $data ) {
				$raw_value            = self::wordform_extract_scalar_submission_value( $data['values'] ?? array() );
				$display_value        = self::wordform_format_datetime_value_for_display( $raw_value, $data['defaultDateTimeFormat'] ?? 'yy-mm-dd HH:mm' );
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = '' !== $display_value ? sanitize_text_field( $display_value ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'datetime', $raw_value );
			} // foreach
		}


		/**
		 * Process Radio data
		 *
		 * @since 1.0.0
		 */
		public static function process_radio( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Checkbox data
		 *
		 * @since 1.0.0
		 */
		public static function process_checkbox( $input_data ) {
			// echo '<pre>';
			// print_r( $input_data );
			// echo '</pre>';
			// exit();

			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			} // foreach
		}

		/**
		 * Process Select data
		 *
		 * @since 1.0.0
		 */
		public static function process_select( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );

			} // foreach
		}

		/**
		 * Process Email data
		 *
		 * @since 1.0.0
		 */
		public static function process_email( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp .= isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_email( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );

			} // foreach
		}

		/**
		 * Process compound address data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_address( $input_data ) {
			foreach ( $input_data as $data ) {
				$address_values = isset( $data['values'] ) && is_array( $data['values'] ) ? array_map( 'sanitize_text_field', $data['values'] ) : array();
				$address_parts  = array_filter(
					array(
						isset( $address_values['street'] ) ? $address_values['street'] : '',
						isset( $address_values['city'] ) ? $address_values['city'] : '',
						isset( $address_values['state'] ) ? $address_values['state'] : '',
						isset( $address_values['zip'] ) ? $address_values['zip'] : '',
						isset( $address_values['country'] ) ? $address_values['country'] : '',
					)
				);

				$form_field_values = implode( ', ', $address_parts );
				$temp              = '';
				$temp             .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				$temp             .= $form_field_values;
				self::$sc_wordform_submission_readable_data[] = $temp;

				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}

		/**
		 * Process masked input data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_maskinput( $input_data ) {
			self::process_text( $input_data );
		}

		/**
		 * Process hidden field data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_hidden( $input_data ) {
			self::process_text( $input_data );
		}

		/**
		 * Process signature data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_signature( $input_data ) {
			foreach ( $input_data as $data ) {
				$temp              = '';
				$form_label_name   = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$signature_value   = isset( $data['values'][0] ) ? trim( (string) $data['values'][0] ) : '';
				$signature_present = '' !== $signature_value;
				$form_field_values = $signature_present ? __( 'Signature captured', 'wordform-pro' ) : '';

				$temp .= $form_label_name ? '<strong>' . $form_label_name . ' : </strong>' : '<strong>' . esc_html__( 'Signature', 'wordform-pro' ) . ' : </strong>';
				$temp .= $signature_present ? esc_html__( 'Signature captured', 'wordform-pro' ) : esc_html__( 'No signature provided', 'wordform-pro' );
				self::$sc_wordform_submission_readable_data[] = $temp;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '', 'signature', $signature_value );
			}
		}

		/**
		 * Process Upload data
		 *
		 * @since 1.0.0
		 */
		public static function process_upload( $input_data ) {
			// echo '<pre>';
			// print_r( $input_data );
			// echo '</pre>';
			// exit();

			foreach ( $input_data as $data ) {
				$temp  = '';
				$form_field_values = '';
				$temp .= isset( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '';
				if ( isset( $data['uploadStatus'] ) && array_filter( $data['uploadStatus'] ) ) {
					foreach( $data['uploadStatus'] as $file_key => $upload_status ) {
						if ( $upload_status === 'success' ) {
							$temp .= '<br/><a href="' . esc_url( $data['uploadFileUrl'][ $file_key ] ) . '" target="_blank">' . sanitize_text_field( $data['uploadFileName'][ $file_key ] ) . '</a>';
							$form_field_values .= esc_url( $data['uploadFileUrl'][ $file_key ] ) . PHP_EOL;
						}
					} // foreach
				}				
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';				
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );

			} // foreach
		}

		/**
		 * Process Terms data
		 *
		 * @since 1.0.0
		 */
		public static function process_terms( $input_data ) {

			// echo '<pre>';
			// print_r( $input_data );
			// echo '</pre>';
			// exit();

			foreach ( $input_data as $data ) {
				$temp  = '';
				$temp .= isset( $data['label'] ) && ! empty( $data['label'] ) ? '<strong>' . sanitize_text_field( $data['label'] ) . ' : </strong>' : '<strong>Terms : </strong>';
				$temp .= isset( $data['values'] ) && isset( $data['values'][0] ) && $data['values'][0] == 'on' ? 'Yes' : 'No';
				self::$sc_wordform_submission_readable_data[] = $temp;

				// Processed raw fields data
				$form_label_name = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : '';
				$form_field_values = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $form_label_name, $form_field_values, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );

			} // foreach
		}

		/**
		 * Process GDPR consent data.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_gdpr( $input_data ) {
			foreach ( $input_data as $data ) {
				$is_checked = isset( $data['values'] ) && isset( $data['values'][0] ) && 'on' === $data['values'][0];
				$readable   = $is_checked ? 'Yes' : 'No';
				$label      = isset( $data['label'] ) && ! empty( $data['label'] ) ? sanitize_text_field( $data['label'] ) : __( 'GDPR Consent', 'wordform-pro' );

				$temp  = '';
				$temp .= '<strong>' . $label . ' : </strong>';
				$temp .= $readable;
				self::$sc_wordform_submission_readable_data[] = $temp;

				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload(
					$label,
					$readable,
					isset( $data['adminLabel'] ) ? $data['adminLabel'] : '',
					'gdpr'
				);
			}
		}

		/**
		 * Process fixed payment items.
		 *
		 * @param array $input_data
		 */
		public static function process_payment_item( $input_data ) {
			self::process_number( $input_data );
		}

		/**
		 * Process payment coupon metadata.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_payment_coupon( $input_data ) {
			foreach ( $input_data as $data ) {
				$label            = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : 'Coupon Code';
				$coupon_code      = isset( $data['values'] ) && array_filter( $data['values'] ) ? strtoupper( sanitize_text_field( implode( ',', $data['values'] ) ) ) : '';
				$discount_amount  = isset( $data['discountAmount'] ) ? sanitize_text_field( $data['discountAmount'] ) : '';
				$readable_value   = trim( $coupon_code . ( '' !== $discount_amount && '0.00' !== $discount_amount ? ' | Discount: ' . $discount_amount : '' ), ' |' );

				self::$sc_wordform_submission_readable_data[] = '<strong>' . $label . ' : </strong>' . $readable_value;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}

		/**
		 * Process payment method selection.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_payment_method( $input_data ) {
			self::process_radio( $input_data );
		}

		/**
		 * Process custom payment amount fields.
		 *
		 * @param array $input_data
		 */
		public static function process_payment_custom_amount( $input_data ) {
			self::process_number( $input_data );
		}

		/**
		 * Process quantity fields.
		 *
		 * @param array $input_data
		 */
		public static function process_payment_quantity( $input_data ) {
			self::process_number( $input_data );
		}

		/**
		 * Process payment summary totals.
		 *
		 * @param array $input_data
		 */
		public static function process_payment_summary( $input_data ) {
			self::process_number( $input_data );
		}

		/**
		 * Process Stripe payment metadata.
		 *
		 * @param array $input_data
		 */
		public static function process_stripe( $input_data ) {
			foreach ( $input_data as $data ) {
				$label             = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : 'Stripe Payment';
				$amount            = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				$currency          = isset( $data['paymentCurrency'] ) ? sanitize_text_field( $data['paymentCurrency'] ) : '';
				$status            = isset( $data['paymentStatus'] ) ? sanitize_text_field( $data['paymentStatus'] ) : '';
				$intent_id         = isset( $data['paymentIntentId'] ) ? sanitize_text_field( $data['paymentIntentId'] ) : '';
				$readable_value    = trim( $status . ( $amount !== '' ? ' | ' . $amount . ' ' . $currency : '' ) . ( $intent_id !== '' ? ' | ' . $intent_id : '' ), ' |' );

				self::$sc_wordform_submission_readable_data[] = '<strong>' . $label . ' : </strong>' . $readable_value;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}

		/**
		 * Process PayPal payment metadata.
		 *
		 * @param array $input_data
		 */
		public static function process_paypal( $input_data ) {
			foreach ( $input_data as $data ) {
				$label          = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : 'PayPal Payment';
				$amount         = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				$currency       = isset( $data['paymentCurrency'] ) ? sanitize_text_field( $data['paymentCurrency'] ) : '';
				$status         = isset( $data['paymentStatus'] ) ? sanitize_text_field( $data['paymentStatus'] ) : '';
				$order_id       = isset( $data['orderId'] ) ? sanitize_text_field( $data['orderId'] ) : '';
				$capture_id     = isset( $data['captureId'] ) ? sanitize_text_field( $data['captureId'] ) : '';
				$readable_value = trim( $status . ( $amount !== '' ? ' | ' . $amount . ' ' . $currency : '' ) . ( $order_id !== '' ? ' | ' . $order_id : '' ) . ( $capture_id !== '' ? ' | ' . $capture_id : '' ), ' |' );

				self::$sc_wordform_submission_readable_data[] = '<strong>' . $label . ' : </strong>' . $readable_value;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}

		/**
		 * Process Square payment metadata.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_square( $input_data ) {
			foreach ( $input_data as $data ) {
				$label          = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : 'Square Payment';
				$amount         = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				$currency       = isset( $data['paymentCurrency'] ) ? sanitize_text_field( $data['paymentCurrency'] ) : '';
				$status         = isset( $data['paymentStatus'] ) ? sanitize_text_field( $data['paymentStatus'] ) : '';
				$payment_id     = isset( $data['paymentId'] ) ? sanitize_text_field( $data['paymentId'] ) : '';
				$card_brand     = isset( $data['cardBrand'] ) ? sanitize_text_field( $data['cardBrand'] ) : '';
				$last4          = isset( $data['last4'] ) ? sanitize_text_field( $data['last4'] ) : '';
				$readable_value = trim( $status . ( $amount !== '' ? ' | ' . $amount . ' ' . $currency : '' ) . ( $payment_id !== '' ? ' | ' . $payment_id : '' ) . ( $card_brand !== '' ? ' | ' . $card_brand : '' ) . ( $last4 !== '' ? ' | ****' . $last4 : '' ), ' |' );

				self::$sc_wordform_submission_readable_data[] = '<strong>' . $label . ' : </strong>' . $readable_value;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}

		/**
		 * Process Authorize.Net payment metadata.
		 *
		 * @param array $input_data
		 * @return void
		 */
		public static function process_authorizenet( $input_data ) {
			foreach ( $input_data as $data ) {
				$label           = isset( $data['label'] ) ? sanitize_text_field( $data['label'] ) : 'Authorize.Net Payment';
				$amount          = isset( $data['values'] ) && array_filter( $data['values'] ) ? sanitize_text_field( implode( ',', $data['values'] ) ) : '';
				$currency        = isset( $data['paymentCurrency'] ) ? sanitize_text_field( $data['paymentCurrency'] ) : '';
				$status          = isset( $data['paymentStatus'] ) ? sanitize_text_field( $data['paymentStatus'] ) : '';
				$transaction_id  = isset( $data['transactionId'] ) ? sanitize_text_field( $data['transactionId'] ) : '';
				$account_number  = isset( $data['accountNumber'] ) ? sanitize_text_field( $data['accountNumber'] ) : '';
				$account_type    = isset( $data['accountType'] ) ? sanitize_text_field( $data['accountType'] ) : '';
				$readable_value  = trim( $status . ( $amount !== '' ? ' | ' . $amount . ' ' . $currency : '' ) . ( $transaction_id !== '' ? ' | ' . $transaction_id : '' ) . ( $account_type !== '' ? ' | ' . $account_type : '' ) . ( $account_number !== '' ? ' | ' . $account_number : '' ), ' |' );

				self::$sc_wordform_submission_readable_data[] = '<strong>' . $label . ' : </strong>' . $readable_value;
				self::$sc_wordform_processed_forms_raw_data[] = self::build_processed_field_payload( $label, $readable_value, isset( $data['adminLabel'] ) ? $data['adminLabel'] : '' );
			}
		}


	} // End class
}

