<?php
/**
 * Dynamic Tag: DC Term Image
 *
 * @package captain-widgets-kit
 * @since   1.1.0
 */

use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Modules\DynamicTags\Module;
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
/**
 * Class Captwiki_Tag_DC_Term_Image
 * Returns the thumbnail image of the current term in a Dynamic Categories loop.
 * Meta key priority: tp_taxonomy_image_id → tp_taxonomy_image → thumbnail_id (WooCommerce).
 */
class Captwiki_Tag_DC_Term_Image extends Data_Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-dc-term-image'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'DC Term Image', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-dc' ); }
	/**
	 * Get tag categories.
	 *
	 * @return array
	 */
	public function get_categories(): array {
		return array( Module::IMAGE_CATEGORY, Module::MEDIA_CATEGORY ); }
	/**
	 * Register tag controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {
		$this->add_control(
			'captwiki_fallback_image',
			array(
				'label' => esc_html__( 'Fallback Image', 'captain-widgets-kit' ),
				'type'  => \Elementor\Controls_Manager::MEDIA,
			)
		);
	}
	/**
	 * Resolve term image using TPAE-compatible meta key priority.
	 *
	 * @since  1.1.0
	 * @param  \WP_Term $term Term object.
	 * @return array          [ 'id' => int, 'url' => string ]
	 */
	private function captwiki_resolve_term_image( \WP_Term $term ): array {
		// 1. TPAE attachment ID.
		$tpae_id = get_term_meta( $term->term_id, 'tp_taxonomy_image_id', true );
		if ( ! empty( $tpae_id ) && is_numeric( $tpae_id ) ) {
			$url = wp_get_attachment_image_url( (int) $tpae_id, 'full' );
			if ( $url ) {
				return array(
					'id'  => (int) $tpae_id,
					'url' => $url,
				); }
		}
		// 2. TPAE direct URL.
		$tpae_url = get_term_meta( $term->term_id, 'tp_taxonomy_image', true );
		if ( ! empty( $tpae_url ) && filter_var( $tpae_url, FILTER_VALIDATE_URL ) ) {
			return array(
				'id'  => 0,
				'url' => $tpae_url,
			);
		}
		// 3. WooCommerce thumbnail_id.
		$wc_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
		if ( ! empty( $wc_id ) && is_numeric( $wc_id ) ) {
			$url = wp_get_attachment_image_url( (int) $wc_id, 'full' );
			if ( $url ) {
				return array(
					'id'  => (int) $wc_id,
					'url' => $url,
				); }
		}
		return array(
			'id'  => 0,
			'url' => '',
		);
	}
	/**
	 * Get tag value.
	 *
	 * @param array $options Function parameter.
	 * @return mixed
	 */
	public function get_value( array $options = array() ): array {
		$term = null;
		if ( isset( $GLOBALS['captwiki_current_dc_term'] ) && $GLOBALS['captwiki_current_dc_term'] instanceof \WP_Term ) {
			$term = $GLOBALS['captwiki_current_dc_term'];
		} else {
			$queried = get_queried_object();
			if ( $queried instanceof \WP_Term ) {
				$term = $queried; }
		}
		if ( $term ) {
			$resolved = $this->captwiki_resolve_term_image( $term );
			if ( ! empty( $resolved['url'] ) ) {
				return $resolved; }
		}
		$fallback = $this->get_settings( 'captwiki_fallback_image' );
		if ( ! empty( $fallback['url'] ) ) {
			return array(
				'id'  => (int) ( isset( $fallback['id'] ) ? $fallback['id'] : 0 ),
				'url' => $fallback['url'],
			); }
		return array(
			'id'  => 0,
			'url' => \Elementor\Utils::get_placeholder_image_src(),
		);
	}
	/**
	 * Render tag output.
	 *
	 * @return void
	 */
	public function render(): void {
		$value = $this->get_value();
		echo esc_url( isset( $value['url'] ) ? $value['url'] : '' );
	}
}
