<?php
/**
 * Class: Product_Featured_Gallery
 *
 * @return créer un tableau d'ID des images des produits vedettes
 * @since 2.2.2
 */

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_Gallery extends Data_Tag {
	use \EACCustomWidgets\Includes\Traits\Product_Trait;

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

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

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

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

	protected function register_controls(): void {
		$this->add_control(
			'woo_featured',
			array(
				'label'   => esc_html__( 'Product count', 'eac-components' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 50,
				'step'    => 1,
				'default' => 4,
			)
		);

		$this->register_product_term_control();
	}

	public function get_value( array $options = array() ): array {
		$limit  = ! empty( $this->get_settings( 'woo_featured' ) ) ? $this->get_settings( 'woo_featured' ) : 10;
		$termid = $this->get_settings( 'product_category' );
		$value  = array();

		$products = wc_get_products(
			array(
				'post_status' => 'publish',
				'limit'       => absint( $limit ),
				'orderby'     => 'name',
				'order'       => 'ASC',
				'parent'      => 0,
				'include'     => wc_get_featured_product_ids(),
			)
		);

		if ( ! is_wp_error( $products ) && ! empty( $products ) ) {
			foreach ( $products as $product ) {
				$product_id = $product->get_id();
				if ( ! empty( $termid ) && ! has_term( $termid, 'product_cat', $product_id ) ) {
					continue;
				}

				$thumb_id = $product->get_image_id();
				if ( $thumb_id ) {
					$value[] = array( 'id' => $thumb_id . '::featured::' . $product_id );
				}
			}
		}
		return $value;
	}
}
