<?php
/**
 * Global post categories widget.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Elementor\Widget_Base;

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

class Post_Categories extends Widget_Base {
	use Post_Context;

	public function get_name(): string {
		return 'impact_widgets_elementor_post-categories';
	}

	public function get_title(): string {
		return esc_html__( 'Post Categories', 'impact-widgets-for-elementor' );
	}

	public function get_icon(): string {
		return 'eicon-folder-o';
	}

	public function get_categories(): array {
		return array( 'impact-widgets-elementor' );
	}

	public function get_keywords(): array {
		return array( 'blog', 'post', 'category', 'categories', 'taxonomy', 'archive' );
	}

	public function get_style_depends(): array {
		return array( 'impact_widgets_elementor_post-widgets' );
	}

	protected function register_controls(): void {
		$this->register_content_controls();
		$this->register_card_style_controls();
		$this->register_heading_style_controls();
		$this->register_item_style_controls();
		$this->register_count_style_controls();
		$this->register_arrow_style_controls();
	}

	private function register_content_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => esc_html__( 'Categories', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'show_heading',
			array(
				'label'        => esc_html__( 'Show Heading', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'Hide', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'heading',
			array(
				'label'     => esc_html__( 'Heading', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'Categories', 'impact-widgets-for-elementor' ),
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_control(
			'heading_tag',
			array(
				'label'     => esc_html__( 'Heading HTML Tag', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'h3',
				'options'   => array(
					'h2'  => 'H2',
					'h3'  => 'H3',
					'h4'  => 'H4',
					'h5'  => 'H5',
					'div' => 'div',
				),
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_control(
			'category_source',
			array(
				'label'   => esc_html__( 'Show Categories', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'all',
				'options' => array(
					'all'      => esc_html__( 'All Categories', 'impact-widgets-for-elementor' ),
					'selected' => esc_html__( 'Selected Categories', 'impact-widgets-for-elementor' ),
					'current'  => esc_html__( 'Current Post Categories', 'impact-widgets-for-elementor' ),
				),
			)
		);

		$this->add_control(
			'selected_categories',
			array(
				'label'       => esc_html__( 'Select Categories', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => $this->get_category_options(),
				'multiple'    => true,
				'label_block' => true,
				'condition'   => array(
					'category_source' => 'selected',
				),
			)
		);

		$this->add_control(
			'hide_empty',
			array(
				'label'        => esc_html__( 'Hide Empty Categories', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Yes', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'No', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'condition'    => array(
					'category_source!' => 'current',
				),
			)
		);

		$this->add_control(
			'orderby',
			array(
				'label'     => esc_html__( 'Order By', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'name',
				'options'   => array(
					'name'  => esc_html__( 'Name', 'impact-widgets-for-elementor' ),
					'count' => esc_html__( 'Post Count', 'impact-widgets-for-elementor' ),
					'id'    => esc_html__( 'Category ID', 'impact-widgets-for-elementor' ),
					'slug'  => esc_html__( 'Slug', 'impact-widgets-for-elementor' ),
				),
				'condition' => array(
					'category_source!' => 'selected',
				),
			)
		);

		$this->add_control(
			'order',
			array(
				'label'     => esc_html__( 'Order', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'ASC',
				'options'   => array(
					'ASC'  => esc_html__( 'Ascending', 'impact-widgets-for-elementor' ),
					'DESC' => esc_html__( 'Descending', 'impact-widgets-for-elementor' ),
				),
				'condition' => array(
					'category_source!' => 'selected',
				),
			)
		);

		$this->add_control(
			'show_count',
			array(
				'label'        => esc_html__( 'Show Post Count', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'Hide', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_arrow',
			array(
				'label'        => esc_html__( 'Show Arrow', 'impact-widgets-for-elementor' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'impact-widgets-for-elementor' ),
				'label_off'    => esc_html__( 'Hide', 'impact-widgets-for-elementor' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'arrow_icon',
			array(
				'label'     => esc_html__( 'Arrow Icon', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::ICONS,
				'default'   => array(
					'value'   => 'fas fa-chevron-right',
					'library' => 'fa-solid',
				),
				'condition' => array(
					'show_arrow' => 'yes',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_card_style_controls(): void {
		$this->start_controls_section(
			'section_card_style',
			array(
				'label' => esc_html__( 'Card', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'card_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .iaew-post-categories',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'card_border',
				'selector' => '{{WRAPPER}} .iaew-post-categories',
			)
		);

		$this->add_responsive_control(
			'card_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'top'      => 8,
					'right'    => 8,
					'bottom'   => 8,
					'left'     => 8,
					'unit'     => 'px',
					'isLinked' => true,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'card_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'default'    => array(
					'top'      => 28,
					'right'    => 20,
					'bottom'   => 10,
					'left'     => 20,
					'unit'     => 'px',
					'isLinked' => false,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'card_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-post-categories',
			)
		);

		$this->end_controls_section();
	}

	private function register_heading_style_controls(): void {
		$this->start_controls_section(
			'section_heading_style',
			array(
				'label'     => esc_html__( 'Heading', 'impact-widgets-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_heading' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'heading_typography',
				'selector' => '{{WRAPPER}} .iaew-post-categories__heading',
			)
		);

		$this->add_control(
			'heading_color',
			array(
				'label'     => esc_html__( 'Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__heading' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'heading_alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__heading' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'heading_spacing',
			array(
				'label'      => esc_html__( 'Bottom Spacing', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 24,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__heading' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	private function register_item_style_controls(): void {
		$this->start_controls_section(
			'section_item_style',
			array(
				'label' => esc_html__( 'Category Items', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'top'      => 18,
					'right'    => 0,
					'bottom'   => 18,
					'left'     => 0,
					'unit'     => 'px',
					'isLinked' => false,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'divider_color',
			array(
				'label'     => esc_html__( 'Divider Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#edf0f4',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__item:not(:last-child)' => 'border-bottom-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'divider_style',
			array(
				'label'     => esc_html__( 'Divider Style', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'solid',
				'options'   => array(
					'solid'  => esc_html__( 'Solid', 'impact-widgets-for-elementor' ),
					'double' => esc_html__( 'Double', 'impact-widgets-for-elementor' ),
					'dotted' => esc_html__( 'Dotted', 'impact-widgets-for-elementor' ),
					'dashed' => esc_html__( 'Dashed', 'impact-widgets-for-elementor' ),
				),
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__item:not(:last-child)' => 'border-bottom-style: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'divider_width',
			array(
				'label'      => esc_html__( 'Divider Width', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 10,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 1,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__item:not(:last-child)' => 'border-bottom-width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'item_typography',
				'selector' => '{{WRAPPER}} .iaew-post-categories__name',
			)
		);

		$this->start_controls_tabs( 'item_color_tabs' );

		$this->start_controls_tab(
			'item_normal_tab',
			array(
				'label' => esc_html__( 'Normal', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'item_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'item_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'item_hover_tab',
			array(
				'label' => esc_html__( 'Hover', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'item_hover_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover, {{WRAPPER}} .iaew-post-categories__link:focus' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'item_hover_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover, {{WRAPPER}} .iaew-post-categories__link:focus' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();
	}

	private function register_count_style_controls(): void {
		$this->start_controls_section(
			'section_count_style',
			array(
				'label'     => esc_html__( 'Post Count', 'impact-widgets-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_count' => 'yes',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'count_typography',
				'selector' => '{{WRAPPER}} .iaew-post-categories__count',
			)
		);

		$this->start_controls_tabs( 'count_color_tabs' );

		$this->start_controls_tab(
			'count_normal_tab',
			array(
				'label' => esc_html__( 'Normal', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'count_color',
			array(
				'label'     => esc_html__( 'Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#667085',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__count' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'count_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#f4f5f7',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__count' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'count_border',
				'selector' => '{{WRAPPER}} .iaew-post-categories__count',
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'count_hover_tab',
			array(
				'label' => esc_html__( 'Hover', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'count_hover_color',
			array(
				'label'     => esc_html__( 'Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__count, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__count' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'count_hover_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__count, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__count' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'count_hover_border_color',
			array(
				'label'     => esc_html__( 'Border Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__count, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__count' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control(
			'count_min_width',
			array(
				'label'      => esc_html__( 'Minimum Width', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 48,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__count' => 'min-width: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'count_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'top'      => 7,
					'right'    => 12,
					'bottom'   => 7,
					'left'     => 12,
					'unit'     => 'px',
					'isLinked' => false,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__count' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'count_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'default'    => array(
					'top'      => 50,
					'right'    => 50,
					'bottom'   => 50,
					'left'     => 50,
					'unit'     => 'px',
					'isLinked' => true,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__count' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'count_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-post-categories__count',
			)
		);

		$this->end_controls_section();
	}

	private function register_arrow_style_controls(): void {
		$this->start_controls_section(
			'section_arrow_style',
			array(
				'label'     => esc_html__( 'Arrow', 'impact-widgets-for-elementor' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array(
					'show_arrow' => 'yes',
				),
			)
		);

		$this->add_control(
			'arrow_color',
			array(
				'label'     => esc_html__( 'Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#111827',
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'color: {{VALUE}};',
					'{{WRAPPER}} .iaew-post-categories__arrow svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'arrow_background',
			array(
				'label'     => esc_html__( 'Background Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'arrow_hover_color',
			array(
				'label'     => esc_html__( 'Hover Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__arrow, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__arrow' => 'color: {{VALUE}};',
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__arrow svg, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__arrow svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'arrow_hover_background',
			array(
				'label'     => esc_html__( 'Hover Background', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-post-categories__link:hover .iaew-post-categories__arrow, {{WRAPPER}} .iaew-post-categories__link:focus .iaew-post-categories__arrow' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'arrow_size',
			array(
				'label'      => esc_html__( 'Size', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 6,
						'max' => 50,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 14,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .iaew-post-categories__arrow svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'arrow_spacing',
			array(
				'label'      => esc_html__( 'Left Spacing', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'default'    => array(
					'unit' => 'px',
					'size' => 14,
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'margin-left: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'arrow_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'arrow_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-post-categories__arrow' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings    = $this->get_settings_for_display();
		$categories  = $this->get_display_categories( $settings );
		$heading_tag = in_array( $settings['heading_tag'], array( 'h2', 'h3', 'h4', 'h5', 'div' ), true ) ? $settings['heading_tag'] : 'h3';

		echo '<div class="iaew-post-categories">';

		if ( 'yes' === $settings['show_heading'] && '' !== trim( (string) $settings['heading'] ) ) {
			printf(
				'<%1$s class="iaew-post-categories__heading">%2$s</%1$s>',
				esc_attr( $heading_tag ),
				esc_html( $settings['heading'] )
			);
		}

		if ( empty( $categories ) ) {
			echo '<div class="iaew-post-categories__empty">' . esc_html__( 'No categories found.', 'impact-widgets-for-elementor' ) . '</div>';
			echo '</div>';
			return;
		}

		echo '<ul class="iaew-post-categories__list">';

		foreach ( $categories as $category ) {
			$link = get_category_link( $category->term_id );

			if ( is_wp_error( $link ) ) {
				continue;
			}

			echo '<li class="iaew-post-categories__item">';
			printf(
				'<a class="iaew-post-categories__link" href="%1$s"><span class="iaew-post-categories__name">%2$s</span><span class="iaew-post-categories__meta">',
				esc_url( $link ),
				esc_html( $category->name )
			);

			if ( 'yes' === $settings['show_count'] ) {
				printf(
					'<span class="iaew-post-categories__count" aria-label="%1$s">%2$s</span>',
					esc_attr(
						sprintf(
							/* translators: %s: number of posts. */
							_n( '%s post', '%s posts', (int) $category->count, 'impact-widgets-for-elementor' ),
							number_format_i18n( $category->count )
						)
					),
					esc_html( number_format_i18n( $category->count ) )
				);
			}

			if ( 'yes' === $settings['show_arrow'] ) {
				echo '<span class="iaew-post-categories__arrow" aria-hidden="true">';
				Icons_Manager::render_icon( $settings['arrow_icon'] );
				echo '</span>';
			}

			echo '</span></a></li>';
		}

		echo '</ul></div>';
	}

	private function get_category_options(): array {
		$options = array();
		$terms   = get_categories(
			array(
				'hide_empty' => false,
				'orderby'    => 'name',
				'order'      => 'ASC',
			)
		);

		foreach ( $terms as $term ) {
			$options[ (string) $term->term_id ] = $term->name;
		}

		return $options;
	}

	private function get_display_categories( array $settings ): array {
		$source = in_array( $settings['category_source'], array( 'all', 'selected', 'current' ), true ) ? $settings['category_source'] : 'all';

		if ( 'selected' === $source ) {
			$selected = isset( $settings['selected_categories'] ) && is_array( $settings['selected_categories'] ) ? array_filter( array_map( 'absint', $settings['selected_categories'] ) ) : array();

			if ( empty( $selected ) ) {
				return array();
			}

			$terms = get_categories(
				array(
					'include'    => $selected,
					'orderby'    => 'include',
					'hide_empty' => 'yes' === $settings['hide_empty'],
				)
			);

			return is_array( $terms ) ? $terms : array();
		}

		if ( 'current' === $source ) {
			$post = $this->iaew_get_blog_context_post();

			if ( ! $post ) {
				return array();
			}

			$terms = get_the_category( $post->ID );
			return is_array( $terms ) ? $terms : array();
		}

		$orderby = in_array( $settings['orderby'], array( 'name', 'count', 'id', 'slug' ), true ) ? $settings['orderby'] : 'name';
		$order   = 'DESC' === $settings['order'] ? 'DESC' : 'ASC';
		$terms   = get_categories(
			array(
				'hide_empty' => 'yes' === $settings['hide_empty'],
				'orderby'    => $orderby,
				'order'      => $order,
			)
		);

		return is_array( $terms ) ? $terms : array();
	}
}
