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

use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Modules\DynamicTags\Module;
use Elementor\Controls_Manager;
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
/**
 * Class Captwiki_Tag_ACF_Image
 * Returns an ACF image field as an Elementor image/media dynamic tag.
 */
class Captwiki_Tag_ACF_Image extends Data_Tag {
	/**
	 * Get tag name.
	 *
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-tag-acf-image'; }
	/**
	 * Get tag title.
	 *
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'ACF Image Field', 'captain-widgets-kit' ); }
	/**
	 * Get tag group.
	 *
	 * @return array
	 */
	public function get_group(): array {
		return array( 'captwiki-acf' ); }
	/**
	 * Get tag categories.
	 *
	 * @return array
	 */
	public function get_categories(): array {
		return array( Module::IMAGE_CATEGORY ); }
	/**
	 * Check whether settings are required.
	 *
	 * @return bool
	 */
	public function is_settings_required(): bool {
		return true; }

	/**
	 * Register tag controls.
	 *
	 * @return void
	 */
	protected function register_controls(): void {
		$this->add_control(
			'captwiki_acf_field_key',
			array(
				'label'  => esc_html__( 'ACF Image Field', 'captain-widgets-kit' ),
				'type'   => Controls_Manager::SELECT,
				'groups' => $this->captwiki_get_acf_image_groups(),
			)
		);
	}

	/**
	 * Build grouped ACF field options for image field type.
	 *
	 * @since  1.1.0
	 * @return array<int, array<string, mixed>>
	 */
	protected function captwiki_get_acf_image_groups(): array {
		if ( ! function_exists( 'acf_get_field_groups' ) ) {
			return array(); }
		$groups = array();
		foreach ( acf_get_field_groups() as $group ) {
			$fields = acf_get_fields( $group['key'] );
			if ( empty( $fields ) ) {
				continue; }
			$options = array();
			foreach ( $fields as $f ) {
				if ( 'image' === $f['type'] ) {
					$options[ $f['name'] ] = $f['label']; }
			}
			if ( $options ) {
				$groups[] = array(
					'label'   => $group['title'],
					'options' => $options,
				); }
		}
		return $groups;
	}

	/**
	 * Get tag value.
	 *
	 * @param array $options Function parameter.
	 * @return mixed
	 */
	public function get_value( array $options = array() ) {
		$key = $this->get_settings_for_display( 'captwiki_acf_field_key' );
		if ( empty( $key ) || ! function_exists( 'get_field' ) ) {
			return array(); }
		$image = ( is_tax() || is_category() || is_tag() )
			? get_field( $key, 'term_' . get_queried_object_id() )
			: get_field( $key, get_the_ID() );
		if ( is_array( $image ) && ! empty( $image['ID'] ) ) {
			return array(
				'id'  => (int) $image['ID'],
				'url' => isset( $image['url'] ) ? $image['url'] : '',
			);
		}
		if ( is_numeric( $image ) ) {
			return array(
				'id'  => (int) $image,
				'url' => wp_get_attachment_url( $image ),
			);
		}
		if ( is_string( $image ) ) {
			$image_id = attachment_url_to_postid( $image );

			return array(
				'id'  => $image_id ? $image_id : null,
				'url' => $image,
			);
		}
		return array();
	}
}
