<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class TextPathWidget extends WidgetBase {
	public function get_type(): string { return 'text_path'; }
	public function get_title(): string { return 'Texto em caminho'; }
	public function get_icon(): string { return 'text_path'; }
	public function get_defaults(): array {
		return array(
			'text'  => 'Aura Builder · Texto em caminho',
			'align' => 'center',
		);
	}
	public function sanitize_settings( array $settings ): array {
		$align = in_array( ( $settings['align'] ?? '' ), array( 'left', 'center', 'right' ), true ) ? (string) $settings['align'] : 'center';
		return array(
			'text'  => sanitize_text_field( (string) ( $settings['text'] ?? '' ) ),
			'align' => $align,
		);
	}
	public function render( Node $node, array $context = array() ): string {
		$text  = (string) ( $node->settings['text'] ?? '' );
		$align = esc_attr( (string) ( $node->settings['align'] ?? 'center' ) );
		$id    = 'abpath_' . preg_replace( '/[^a-zA-Z0-9_]/', '', $node->id );
		// Simple circular path SVG.
		return '<div class="ab-text-path" style="text-align:' . $align . '">'
			. '<svg viewBox="0 0 200 200" class="ab-text-path__svg" width="200" height="200">'
			. '<defs><path id="' . esc_attr( $id ) . '" d="M100,100 m-75,0 a75,75 0 1,1 150,0 a75,75 0 1,1 -150,0"/></defs>'
			. '<text class="ab-text-path__text"><textPath href="#' . esc_attr( $id ) . '" startOffset="0%">' . esc_html( $text ) . '</textPath></text>'
			. '</svg></div>';
	}
}
