<?php 
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Image_Tags;
use Elementor\Core\DynamicTags\Data_Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Image_Group;
if ( ! defined( 'ABSPATH' ) ) exit;
class Softech_Feature_Image extends Data_Tag {
    public function get_name() {
        return 'softech-feature-image';
    }
    public function get_title() {
        return esc_html__( 'Featured Image', 'softech-addons-for-elementor' );
    }
    public function get_group() {
        return Softech_Image_Group::get_name();
    }
    public function get_categories() {
        return [ Module::IMAGE_CATEGORY ];
    }
    public function get_value( array $options = [] ) {
        $post_id = get_the_ID();
        if ( ! $post_id ) {
            return $this->get_placeholder_image();
        }
        $image_id = get_post_thumbnail_id( $post_id );
        if ( ! $image_id ) {
            return $this->get_placeholder_image();
        }

        // Get the image size from widget options, fallback to 'large'
        $size = ! empty( $options['image_size'] ) ? $options['image_size'] : 'large';

        // Handle custom dimensions if size is set to 'custom'
        if ( 'custom' === $size ) {
            $custom_dimension = ! empty( $options['image_custom_dimension'] ) 
                ? $options['image_custom_dimension'] 
                : [ 'width' => 800, 'height' => 600 ];

            $size = [
                absint( $custom_dimension['width'] ),
                absint( $custom_dimension['height'] ),
            ];
        }

        $image_url = wp_get_attachment_image_url( $image_id, $size );
        if ( ! $image_url ) {
            return $this->get_placeholder_image();
        }
        return [
            'id'  => $image_id,
            'url' => $image_url,
        ];
    }
    /**
     * Returns a placeholder image array when no featured image is available.
     *
     * @return array
     */
    private function get_placeholder_image() {
        $placeholder_url = \Elementor\Utils::get_placeholder_image_src();
        return [
            'id'  => 0,
            'url' => $placeholder_url,
        ];
    }
}