<?php

namespace SolaceExtraAddons\Modules\DynamicTags\Tags;

use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module as TagsModule;
use SolaceExtraAddons\Inc\Helper\Solace_Extra_Addons_Helper;

if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly
}

class Solace_Extra_Taxonomies_Url extends Tag
{

	public function get_name()
	{
		return 'solace-extra-tax-url';
	}

	public function get_title()
	{
		return esc_html__('Taxonomies URL', 'solace-extra' );
	}

	public function get_group()
	{
		return 'URL';
	}

	public function get_categories()
	{
		return [
			TagsModule::URL_CATEGORY
		];
	}

	public function get_categories_list()
	{

		$items = [
			'' => esc_html__('Select...', 'solace-extra' ),
		];

		$categories = Solace_Extra_Addons_Helper::solace_extra_post_types_category_slug();
		foreach ($categories as $category_slug => $post_type_name) {
			$terms = get_categories(array('taxonomy' => $category_slug));
			foreach ($terms as $term) {
				$items[$term->term_id] = $post_type_name . ' - ' . $term->name;
			}
		}

		return $items;
	}

	public function is_settings_required()
	{
		return true;
	}

	protected function register_controls()
	{
		$this->add_control(
			'key',
			[
				'label'   => esc_html__('Categories URL', 'solace-extra' ),
				'type'    => Controls_Manager::SELECT,
				'options' => $this->get_categories_list(),
				'default' => ''
			]
		);
	}

	protected function get_category_url()
	{
		if ($key = $this->get_settings('key')) {
			return get_category_link($key);
		}

		return '';
	}

	public function get_value(array $options = [])
	{
		return $this->get_category_url();
	}

	public function render()
	{
		echo wp_kses_post($this->get_category_url());
	}
}
