<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class HtmlWidget extends WidgetBase {

	public function get_type(): string {
		return 'html';
	}

	public function get_title(): string {
		return 'HTML';
	}

	public function get_category(): string {
		return 'advanced';
	}

	public function get_icon(): string {
		return 'code';
	}

	public function get_defaults(): array {
		return array( 'code' => '' );
	}

	public function get_controls(): array {
		return array(
			array( 'type' => 'textarea', 'name' => 'code', 'label' => 'HTML' ),
		);
	}

	public function sanitize_settings( array $settings ): array {
		$code = (string) ( $settings['code'] ?? '' );
		// Drop pure CSS dumps on save (import leftovers); keep real HTML.
		return array(
			'code' => \AuraBuilder\Support\Assets::sanitize_user_html( $code, false ),
		);
	}

	public function render( Node $node, array $context = array() ): string {
		$code = (string) ( $node->settings['code'] ?? '' );
		// Front: extract <style> → real stylesheet; never show CSS as text.
		// Editor: same strip so canvas does not dump CSS source.
		$enqueue = empty( $context['editor'] );
		$code    = \AuraBuilder\Support\Assets::sanitize_user_html( $code, $enqueue );
		if ( $code === '' && ! empty( $context['editor'] ) ) {
			return '<div class="ab-img-ph">HTML vazio</div>';
		}
		if ( $code === '' ) {
			return '';
		}
		return '<div class="ab-html">' . $code . '</div>';
	}
}
