<?php
/**
 * Shared helpers for Elementor widget controls.
 *
 * Provides option-array builders for SELECT2 dropdowns used across the
 * widget surface — vehicle/dealer post pickers and taxonomy term pickers.
 * Results are cached in a static map per request so a page with a dozen
 * widgets doesn't issue a dozen identical queries.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

use ThemeREX\AutoDealer\PostTypes\DealerPostType;
use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

class WidgetHelpers {

	/**
	 * @var array<string, array<int|string, string>>
	 */
	private static array $cache = array();

	/**
	 * Vehicle options keyed by post ID. Includes a leading `'' => 'All vehicles'`
	 * placeholder when $blank is non-empty so the widget's "render the current
	 * post" semantics are preserved.
	 *
	 * @return array<int|string, string>
	 */
	public static function vehicleOptions( string $blank = '' ): array {
		$key = 'vehicles:' . $blank;
		if ( isset( self::$cache[ $key ] ) ) {
			return self::$cache[ $key ];
		}

		$opts = '' !== $blank ? array( '' => $blank ) : array();

		$posts = get_posts(
			array(
				'post_type'      => VehiclePostType::POST_TYPE,
				'posts_per_page' => 500,
				'post_status'    => array( 'publish', 'draft', 'pending' ),
				'orderby'        => 'date',
				'order'          => 'DESC',
			)
		);

		foreach ( $posts as $post ) {
			$opts[ (string) $post->ID ] = sprintf( '#%d — %s', $post->ID, get_the_title( $post ) ?: __( '(no title)', 'trx-auto-dealer' ) );
		}

		return self::$cache[ $key ] = $opts;
	}

	/**
	 * Dealer options keyed by post ID.
	 *
	 * @return array<int|string, string>
	 */
	public static function dealerOptions( string $blank = '' ): array {
		$key = 'dealers:' . $blank;
		if ( isset( self::$cache[ $key ] ) ) {
			return self::$cache[ $key ];
		}

		$opts = '' !== $blank ? array( '' => $blank ) : array();

		$posts = get_posts(
			array(
				'post_type'      => DealerPostType::POST_TYPE,
				'posts_per_page' => 500,
				'post_status'    => array( 'publish', 'draft', 'pending' ),
				'orderby'        => 'title',
				'order'          => 'ASC',
			)
		);

		foreach ( $posts as $post ) {
			$opts[ (string) $post->ID ] = sprintf( '#%d — %s', $post->ID, get_the_title( $post ) ?: __( '(no title)', 'trx-auto-dealer' ) );
		}

		return self::$cache[ $key ] = $opts;
	}

	/**
	 * Taxonomy term options keyed by slug (so they slot directly into
	 * `FilterParams::fromArray()` which expects slugs).
	 *
	 * @return array<string, string>
	 */
	public static function termOptions( string $taxonomy, string $blank = '' ): array {
		$key = "terms:$taxonomy:$blank";
		if ( isset( self::$cache[ $key ] ) ) {
			return self::$cache[ $key ];
		}

		$opts = '' !== $blank ? array( '' => $blank ) : array();

		$terms = get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => false,
				'orderby'    => 'name',
			)
		);

		if ( ! is_wp_error( $terms ) ) {
			foreach ( $terms as $term ) {
				$opts[ $term->slug ] = $term->name;
			}
		}

		return self::$cache[ $key ] = $opts;
	}
}
