<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class RootWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Raiz (página)';
	}

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

	public function is_container(): bool {
		return true;
	}

	public function get_defaults(): array {
		return array(
			'content_width' => 'full', // full | wide | content
			'max_width'     => '1200px',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$cw = isset( $settings['content_width'] ) ? sanitize_key( (string) $settings['content_width'] ) : 'full';
		if ( ! in_array( $cw, array( 'full', 'wide', 'content' ), true ) ) {
			$cw = 'full';
		}
		$max = isset( $settings['max_width'] ) ? sanitize_text_field( (string) $settings['max_width'] ) : '1200px';
		if ( $max !== '' && ! preg_match( '/%|px|rem|em|vw|vh|ch$/i', $max ) && is_numeric( $max ) ) {
			$max .= 'px';
		}
		// Preserve extra keys (gap, display_name, …) — do not strip to only width fields.
		$out                   = $settings;
		$out['content_width']  = $cw;
		$out['max_width']      = $max !== '' ? $max : '1200px';
		if ( isset( $out['gap'] ) ) {
			$out['gap'] = sanitize_text_field( (string) $out['gap'] );
		}
		if ( isset( $out['display_name'] ) ) {
			$out['display_name'] = sanitize_text_field( (string) $out['display_name'] );
		}
		return $out;
	}

	public function render( Node $node, array $context = array() ): string {
		return '';
	}
}
