<?php
namespace PixelioCartAbandonRecoveryMore\Admin\Sanitizers;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

class Pixelio_Cart_Abandon_Recovery_More_Request_Sanitizer
{
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;

	public function __construct(Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper)
	{
		$this->wordpress_wrapper = $wordpress_wrapper;
	}

	public function pixelio_sanitize_request(array $input): array
	{

		return [
			'id' => isset($input['id']) ? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($input['id'])) : null,

			'template_name' => isset($input['template_name'])
				? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($input['template_name']))
				: '',

			'language' => isset($input['language'])
				? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($input['language']))
				: 'fr',

			'subject' => isset($input['subject'])
				? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($input['subject']))
				: '',

			'type_of_email' => isset($input['type_of_email'])
				? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($input['type_of_email']))
				: '',

			'body_html' => isset($input['body_html'])
				? $this->wordpress_wrapper->pixelio_wp_kses(
					$this->wordpress_wrapper->pixelio_wp_unslash($input['body_html']),
					Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_cart_abandon_recovery_more_allowed_html
				)
				: '',

			'variables' => isset($input['variables']) && is_array($input['variables'])
				? array_map([$this->wordpress_wrapper, 'pixelio_sanitize_text_field'], $this->wordpress_wrapper->pixelio_wp_unslash($input['variables']))
				: [],

			'is_active' => !empty($input['is_active']) ? 1 : 0,
			'send_after' => isset($input['send_after']) ? (int)$input['send_after'] : 0,
			'discount' => isset($input['discount']) ? (int)$input['discount'] : 0,
			'discount_type' => isset($input['discount_type']) ? (int)$input['discount_type'] : 0,
			'discount_expiration_time' => isset($input['discount_expiration_time']) ? (int)$input['discount_expiration_time'] : 0,
			'generated_by_ia' => !empty($input['generated_by_ia']) ? 1 : 0,
			'synced' => isset($input['synced']) ? (int)$input['synced'] : 0,
		];
	}
}
