<?php

namespace Limb_Chatbot\Includes\Widgets\Data;

use Limb_Chatbot\Includes\Services\Helper;

class Header_Widget_Config extends Abstract_Widget_Config {
	public function sanitize( $data ): array {
		$data = is_array( $data ) ? $data : [];

		$raw_logo = $this->sanitize_string( $data['logo'] ?? 'monogram', 'monogram' );
		$logo     = in_array( $raw_logo, [ 'monogram', 'custom_logo', 'none' ], true ) ? $raw_logo : 'monogram';

		$custom_logo_input = is_array( $data['custom_logo'] ?? null ) ? $data['custom_logo'] : [];
		$custom_logo = $this->sanitize_custom_logo( $custom_logo_input );

		return [
			'logo'                           => $logo,
			'custom_logo'                    => $custom_logo,
			'company_name'                   => $this->sanitize_string( $data['company_name'] ?? '' ),
			'show_company_name'              => $this->sanitize_bool( $data['show_company_name'] ?? true, true ),
			'show_ai_avatar'                 => $this->sanitize_bool( $data['show_ai_avatar'] ?? true, true ),
			'show_active_live_agents_avatar' => $this->sanitize_bool( $data['show_active_live_agents_avatar'] ),
		];
	}

	private function sanitize_custom_logo( array $data ): array {
		$type = isset( $data['type'] ) && $data['type'] === 'svg' ? 'svg' : 'image';

		return [
			'type'  => $type,
			'image' => $this->sanitize_url_or_empty( $data['image'] ?? '' ),
			'svg'   => Helper::sanitize_svg( (string) ( $data['svg'] ?? '' ) ),
		];
	}

	public function validate( $data ): bool {
		return is_array( $data );
	}

	public function get_translatable_fields(): array {
		return [
			[ 'path' => 'company_name', 'type' => 'string' ],
		];
	}
}
