<?php
/**
 * Class: Featured_Image
 *
 * @return l'url et l'id de l'image en avant (Featured image) de l'article courant
 * pour charger/afficher cette image
 * @since 1.6.0
 */

namespace EACCustomWidgets\Includes\Elementor\DynamicTags\Tags;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Utils;
use Elementor\Modules\DynamicTags\Module as TagsModule;

class Featured_Image extends Data_Tag {
	public function get_name(): string {
		return 'eac-addon-featured';
	}

	public function get_title(): string {
		return esc_html__( 'Featured image', 'eac-components' );
	}

	public function get_group(): array {
		return array( 'eac-post' );
	}

	public function get_categories(): array {
		return array(
			TagsModule::IMAGE_CATEGORY,
			TagsModule::MEDIA_CATEGORY,
		);
	}

	protected function register_controls(): void {
		$this->add_control(
			'fallback',
			array(
				'label' => esc_html__( 'Fallback', 'eac-components' ),
				'type'  => Controls_Manager::MEDIA,
			)
		);
	}

	public function get_value( array $options = array() ): array {
		$id = get_post_thumbnail_id();

		if ( $id && 0 !== $id ) {
			$url = wp_get_attachment_image_src( $id, 'full' )[0];
		} else {
			return $this->get_settings( 'fallback' );
		}

		return array(
			'id'  => $id,
			'url' => $url,
		);
	}
}
