<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Product_Short_Description extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-short-description',
			'title'         => 'Sifency Product Short Description',
			'icon'          => 'eicon-product-description',
			'category'      => array( 'sifency-addons-woo-widgets' ),
			'style_depends' => array( 'sifency-typo-style' ),
			'keywords'      => array( 'product', 'description', 'short', 'excerpt', 'woocommerce', 'sifency' ),
		);}
	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'content_style',
				'label'    => 'Content Style',
				'selector' => '.sifency-content-style',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
		$this->update_control(
			'sifency_animation_type',
			array(
				'type'    => \Elementor\Controls_Manager::HIDDEN,
				'default' => 'wow',
			)
		);
	}

	private function get_description_content() {
		if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
			$content  = '<p>' . esc_html__( 'This is a sample short description for the product. It demonstrates how animations will apply to each paragraph.', 'sifency-addons' ) . '</p>';
			$content .= '<p>' . esc_html__( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt.', 'sifency-addons' ) . '</p>';
			return $content;
		}

		$product = wc_get_product();
		if ( empty( $product ) ) {
			return '';
		}

		return $product->get_short_description();
	}
	protected function render_dom( array $context ) {
		$wrapper_id          = esc_attr( $context['dom_id'] );
		$description_content = $this->get_description_content();

		if ( empty( trim( $description_content ) ) ) {
			if ( ElementorPlugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="sifency-placeholder">' . esc_html__( 'Product Short Description', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes = array( 'sifency-product-short-description', 'sifency-content-style' );
		$wrapper_attrs   = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
				echo wp_kses_post( $description_content );
			?>
		</div>
		<?php
	}
	public function render_plain_content() {
		echo esc_html( wp_strip_all_tags( $this->get_description_content() ) );
	}
}
