<?php
/**
 * Class: Product_Gallery_Images
 *
 * @return créer un tableau d'ID des images d'un 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_Gallery_Images extends Data_Tag {
	use \EACCustomWidgets\Includes\Traits\Product_Trait;

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

	public function get_title(): string {
		return esc_html__( 'Product 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->register_product_id_control();

		$this->add_control(
			'eac_woo_gallery_thumb',
			array(
				'label'        => esc_html__( 'Add product image', 'eac-components' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'eac-components' ),
				'label_off'    => esc_html__( 'No', 'eac-components' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);
	}

	public function get_value( array $options = array() ): array {
		$product_id     = $this->get_settings( 'product_id' );
		$settings_thumb = 'yes' === $this->get_settings( 'eac_woo_gallery_thumb' ) ? true : false;
		$value          = array();

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

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

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

		$attachment_ids = $product->get_gallery_image_ids();
		foreach ( $attachment_ids as $attachment_id ) {
			$value[] = array( 'id' => $attachment_id . '::product::' . $product->get_id() );
		}
		return $value;
	}
}
