<?php
/**
 * Widget Name: Mega Menu
 * Description: A responsive navigation bar with mega dropdown panels. Each menu
 *              item is either a simple link or a "mega" item whose dropdown
 *              renders a saved Elementor template — so panels can be designed
 *              freely ( columns, images, posts, anything ). Ships hover or click
 *              triggers, a mobile hamburger drawer with inline accordion panels,
 *              an optional brand / logo and five distinct visual styles. Only the
 *              selected style CSS file is loaded on the frontend.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes
 * @since      1.6.0
 */

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

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

/**
 * Class Captwiki_Mega_Menu
 *
 * @since 1.6.0
 */
class Captwiki_Mega_Menu extends Widget_Base {

	/**
	 * Get widget name.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_name(): string {
		return 'captwiki-mega-menu';
	}

	/**
	 * Get widget title.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_title(): string {
		return esc_html__( 'Mega Menu', 'captain-widgets-kit' );
	}

	/**
	 * Get widget icon.
	 *
	 * @since 1.6.0
	 * @return string
	 */
	public function get_icon(): string {
		return 'eicon-nav-menu captwiki-el-logo';
	}

	/**
	 * Get widget categories.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_categories(): array {
		return array( 'captwiki-essential' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_keywords(): array {
		return array( 'mega', 'menu', 'navigation', 'nav', 'dropdown', 'header', 'links', 'captain-widgets-kit' );
	}

	/**
	 * Frontend script dependency.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_script_depends(): array {
		return array( 'captwiki-mega-menu' );
	}

	/**
	 * Common stylesheet dependency.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	public function get_style_depends(): array {
		return array( 'captwiki_mega_menu' );
	}

	/**
	 * Disable Elementor's default inner wrapper.
	 *
	 * @since 1.6.0
	 * @return bool
	 */
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	/**
	 * Selectable visual styles.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	private function captwiki_style_options(): array {
		return array(
			'style-1' => esc_html__( 'Style 1 – Classic Bar', 'captain-widgets-kit' ),
			'style-2' => esc_html__( 'Style 2 – Pill Nav', 'captain-widgets-kit' ),
			'style-3' => esc_html__( 'Style 3 – Underline Slide', 'captain-widgets-kit' ),
			'style-4' => esc_html__( 'Style 4 – Boxed Accent', 'captain-widgets-kit' ),
			'style-5' => esc_html__( 'Style 5 – Floating Glass', 'captain-widgets-kit' ),
		);
	}

	/**
	 * Build the saved-template options list for SELECT controls.
	 *
	 * @since 1.6.0
	 * @return array
	 */
	private function captwiki_get_template_options(): array {
		$options = array( '' => esc_html__( '— Select a template —', 'captain-widgets-kit' ) );

		if ( ! class_exists( '\Elementor\Plugin' ) || ! isset( \Elementor\Plugin::$instance->templates_manager ) ) {
			return $options;
		}

		$source = \Elementor\Plugin::$instance->templates_manager->get_source( 'local' );

		if ( ! $source ) {
			return $options;
		}

		$templates = $source->get_items();

		if ( empty( $templates ) || ! is_array( $templates ) ) {
			return $options;
		}

		foreach ( $templates as $template ) {
			if ( empty( $template['template_id'] ) ) {
				continue;
			}

			$title = isset( $template['title'] ) ? $template['title'] : esc_html__( '(no title)', 'captain-widgets-kit' );
			$type  = isset( $template['type'] ) ? ' (' . $template['type'] . ')' : '';

			$options[ $template['template_id'] ] = $title . $type;
		}

		return $options;
	}

