<?php

namespace Limb_Chatbot\Includes\Widgets\Data;

class Greeting_Widget_Config extends Abstract_Widget_Config {
	public function sanitize( $data ): array {
		$data = is_array( $data ) ? $data : [];
		$mode = $this->sanitize_string( $data['mode'] ?? 'daytime_based', 'daytime_based' );
		if ( ! in_array( $mode, [ 'daytime_based', 'custom' ], true ) ) {
			$mode = 'daytime_based';
		}

		$daytime_based = [];
		if ( isset( $data['daytime_based'] ) && is_array( $data['daytime_based'] ) ) {
			foreach ( $data['daytime_based'] as $hour => $message ) {
				$daytime_based[ (string) (int) $hour ] = $this->sanitize_textarea( $message );
			}
		}

		return [
			'mode'          => $mode,
			'daytime_based' => $daytime_based,
			'custom'        => $this->sanitize_textarea( $data['custom'] ?? '' ),
			'sub_greeting'  => $this->sanitize_textarea( $data['sub_greeting'] ?? '' ),
			'tagline'       => $this->sanitize_textarea( $data['tagline'] ?? '' ),
		];
	}

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

	public function get_translatable_fields(): array {
		return [
			[ 'path' => 'custom', 'type' => 'string' ],
			[ 'path' => 'sub_greeting', 'type' => 'string' ],
			[ 'path' => 'tagline', 'type' => 'string' ],
			[ 'path' => 'daytime_based.*', 'type' => 'string' ],
		];
	}
}
