<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

use AuraBuilder\Document\Node;
use AuraBuilder\Widgets\WidgetBase;

final class AlertWidget extends WidgetBase {
	public function get_type(): string { return 'alert'; }
	public function get_title(): string { return 'Alerta'; }
	public function get_icon(): string { return 'alert'; }
	public function get_defaults(): array {
		return array(
			'title'   => 'Atenção',
			'content' => 'Mensagem de alerta importante.',
			'type'    => 'info',
		);
	}
	public function sanitize_settings( array $settings ): array {
		$type = sanitize_key( (string) ( $settings['type'] ?? 'info' ) );
		if ( ! in_array( $type, array( 'info', 'success', 'warning', 'danger' ), true ) ) {
			$type = 'info';
		}
		return array(
			'title'   => sanitize_text_field( (string) ( $settings['title'] ?? '' ) ),
			'content' => sanitize_textarea_field( (string) ( $settings['content'] ?? '' ) ),
			'type'    => $type,
		);
	}
	public function render( Node $node, array $context = array() ): string {
		$s    = $node->settings;
		$type = esc_attr( (string) ( $s['type'] ?? 'info' ) );
		return '<div class="ab-alert ab-alert--' . $type . '" role="alert">'
			. '<strong class="ab-alert__title">' . esc_html( (string) ( $s['title'] ?? '' ) ) . '</strong>'
			. '<div class="ab-alert__body">' . esc_html( (string) ( $s['content'] ?? '' ) ) . '</div>'
			. '</div>';
	}
}