	/**
	 * Register widget controls.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	protected function register_controls(): void {
		$this->register_menu_controls();
		$this->register_brand_controls();
		$this->register_settings_controls();

		$this->register_bar_style_controls();
		$this->register_items_style_controls();
		$this->register_panel_style_controls();
		$this->register_brand_style_controls();
		$this->register_toggle_style_controls();
	}

	/**
	 * Content tab — menu items.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_menu_controls(): void {

		$this->start_controls_section(
			'section_menu',
			array(
				'label' => esc_html__( 'Menu', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'widget_style',
			array(
				'label'   => esc_html__( 'Style', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'style-1',
				'options' => $this->captwiki_style_options(),
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'item_title',
			array(
				'label'       => esc_html__( 'Title', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Menu Item', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
			)
		);

		$repeater->add_control(
			'item_type',
			array(
				'label'   => esc_html__( 'Type', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'link',
				'options' => array(
					'link' => esc_html__( 'Link', 'captain-widgets-kit' ),
					'mega' => esc_html__( 'Mega Dropdown', 'captain-widgets-kit' ),
				),
			)
		);

		$repeater->add_control(
			'item_link',
			array(
				'label'         => esc_html__( 'Link', 'captain-widgets-kit' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-link.com', 'captain-widgets-kit' ),
				'show_external' => true,
				'default'       => array( 'url' => '#' ),
				'dynamic'       => array( 'active' => true ),
			)
		);

		$repeater->add_control(
			'mega_template',
			array(
				'label'       => esc_html__( 'Dropdown Template', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => $this->captwiki_get_template_options(),
				'label_block' => true,
				'description' => esc_html__( 'Design a saved Template / Section in Elementor, then pick it here.', 'captain-widgets-kit' ),
				'condition'   => array( 'item_type' => 'mega' ),
			)
		);

		$repeater->add_control(
			'panel_width',
			array(
				'label'     => esc_html__( 'Panel Width', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'full',
				'options'   => array(
					'full' => esc_html__( 'Full Bar Width', 'captain-widgets-kit' ),
					'fit'  => esc_html__( 'Fit Content', 'captain-widgets-kit' ),
				),
				'condition' => array( 'item_type' => 'mega' ),
			)
		);

		$repeater->add_control(
			'item_icon',
			array(
				'label'       => esc_html__( 'Icon', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::ICONS,
				'skin'        => 'inline',
				'label_block' => false,
			)
		);

		$repeater->add_control(
			'item_badge',
			array(
				'label'       => esc_html__( 'Badge', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => '',
				'placeholder' => esc_html__( 'e.g. New', 'captain-widgets-kit' ),
			)
		);

		$this->add_control(
			'menu_items',
			array(
				'label'       => esc_html__( 'Menu Items', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'item_title' => esc_html__( 'Home', 'captain-widgets-kit' ) ),
					array(
						'item_title' => esc_html__( 'Products', 'captain-widgets-kit' ),
						'item_type'  => 'mega',
					),
					array(
						'item_title' => esc_html__( 'Resources', 'captain-widgets-kit' ),
						'item_type'  => 'mega',
						'panel_width' => 'fit',
					),
					array( 'item_title' => esc_html__( 'Pricing', 'captain-widgets-kit' ) ),
					array( 'item_title' => esc_html__( 'Contact', 'captain-widgets-kit' ) ),
				),
				'title_field' => '{{{ item_title }}}',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — optional brand / logo.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_brand_controls(): void {

		$this->start_controls_section(
			'section_brand',
			array(
				'label' => esc_html__( 'Brand', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'show_brand',
			array(
				'label'        => esc_html__( 'Show Brand', 'captain-widgets-kit' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'captain-widgets-kit' ),
				'label_off'    => esc_html__( 'Hide', 'captain-widgets-kit' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$this->add_control(
			'brand_image',
			array(
				'label'     => esc_html__( 'Logo', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::MEDIA,
				'dynamic'   => array( 'active' => true ),
				'condition' => array( 'show_brand' => 'yes' ),
			)
		);

		$this->add_control(
			'brand_title',
			array(
				'label'       => esc_html__( 'Brand Text', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Captain', 'captain-widgets-kit' ),
				'label_block' => true,
				'dynamic'     => array( 'active' => true ),
				'condition'   => array( 'show_brand' => 'yes' ),
			)
		);

		$this->add_control(
			'brand_link',
			array(
				'label'         => esc_html__( 'Brand Link', 'captain-widgets-kit' ),
				'type'          => Controls_Manager::URL,
				'placeholder'   => esc_html__( 'https://your-site.com', 'captain-widgets-kit' ),
				'show_external' => true,
				'default'       => array( 'url' => '#' ),
				'condition'     => array( 'show_brand' => 'yes' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Content tab — behaviour settings.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_settings_controls(): void {

		$this->start_controls_section(
			'section_settings',
			array(
				'label' => esc_html__( 'Settings', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'trigger',
			array(
				'label'   => esc_html__( 'Open Panel On', 'captain-widgets-kit' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'hover',
				'options' => array(
					'hover' => esc_html__( 'Hover', 'captain-widgets-kit' ),
					'click' => esc_html__( 'Click', 'captain-widgets-kit' ),
				),
				'description' => esc_html__( 'Mobile always uses tap to expand.', 'captain-widgets-kit' ),
			)
		);

		$this->add_responsive_control(
			'menu_align',
			array(
				'label'     => esc_html__( 'Menu Alignment', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'flex-start',
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'captain-widgets-kit' ),
						'icon'  => 'eicon-justify-start-h',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'captain-widgets-kit' ),
						'icon'  => 'eicon-justify-center-h',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'captain-widgets-kit' ),
						'icon'  => 'eicon-justify-end-h',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__menu' => 'justify-content: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'mobile_breakpoint',
			array(
				'label'      => esc_html__( 'Mobile Breakpoint', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 480,
						'max' => 1440,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 1024,
				),
				'description' => esc_html__( 'At and below this width the menu collapses to a hamburger.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — menu bar.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_bar_style_controls(): void {

		$this->start_controls_section(
			'section_bar_style',
			array(
				'label' => esc_html__( 'Menu Bar', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'accent_color',
			array(
				'label'       => esc_html__( 'Accent Color', 'captain-widgets-kit' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '',
				'selectors'   => array(
					'{{WRAPPER}} .captwiki-mega' => '--captwiki-mm-accent: {{VALUE}};',
				),
				'description' => esc_html__( 'Drives indicators, hovers and highlights.', 'captain-widgets-kit' ),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'bar_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__bar',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'bar_border',
				'label'    => esc_html__( 'Border', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__bar',
			)
		);

		$this->add_responsive_control(
			'bar_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__bar' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'bar_shadow',
				'label'    => esc_html__( 'Box Shadow', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__bar',
			)
		);

		$this->add_responsive_control(
			'bar_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__bar' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — menu items.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_items_style_controls(): void {

		$this->start_controls_section(
			'section_items_style',
			array(
				'label' => esc_html__( 'Menu Items', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

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

		$this->start_controls_tabs( 'item_state_tabs' );

		$this->start_controls_tab(
			'item_normal_tab',
			array( 'label' => esc_html__( 'Normal', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'item_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__link'     => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-mega__link svg' => 'fill: {{VALUE}};',
				),
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'item_hover_tab',
			array( 'label' => esc_html__( 'Hover / Active', 'captain-widgets-kit' ) )
		);

		$this->add_control(
			'item_color_hover',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__link:hover'         => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-mega__item.is-open .captwiki-mega__link' => 'color: {{VALUE}};',
					'{{WRAPPER}} .captwiki-mega__link:hover svg'     => 'fill: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'item_bg_hover',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__link:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

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

		$this->add_responsive_control(
			'item_gap',
			array(
				'label'      => esc_html__( 'Gap', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__menu' => 'gap: {{SIZE}}{{UNIT}};',
				),
				'separator'  => 'before',
			)
		);

		$this->add_responsive_control(
			'item_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'badge_color',
			array(
				'label'     => esc_html__( 'Badge Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__badge' => 'color: {{VALUE}};',
				),
				'separator' => 'before',
			)
		);

		$this->add_control(
			'badge_bg',
			array(
				'label'     => esc_html__( 'Badge Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__badge' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — dropdown panel.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_panel_style_controls(): void {

		$this->start_controls_section(
			'section_panel_style',
			array(
				'label' => esc_html__( 'Dropdown Panel', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'panel_background',
				'label'    => esc_html__( 'Background', 'captain-widgets-kit' ),
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__panel',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'panel_border',
				'label'    => esc_html__( 'Border', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__panel',
			)
		);

		$this->add_responsive_control(
			'panel_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__panel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'panel_shadow',
				'label'    => esc_html__( 'Box Shadow', 'captain-widgets-kit' ),
				'selector' => '{{WRAPPER}} .captwiki-mega__panel',
			)
		);

		$this->add_responsive_control(
			'panel_padding',
			array(
				'label'      => esc_html__( 'Padding', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__panel-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'panel_fit_width',
			array(
				'label'      => esc_html__( 'Fit-Content Min Width', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 160,
						'max' => 600,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__item--fit .captwiki-mega__panel' => 'min-width: {{SIZE}}{{UNIT}};',
				),
				'description' => esc_html__( 'Applies to items using the Fit Content panel width.', 'captain-widgets-kit' ),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — brand.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_brand_style_controls(): void {

		$this->start_controls_section(
			'section_brand_style',
			array(
				'label'     => esc_html__( 'Brand', 'captain-widgets-kit' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_brand' => 'yes' ),
			)
		);

		$this->add_responsive_control(
			'brand_logo_height',
			array(
				'label'      => esc_html__( 'Logo Height', 'captain-widgets-kit' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 16,
						'max' => 120,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .captwiki-mega__brand-logo' => 'height: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'brand_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__brand-text' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'brand_typography',
				'selector' => '{{WRAPPER}} .captwiki-mega__brand-text',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Style tab — mobile toggle ( hamburger ).
	 *
	 * @since 1.6.0
	 * @return void
	 */
	private function register_toggle_style_controls(): void {

		$this->start_controls_section(
			'section_toggle_style',
			array(
				'label' => esc_html__( 'Mobile Toggle', 'captain-widgets-kit' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'toggle_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__toggle-bar' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'toggle_bg',
			array(
				'label'     => esc_html__( 'Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega__toggle' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'drawer_bg',
			array(
				'label'     => esc_html__( 'Mobile Drawer Background', 'captain-widgets-kit' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '',
				'selectors' => array(
					'{{WRAPPER}} .captwiki-mega.is-mobile .captwiki-mega__nav' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Capture an Elementor icon as an HTML string.
	 *
	 * @since 1.6.0
	 * @param array $icon Icon control value.
	 * @return string
	 */
	private function captwiki_get_icon_html( array $icon ): string {
		ob_start();
		Icons_Manager::render_icon( $icon, array( 'aria-hidden' => 'true' ) );
		return (string) ob_get_clean();
	}

	/**
	 * Render the optional brand block.
	 *
	 * @since 1.6.0
	 * @param array $settings Widget settings.
	 * @return void
	 */
	private function captwiki_render_brand( array $settings ): void {
		if ( 'yes' !== $settings['show_brand'] ) {
			return;
		}

		$has_logo = ! empty( $settings['brand_image']['url'] );
		$text     = trim( (string) $settings['brand_title'] );

		if ( ! $has_logo && '' === $text ) {
			return;
		}

		$url    = ! empty( $settings['brand_link']['url'] ) ? $settings['brand_link']['url'] : '';
		$target = ! empty( $settings['brand_link']['is_external'] ) ? ' target="_blank"' : '';
		$rel    = ! empty( $settings['brand_link']['nofollow'] ) ? ' rel="nofollow"' : '';
		$tag    = '' !== $url ? 'a' : 'span';
		$href   = '' !== $url ? ' href="' . esc_url( $url ) . '"' : '';

		echo '<' . esc_attr( $tag ) . ' class="captwiki-mega__brand"' . $href . $target . $rel . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( $has_logo ) {
			printf(
				'<img class="captwiki-mega__brand-logo" src="%1$s" alt="%2$s" />',
				esc_url( $settings['brand_image']['url'] ),
				esc_attr( $text )
			);
		}

		if ( '' !== $text ) {
			echo '<span class="captwiki-mega__brand-text">' . esc_html( $text ) . '</span>';
		}

		echo '</' . esc_attr( $tag ) . '>';
	}

	/**
	 * Render a single menu item ( link or mega dropdown ).
	 *
	 * @since 1.6.0
	 * @param array $item  Repeater item.
	 * @param int   $index Item index ( for unique panel ids ).
	 * @return void
	 */
	private function captwiki_render_item( array $item, int $index ): void {
		$title = isset( $item['item_title'] ) ? trim( (string) $item['item_title'] ) : '';

		if ( '' === $title ) {
			return;
		}

		$is_mega   = isset( $item['item_type'] ) && 'mega' === $item['item_type'];
		$template  = $is_mega && ! empty( $item['mega_template'] ) ? absint( $item['mega_template'] ) : 0;
		$has_panel = $is_mega && $template > 0;
		$fit       = $has_panel && isset( $item['panel_width'] ) && 'fit' === $item['panel_width'];

		$item_classes = 'captwiki-mega__item';

		if ( $has_panel ) {
			$item_classes .= ' captwiki-mega__item--mega';
			$item_classes .= $fit ? ' captwiki-mega__item--fit' : ' captwiki-mega__item--full';
		}

		$panel_id = 'captwiki-mega-panel-' . $this->get_id() . '-' . $index;

		echo '<li class="' . esc_attr( $item_classes ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		$url    = ! empty( $item['item_link']['url'] ) ? $item['item_link']['url'] : '#';
		$target = ! empty( $item['item_link']['is_external'] ) ? ' target="_blank"' : '';
		$rel    = ! empty( $item['item_link']['nofollow'] ) ? ' rel="nofollow"' : '';

		$aria = '';

		if ( $has_panel ) {
			$aria = ' aria-haspopup="true" aria-expanded="false" aria-controls="' . esc_attr( $panel_id ) . '"';
		}

		echo '<a class="captwiki-mega__link" href="' . esc_url( $url ) . '"' . $target . $rel . $aria . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( ! empty( $item['item_icon']['value'] ) ) {
			echo '<span class="captwiki-mega__item-icon" aria-hidden="true">' . $this->captwiki_get_icon_html( $item['item_icon'] ) . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		echo '<span class="captwiki-mega__item-text">' . esc_html( $title ) . '</span>';

		if ( ! empty( $item['item_badge'] ) ) {
			echo '<span class="captwiki-mega__badge">' . esc_html( $item['item_badge'] ) . '</span>';
		}

		if ( $has_panel ) {
			echo '<span class="captwiki-mega__caret" aria-hidden="true"></span>';
		}

		echo '</a>';

		if ( $has_panel ) {
			echo '<div class="captwiki-mega__panel" id="' . esc_attr( $panel_id ) . '" role="region">';
			echo '<div class="captwiki-mega__panel-inner">';

			// Elementor returns fully-rendered, escaped builder markup for the template.
			echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template, true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

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

		echo '</li>';
	}

	/**
	 * Render widget output on the frontend.
	 *
	 * @since 1.6.0
	 * @return void
	 */
	protected function render(): void {
		$settings = $this->get_settings_for_display();

		if ( empty( $settings['menu_items'] ) || ! is_array( $settings['menu_items'] ) ) {
			return;
		}

		$allowed_styles = array_keys( $this->captwiki_style_options() );
		$style          = in_array( $settings['widget_style'], $allowed_styles, true ) ? $settings['widget_style'] : 'style-1';

		// Load only the active style stylesheet, depending on the common one.
		wp_enqueue_style(
			'captwiki-mega-menu-' . $style,
			CAPTWIKI_URL . 'assets/css/widgets/mega-menu/captwiki-mega-menu-' . $style . '.css',
			array( 'captwiki_mega_menu' ),
			CAPTWIKI_VERSION
		);

		$trigger    = 'click' === $settings['trigger'] ? 'click' : 'hover';
		$breakpoint = isset( $settings['mobile_breakpoint']['size'] ) ? absint( $settings['mobile_breakpoint']['size'] ) : 1024;
		$breakpoint = $breakpoint > 0 ? $breakpoint : 1024;
		$nav_id     = 'captwiki-mega-nav-' . $this->get_id();

		printf(
			'<div class="captwiki-mega captwiki-%1$s captwiki-mega--%2$s" data-trigger="%2$s" data-breakpoint="%3$d">',
			esc_attr( $style ),
			esc_attr( $trigger ),
			esc_attr( $breakpoint )
		);

		echo '<div class="captwiki-mega__bar">';

		$this->captwiki_render_brand( $settings );

		printf(
			'<button class="captwiki-mega__toggle" type="button" aria-expanded="false" aria-controls="%1$s" aria-label="%2$s"><span class="captwiki-mega__toggle-bar"></span><span class="captwiki-mega__toggle-bar"></span><span class="captwiki-mega__toggle-bar"></span></button>',
			esc_attr( $nav_id ),
			esc_attr__( 'Toggle menu', 'captain-widgets-kit' )
		);

		echo '<nav class="captwiki-mega__nav" id="' . esc_attr( $nav_id ) . '" aria-label="' . esc_attr__( 'Main menu', 'captain-widgets-kit' ) . '">';
		echo '<ul class="captwiki-mega__menu">';

		$index = 0;

		foreach ( $settings['menu_items'] as $item ) {
			$this->captwiki_render_item( (array) $item, $index );
			++$index;
		}

		echo '</ul>';
		echo '</nav>';
		echo '</div>'; // bar.
		echo '</div>'; // mega.
	}
}
