<?php
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Post_Tags;

use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Post_Group;

if ( ! defined( 'ABSPATH' ) ) exit;

class Softech_Post_Excerpt extends Tag {

    public function get_name() {
        return 'softech-post-excerpt';
    }

    public function get_title() {
        return esc_html__( 'Post Excerpt', 'softech-addons-for-elementor' );
    }

    public function get_group() {
        return Softech_Post_Group::get_name();
    }

    public function get_categories() {
        return [ Module::TEXT_CATEGORY ];
    }


    public function register_controls() {   

    $this->add_control(
        'excerpt_length',
        [
            'label' => esc_html__('Excerpt Length', 'softech-addons-for-elementor'),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 0, 
            'max' => 20,
            'default' => ' 15',
        ]
    );

    

  
}

public function get_value( array $options = [] ) {

    $length = $this->get_settings('excerpt_length');

    // fallback if empty or invalid
    if (empty($length) || $length < 1) {
        $length = 20;
    }

    $excerpt = get_the_excerpt();

    // Split into words
    $words = explode(' ', wp_strip_all_tags($excerpt));

    // Slice according to length
    $trimmed = array_slice($words, 0, $length);

    // Return formatted excerpt
    return implode(' ', $trimmed);
}

public function render() {
    echo esc_html($this->get_value());
}


    
}
