<?php
/**
 * Class: Product_Featured_Image
 *
 * @return affiche l'image du produit
 * @since 1.9.8
 */

namespace EACCustomWidgets\Includes\Woocommerce\DynamicTags\Tags;

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

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

class Product_Featured_Image extends Data_Tag {
	use \EACCustomWidgets\Includes\Traits\Product_Trait;

	public function get_name(): string {
		return 'eac-addon-woo-image';
	}

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

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

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

	protected function register_controls(): void {
		$this->register_product_id_control();
	}

	public function get_value( array $options = array() ): array {
		$product_id = $this->get_settings( 'product_id' );

		if ( empty( $product_id ) ) {
			return array();
		}

		$product = wc_get_product( $product_id );
		if ( ! $product ) {
			return array();    }

		$image_id = $product->get_image_id();
		if ( ! $image_id ) {
			return array();
		}

		$image_url = wp_get_attachment_image_url( $image_id, 'full' );

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