<?php

namespace The7\Mods\Compatibility\Elementor\Pro\Modules\Dynamic_Tags\The7;

use Elementor\Element_Base;
use Elementor\Modules\DynamicTags\Module as TagsModule;

defined( 'ABSPATH' ) || exit;

class Module extends TagsModule {

	const THE7_GROUP                  = 'the7';
	const TEXT_CATEGORY_WITH_TEMPLATE = 'the7-text-with-template';

	/**
	 * @var array
	 */
	private $tag_class_names = [];

	/**
	 * Dynamic tags module constructor.
	 *
	 * Initializing Elementor dynamic tags module.
	 */
	public function __construct() {
		parent::__construct();
		if ( the7_elementor_pro_is_active() ) {
			$this->include_tags( 'the7-color' );
			$this->include_tags( 'the7-template' );

			add_action( 'elementor/frontend/before_render', [ $this, 'before_render' ] );
			add_action( 'elementor/frontend/after_render', [ $this, 'after_render' ] );
		}
	}

	/**
	 * Before frontend element render.
	 *
	 * Fires before Elementor element is rendered in the frontend.
	 *
	 * @since 2.2.0
	 *
	 * @param Element_Base $element The element.
	 */
	public function before_render( Element_Base $element ) {
		add_filter( 'the7_widget/dynamic_tags/template/should_skip', [ $this, 'should_skip_template_tag_output' ]);
	}

	/**
	 * After frontend element render.
	 *
	 * Fires after Elementor element is rendered in the frontend.
	 *
	 * @since 2.3.0
	 *
	 * @param Element_Base $this The element.
	 */
	public function after_render( Element_Base $element ) {
		remove_filter( 'the7_widget/dynamic_tags/template/should_skip', [ $this, 'should_skip_template_tag_output' ] );
	}

	public function should_skip_template_tag_output(){
		return false;
	}

	/**
	 * @param string $tag_filename Tag file name.
	 *
	 * @return void
	 */
	private function include_tags( $tag_filename ) {
		require_once __DIR__ . '/tags/' . $tag_filename . '.php';

		$class_name = str_replace( '-', '_', $tag_filename );

		$this->tag_class_names[] = ucwords( $class_name, '_' );
	}

	/**
	 * @return string
	 */
	public function get_name() {
		return 'the7-tags';
	}

	/**
	 * @return array
	 */
	public function get_tag_classes_names() {
		return $this->tag_class_names;
	}

	/**
	 * @return array[]
	 */
	public function get_groups() {
		return [
			self::THE7_GROUP => [
				'title' => __( 'The7', 'the7mk2' ),
			],
		];
	}
}
