<?php
/**
 * Favorites List widget — displays the visitor's favorited vehicles.
 *
 * Free tier: localStorage-only, hydrated client-side. The server emits a
 * placeholder shell that JS populates from `trx_ad_favorites`. Pro tier
 * filters `trx_ad_favorites_button` to swap in REST-backed CRUD.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Frontend\FavoritesHandler;

class FavoritesListWidget extends Widget_Base {

	public function get_name(): string {
		return 'trx_ad_favorites_list';
	}

	public function get_title(): string {
		return __( 'Favorites List', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		return 'eicon-heart';
	}

	public function get_categories(): array {
		return array( 'trx-auto-dealer' );
	}

	public function get_keywords(): array {
		return array( 'favorites', 'wishlist', 'saved' );
	}

	public function get_style_depends(): array {
		return array( 'trx-ad-frontend' );
	}

	public function get_script_depends(): array {
		return array( 'trx-ad-frontend' );
	}

	protected function register_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Content', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'heading',
			array(
				'label'   => __( 'Heading', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::TEXT,
				'default' => __( 'My Favorites', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'empty_message',
			array(
				'label'   => __( 'Empty State Message', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::TEXTAREA,
				'default' => __( 'You have no favorites yet. Tap the heart on any vehicle to save it.', 'trx-auto-dealer' ),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style',
			array(
				'label' => __( 'Style', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => __( 'Heading Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-favorites-page__heading' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .trx-ad-favorites-page__heading',
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$heading  = (string) ( $settings['heading'] ?? '' );
		$empty    = (string) ( $settings['empty_message'] ?? '' );

		// FavoritesHandler::renderPage() emits the section + heading + grid +
		// empty-state slots that frontend.js' hydrateFavoritesPage targets.
		// Customize via post-process replacement so the data-* selectors stay.
		$html = FavoritesHandler::renderPage();
		if ( '' !== $heading ) {
			$html = preg_replace(
				'/(<h1[^>]*class="trx-ad-favorites-page__heading"[^>]*>)[^<]*(<\\/h1>)/',
				'$1' . esc_html( $heading ) . '$2',
				$html,
				1
			);
		}
		if ( '' !== $empty ) {
			$html = preg_replace(
				'/(<p[^>]*data-trx-ad-favorites-empty[^>]*>)[^<]*(<\\/p>)/',
				'$1' . esc_html( $empty ) . '$2',
				$html,
				1
			);
		}

		echo wp_kses( $html, \ThemeREX\AutoDealer\allowedHtml() );
	}
}
