<?php
namespace HTMega_Inline_Mega_Menu_Pro;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;

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

/**
 * "Menu Badge" hide/style controls for the Inline Mega Menu widget — a
 * pro-only convenience. The badge itself (menu-item meta field, save,
 * render via htmega_walker_menu_badge_markup()) is fully free and
 * unaffected by this class; only the hide toggle and style section are
 * pro-exclusive, injected into the free widget's own control stack via
 * Elementor's per-element hooks.
 */
class HTMegaInlineMegaMenuPro_Elementor {

    private static $_instance = null;
    public static function instance() {
        if ( is_null( self::$_instance ) ) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function __construct() {
        add_action( 'elementor/element/htmega-menu-inline-menu/section_menu_options/before_section_end', [ $this, 'inject_badge_toggle' ] );
        add_action( 'elementor/element/htmega-menu-inline-menu/dorpdown_icon_style/after_section_end', [ $this, 'add_badge_style_section' ] );
    }

    /**
     * Appends the hide-badge controls at the end of the free widget's
     * "Menu" content section (right after its last control, dropdown_icon).
     */
    public function inject_badge_toggle( $element ) {
        $element->add_responsive_control(
            'menu_badge',
            [
                'label' => esc_html__( 'Hide Menu Badge', 'htmega-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => 'none',
                'default' => 'block',
                'selectors'  => array(
                    '{{WRAPPER}} .htmenu-menu-tag' => 'display: {{VALUE}}',
                ),
            ]
        );
        $element->add_responsive_control(
            'menu_badge_arrow',
            [
                'label' => esc_html__( 'Hide Menu Badge Arrow', 'htmega-pro' ),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => 'none',
                'default' => 'block',
                'selectors'  => array(
                    '{{WRAPPER}} .htmega-arrow-down-icon' => 'display: {{VALUE}}',
                ),
                'condition' => [
                    'menu_badge!' => 'none',
                ]
            ]
        );
    }

    /**
     * Adds a new "Menu Badge" style section right after the free widget's
     * dropdown-icon style section.
     */
    public function add_badge_style_section( $element ) {
        $element->start_controls_section(
            'menu_badge_style',
            array(
                'label'      => esc_html__( 'Menu Badge', 'htmega-pro' ),
                'tab'        => Controls_Manager::TAB_STYLE,
                'condition'  => [
                    'menu_badge!' => 'none',
                ]
            )
        );
        $element->add_group_control(
            Group_Control_Typography::get_type(),
            array(
                'name'     => 'menu_badge_typography',
                'selector' => '{{WRAPPER}} .htmenu-menu-tag',
                'condition' => [
                    'menu_badge!' => 'none',
                ]
            )
        );
        $element->add_responsive_control(
            'menu_badge_position_v',
            [
                'label' => esc_html__( 'Vertical Position', 'htmega-pro' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px'],
                'range' => [
                    'px' => [
                        'min' => -100,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .htmenu-menu-tag' => 'top: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $element->add_responsive_control(
            'menu_badge_position_h',
            [
                'label' => esc_html__( 'Horizontal Position', 'htmega-pro' ),
                'type' => Controls_Manager::SLIDER,
                'size_units' => [ 'px'],
                'range' => [
                    'px' => [
                        'min' => -100,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .htmenu-menu-tag' => 'right: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $element->add_responsive_control(
            'menu_badge_padding',
            array(
                'label'      => esc_html__( 'Padding', 'htmega-pro' ),
                'type'       => Controls_Manager::DIMENSIONS,
                'selectors'  => array(
                    '{{WRAPPER}} .htmenu-menu-tag' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ),
            )
        );
        $element->add_responsive_control(
            'menu_badge_border_radius',
            [
                'label' => esc_html__( 'Border Radius', 'htmega-pro' ),
                'type' => Controls_Manager::DIMENSIONS,
                'selectors' => [
                    '{{WRAPPER}} .htmenu-menu-tag' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;',
                ],
            ]
        );
        $element->end_controls_section();
    }
}

HTMegaInlineMegaMenuPro_Elementor::instance();
