<?php
/**
 * Input Output Helper Class
 * Handles all input and output for Revio
 *
 * @since 1.3.8
 * @package RVIOR
 */

// Namespace.
namespace RVIOR\Helper;

defined('ABSPATH') || exit(1);

if ( ! class_exists(__NAMESPACE__ . '\IO') ) {
    class IO {


        private static $instance;

        public static function get_instance() {
            if ( ! self::$instance ) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        public function sanitize( $data ) {
            if ( is_array($data) ) {
                $sanitized = [];
                foreach ( $data as $key => $value ) {
                    // Sanitize array keys
                    $key             = sanitize_key($key);
                    $sanitized[ $key ] = $this->sanitize($value);
                }
                return $sanitized;
            }

            // Handle different data types
            if ( is_numeric($data) ) {
                return filter_var($data, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
            }

            if ( is_bool($data) ) {
                return (bool) $data;
            }

            if ( is_string($data) ) {
                if ( preg_match('/<svg\b[^>]*>/', $data) ) {
                    return $this->sanitize_svg($data);
                }

                // Check if the string is meant to be HTML
                if ( wp_strip_all_tags($data) !== $data ) {
                    // For HTML content, use wp_kses with appropriate allowed tags
                    return wp_kses($data, $this->get_allowed_html_tags());
                }

                // For regular strings, use sanitize_text_field
                return sanitize_text_field($data);
            }

            // Default fallback
            return sanitize_text_field( (string) $data);
        }

		/**
		 * Returns a specific input
		 *
		 * @param string $key The key to get.
		 * @param mixed  $default The default value.
		 * @return mixed
		 */
		// public function get_input( $key, $default = null ) {
		//  $input = $this->get_inputs();

		//  if ( isset( $input[ $key ] ) ) {
		//      return $input[ $key ];
		//  }

		//  return $default;
		// }

        private function sanitize_svg( $svg_content ) {
            // Replace viewBox case-insensitively
            $svg_content = preg_replace('/viewBox/i', 'viewBox', $svg_content);

            // Define allowed SVG tags and attributes
            $allowed_svg_tags = [
                'svg'    => [
                    'xmlns'               => true,
                    'viewBox'             => true,
                    'width'               => true,
                    'height'              => true,
                    'fill'                => true,
                    'preserveAspectRatio' => true,
                ],
                'g'      => [
                    'fill'         => true,
                    'transform'    => true,
                    'stroke'       => true,
                    'stroke-width' => true,
                    'style'        => true,
                ],
                'path'   => [
                    'd'         => true,
                    'fill'      => true,
                    'stroke'    => true,
                    'fill-rule' => true,
                    'clip-rule' => true,
                ],
                'rect'   => [
                    'x'      => true,
                    'y'      => true,
                    'width'  => true,
                    'height' => true,
                    'fill'   => true,
                ],
                'circle' => [
                    'cx'   => true,
                    'cy'   => true,
                    'r'    => true,
                    'fill' => true,
                ],
            ];

            // Sanitize using wp_kses
            return wp_kses($svg_content, $allowed_svg_tags);
        }

        private function get_allowed_html_tags() {
            return [
                'a'      => [
                    'href'   => true,
                    'title'  => true,
                    'class'  => true,
                    'target' => true,
                ],
                'br'     => [],
                'em'     => [],
                'strong' => [],
                'p'      => [
                    'class' => true,
                ],
                'span'   => [
                    'class' => true,
                ],
            ];
        }

        public function send_json( $success, $message = null, $data = null ) {
            $response = [
                'success' => (bool) $success,
                'message' => '',
                'data'    => '',
            ];

            if ( is_array($data) || is_object($data) ) {
                $response['data'] = $data;
            } elseif ( ! is_null($data) ) {
                $response['message'] = wp_kses_post($data);
            }

            if ( $message ) {
                $response['message'] = wp_kses_post($message);
            }

            wp_send_json($response);
            wp_die();
        }
    }
}
