<?php
if (!defined('ABSPATH')) {
    exit;
}

if (!did_action('elementor/loaded') || !class_exists('\Elementor\Widget_Base')) {
    return;
}

if (!class_exists('NEXDIRECTORY_Elementor_Widget_Shortcode')) {
    class NEXDIRECTORY_Elementor_Widget_Shortcode extends \Elementor\Widget_Base {
        private $nexdirectory_config = [];

        public function __construct($data = [], $args = null) {
            if (is_array($args) && isset($args['nexdirectory_config']) && is_array($args['nexdirectory_config'])) {
                $this->nexdirectory_config = $args['nexdirectory_config'];
            }
            if (!is_array($args)) {
                $args = [];
            }
            if (!is_array($data)) {
                $data = [];
            }
            parent::__construct($data, $args);
        }

        public function set_nexdirectory_config(array $config = []) {
            $this->nexdirectory_config = $config;
            return $this;
        }

        public function get_name() {
            return (string) ($this->nexdirectory_config['name'] ?? 'nexdirectory_widget');
        }

        public function get_title() {
            return (string) ($this->nexdirectory_config['title'] ?? __('Events Widget', 'nexdirectory'));
        }

        public function get_icon() {
            return (string) ($this->nexdirectory_config['icon'] ?? 'eicon-calendar');
        }

        public function get_categories() {
            return ['nexdirectory-widgets'];
        }

        protected function register_controls() {
            $controls = isset($this->nexdirectory_config['controls']) && is_array($this->nexdirectory_config['controls']) ? $this->nexdirectory_config['controls'] : [];
            if (empty($controls)) {
                return;
            }
            $this->start_controls_section('nexdirectory_widget_controls', [
                'label' => __('Widget Settings', 'nexdirectory'),
            ]);
            foreach ($controls as $key => $control) {
                if (!is_array($control)) {
                    continue;
                }
                $this->add_control($key, $control);
            }
            $this->end_controls_section();
        }

        protected function render() {
            if (!function_exists('nexdirectory_get_plugin_instance')) {
                return;
            }
            $plugin = nexdirectory_get_plugin_instance();
            if (!$plugin || !method_exists($plugin, 'render_shortcode_tag')) {
                return;
            }
            $shortcode_tag = (string) ($this->nexdirectory_config['shortcode'] ?? '');
            if ($shortcode_tag === '') {
                return;
            }
            $settings = $this->get_settings_for_display();
            $atts_map = isset($this->nexdirectory_config['atts']) && is_array($this->nexdirectory_config['atts']) ? $this->nexdirectory_config['atts'] : [];
            $atts = [];
            foreach ($atts_map as $att_key => $setting_key) {
                if (!is_string($setting_key) || $setting_key === '') {
                    continue;
                }
                if (!array_key_exists($setting_key, $settings)) {
                    continue;
                }
                $value = $settings[$setting_key];
                if (is_bool($value)) {
                    $value = $value ? 'yes' : 'no';
                } elseif (is_array($value)) {
                    if (isset($value['url'])) {
                        $value = esc_url_raw((string) $value['url']);
                    } elseif (isset($value['value'])) {
                        $value = (string) $value['value'];
                    } else {
                        $value = wp_json_encode($value);
                    }
                } else {
                    $value = (string) $value;
                }
                $atts[$att_key] = $value;
            }
            $output = $plugin->render_shortcode_tag($shortcode_tag, $atts);
            echo wp_kses((string) $output, NEXDIRECTORY_Escaping::allowed_html());
        }
    }
}

if (!function_exists('nexdirectory_register_elementor_widgets')) {
    function nexdirectory_register_elementor_widgets($widgets_manager) {
        if (!is_object($widgets_manager) || !method_exists($widgets_manager, 'register')) {
            return;
        }

        $select_yes_no = [
            'type' => \Elementor\Controls_Manager::SELECT,
            'options' => [
                'yes' => __('Yes', 'nexdirectory'),
                'no' => __('No', 'nexdirectory'),
            ],
            'default' => 'yes',
        ];
        $template_options = [
            '1' => __('Template 1 - Classic', 'nexdirectory'),
            '2' => __('Template 2 - Minimal', 'nexdirectory'),
            '3' => __('Template 3 - Editorial', 'nexdirectory'),
            '4' => __('Template 4 - Split', 'nexdirectory'),
            '5' => __('Template 5 - Soft', 'nexdirectory'),
            '6' => __('Template 6 - Bold', 'nexdirectory'),
        ];
        $query_controls_common = [
            'title' => ['label' => __('Widget Title', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT],
            'subtitle' => ['label' => __('Widget Subtitle', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'rows' => 2],
            'limit' => ['label' => __('Result Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 60, 'default' => 9],
            'orderby' => [
                'label' => __('Order By', 'nexdirectory'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'start',
                'options' => [
                    'start' => __('Start Date', 'nexdirectory'),
                    'date' => __('Published Date', 'nexdirectory'),
                    'title' => __('Title', 'nexdirectory'),
                    'modified' => __('Modified Date', 'nexdirectory'),
                    'rand' => __('Random', 'nexdirectory'),
                ],
            ],
            'order' => [
                'label' => __('Sort Direction', 'nexdirectory'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'ASC',
                'options' => [
                    'ASC' => __('Ascending', 'nexdirectory'),
                    'DESC' => __('Descending', 'nexdirectory'),
                ],
            ],
            'search' => ['label' => __('Keyword Query', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('ai summit', 'nexdirectory')],
            'event_type' => ['label' => __('Event Type Slugs', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('conference,workshop', 'nexdirectory')],
            'event_mode' => ['label' => __('Event Mode Slugs', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('in-person,online-stream-event', 'nexdirectory')],
            'event_tag' => ['label' => __('Event Tag Slugs', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('ai,manufacturing', 'nexdirectory')],
            'event_price' => ['label' => __('Price Range Slugs', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('free,paid', 'nexdirectory')],
            'city' => ['label' => __('City Contains', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('London', 'nexdirectory')],
            'date_from' => ['label' => __('From Date (YYYY-MM-DD)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => gmdate('Y-m-d')],
            'official' => [
                'label' => __('Official Status', 'nexdirectory'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => '',
                'options' => [
                    '' => __('Any', 'nexdirectory'),
                    '1' => __('Official Only', 'nexdirectory'),
                    '0' => __('Unofficial Only', 'nexdirectory'),
                ],
            ],
            'include_past' => array_merge($select_yes_no, ['label' => __('Include Past Listings', 'nexdirectory'), 'default' => 'no']),
            'custom_taxonomy' => ['label' => __('Custom Taxonomy Slug', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('event_topic', 'nexdirectory')],
            'custom_terms' => ['label' => __('Custom Taxonomy Term Slugs', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => __('robotics,energy', 'nexdirectory')],
            'tax_relation' => [
                'label' => __('Taxonomy Relation', 'nexdirectory'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'AND',
                'options' => [
                    'AND' => __('AND (match all)', 'nexdirectory'),
                    'OR' => __('OR (match any)', 'nexdirectory'),
                ],
            ],
        ];
        $style_controls_common = [
            'columns_desktop' => ['label' => __('Columns Desktop', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 6, 'default' => 3],
            'columns_tablet' => ['label' => __('Columns Tablet', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 4, 'default' => 2],
            'columns_mobile' => ['label' => __('Columns Mobile', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 2, 'default' => 1],
            'gap' => ['label' => __('Card Gap (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 64, 'default' => 24],
            'radius' => ['label' => __('Card Radius (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 48, 'default' => 18],
            'image_height' => ['label' => __('Image Height (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 120, 'max' => 520, 'default' => 220],
            'accent_color' => ['label' => __('Accent Color', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
            'bg_color' => ['label' => __('Card Background', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
            'text_color' => ['label' => __('Text Color', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
            'custom_class' => ['label' => __('Custom CSS Class', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT],
            'custom_css' => ['label' => __('Custom CSS (use {{WRAPPER}})', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'rows' => 8],
        ];
        $query_atts_common = [
            'title' => 'title',
            'subtitle' => 'subtitle',
            'limit' => 'limit',
            'orderby' => 'orderby',
            'order' => 'order',
            'search' => 'search',
            'event_type' => 'event_type',
            'event_mode' => 'event_mode',
            'event_tag' => 'event_tag',
            'event_price' => 'event_price',
            'city' => 'city',
            'date_from' => 'date_from',
            'official' => 'official',
            'include_past' => 'include_past',
            'custom_taxonomy' => 'custom_taxonomy',
            'custom_terms' => 'custom_terms',
            'tax_relation' => 'tax_relation',
        ];
        $style_atts_common = [
            'columns_desktop' => 'columns_desktop',
            'columns_tablet' => 'columns_tablet',
            'columns_mobile' => 'columns_mobile',
            'gap' => 'gap',
            'radius' => 'radius',
            'image_height' => 'image_height',
            'accent_color' => 'accent_color',
            'bg_color' => 'bg_color',
            'text_color' => 'text_color',
            'custom_class' => 'custom_class',
            'custom_css' => 'custom_css',
        ];

        $widgets = [
            [
                'name' => 'nexdirectory_events_archive',
                'title' => __('Events Archive', 'nexdirectory'),
                'icon' => 'eicon-post-list',
                'shortcode' => 'nexdirectory_events_archive',
                'atts' => ['posts_per_page' => 'posts_per_page', 'show_sidebar' => 'show_sidebar'],
                'controls' => [
                    'posts_per_page' => ['label' => __('Posts Per Page', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 100, 'default' => 12],
                    'show_sidebar' => array_merge(['label' => __('Show Filters Sidebar', 'nexdirectory')], $select_yes_no),
                ],
            ],
            [
                'name' => 'nexdirectory_events_map',
                'title' => __('Events Map', 'nexdirectory'),
                'icon' => 'eicon-google-maps',
                'shortcode' => 'nexdirectory_events_map',
                'atts' => [
                    'max_events' => 'max_events',
                    'show_sidebar' => 'show_sidebar',
                    'show_search' => 'show_search',
                    'show_type' => 'show_type',
                    'show_mode' => 'show_mode',
                    'show_tag' => 'show_tag',
                    'show_price' => 'show_price',
                    'show_official' => 'show_official',
                    'show_date' => 'show_date',
                    'show_distance' => 'show_distance',
                    'show_list' => 'show_list',
                    'map_height' => 'map_height',
                    'view' => 'view',
                    'density' => 'density',
                ],
                'controls' => [
                    'max_events' => ['label' => __('Max Events', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 1000, 'default' => 200],
                    'map_height' => ['label' => __('Map Height (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 280, 'max' => 1400, 'default' => 620],
                    'view' => [
                        'label' => __('View', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => '',
                        'options' => [
                            '' => __('Use Global Setting', 'nexdirectory'),
                            'default' => __('Map Default View', 'nexdirectory'),
                            'triple' => __('Triple View', 'nexdirectory'),
                        ],
                    ],
                    'density' => [
                        'label' => __('Results Density', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => '',
                        'options' => [
                            '' => __('Use Global Setting', 'nexdirectory'),
                            'comfortable' => __('Comfortable', 'nexdirectory'),
                            'compact' => __('Compact', 'nexdirectory'),
                        ],
                    ],
                    'show_sidebar' => array_merge(['label' => __('Show Filter Sidebar', 'nexdirectory')], $select_yes_no),
                    'show_list' => array_merge(['label' => __('Show Results List/Grid', 'nexdirectory')], $select_yes_no),
                    'show_search' => array_merge(['label' => __('Show Search Filter', 'nexdirectory')], $select_yes_no),
                    'show_type' => array_merge(['label' => __('Show Event Type Filter', 'nexdirectory')], $select_yes_no),
                    'show_mode' => array_merge(['label' => __('Show Event Mode Filter', 'nexdirectory')], $select_yes_no),
                    'show_tag' => array_merge(['label' => __('Show Tag Filter', 'nexdirectory')], $select_yes_no),
                    'show_price' => array_merge(['label' => __('Show Price Filter', 'nexdirectory')], $select_yes_no),
                    'show_official' => array_merge(['label' => __('Show Official Filter', 'nexdirectory')], $select_yes_no),
                    'show_date' => array_merge(['label' => __('Show Date Filter', 'nexdirectory')], $select_yes_no),
                    'show_distance' => array_merge(['label' => __('Show Distance Filter', 'nexdirectory')], $select_yes_no),
                ],
            ],
            [
                'name' => 'nexdirectory_events_calendar',
                'title' => __('Events Calendar', 'nexdirectory'),
                'icon' => 'eicon-calendar',
                'shortcode' => 'nexdirectory_events_calendar',
                'atts' => [],
                'controls' => [],
            ],
            [
                'name' => 'nexdirectory_events_query_grid',
                'title' => __('Events Query Grid', 'nexdirectory'),
                'icon' => 'eicon-posts-grid',
                'shortcode' => 'nexdirectory_events_query_grid',
                'atts' => array_merge($query_atts_common, $style_atts_common, [
                    'template' => 'template',
                    'show_excerpt' => 'show_excerpt',
                    'show_meta' => 'show_meta',
                    'excerpt_length' => 'excerpt_length',
                ]),
                'controls' => array_merge($query_controls_common, [
                    'template' => [
                        'label' => __('Grid Template', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => '1',
                        'options' => $template_options,
                    ],
                    'show_excerpt' => array_merge(['label' => __('Show Excerpt', 'nexdirectory')], $select_yes_no),
                    'show_meta' => array_merge(['label' => __('Show Date/Location Meta', 'nexdirectory')], $select_yes_no),
                    'excerpt_length' => ['label' => __('Excerpt Length (words)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 8, 'max' => 70, 'default' => 20],
                ], $style_controls_common),
            ],
            [
                'name' => 'nexdirectory_events_bento_grid',
                'title' => __('Events Bento Grid', 'nexdirectory'),
                'icon' => 'eicon-gallery-grid',
                'shortcode' => 'nexdirectory_events_bento_grid',
                'atts' => array_merge($query_atts_common, $style_atts_common, [
                    'template' => 'template',
                    'show_excerpt' => 'show_excerpt',
                    'show_meta' => 'show_meta',
                    'excerpt_length' => 'excerpt_length',
                    'hover_effect' => 'hover_effect',
                ]),
                'controls' => array_merge($query_controls_common, [
                    'template' => [
                        'label' => __('Bento Template', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => '1',
                        'options' => $template_options,
                    ],
                    'hover_effect' => [
                        'label' => __('Hover Effect', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => 'lift',
                        'options' => [
                            'lift' => __('Lift', 'nexdirectory'),
                            'zoom' => __('Zoom', 'nexdirectory'),
                            'glow' => __('Glow', 'nexdirectory'),
                            'tilt' => __('Tilt', 'nexdirectory'),
                        ],
                    ],
                    'show_excerpt' => array_merge(['label' => __('Show Excerpt', 'nexdirectory')], $select_yes_no),
                    'show_meta' => array_merge(['label' => __('Show Date/Location Meta', 'nexdirectory')], $select_yes_no),
                    'excerpt_length' => ['label' => __('Excerpt Length (words)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 8, 'max' => 70, 'default' => 18],
                ], $style_controls_common),
            ],
            [
                'name' => 'nexdirectory_events_ajax_search',
                'title' => __('Events AJAX Search', 'nexdirectory'),
                'icon' => 'eicon-search',
                'shortcode' => 'nexdirectory_events_ajax_search',
                'atts' => [
                    'title' => 'title',
                    'subtitle' => 'subtitle',
                    'template' => 'template',
                    'limit' => 'limit',
                    'min_chars' => 'min_chars',
                    'search_placeholder' => 'search_placeholder',
                    'orderby' => 'orderby',
                    'order' => 'order',
                    'search' => 'search',
                    'event_type' => 'event_type',
                    'event_mode' => 'event_mode',
                    'event_tag' => 'event_tag',
                    'event_price' => 'event_price',
                    'city' => 'city',
                    'date_from' => 'date_from',
                    'official' => 'official',
                    'custom_taxonomy' => 'custom_taxonomy',
                    'custom_terms' => 'custom_terms',
                    'tax_relation' => 'tax_relation',
                    'include_past' => 'include_past',
                    'show_type' => 'show_type',
                    'show_mode' => 'show_mode',
                    'show_tag' => 'show_tag',
                    'show_price' => 'show_price',
                    'show_city' => 'show_city',
                    'show_date' => 'show_date',
                    'show_official' => 'show_official',
                    'show_excerpt' => 'show_excerpt',
                    'show_meta' => 'show_meta',
                    'excerpt_length' => 'excerpt_length',
                    'gap' => 'gap',
                    'radius' => 'radius',
                    'accent_color' => 'accent_color',
                    'bg_color' => 'bg_color',
                    'text_color' => 'text_color',
                    'custom_class' => 'custom_class',
                    'custom_css' => 'custom_css',
                ],
                'controls' => array_merge($query_controls_common, [
                    'template' => [
                        'label' => __('Search Template', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => '1',
                        'options' => $template_options,
                    ],
                    'min_chars' => ['label' => __('Min Search Characters', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 8, 'default' => 2],
                    'search_placeholder' => ['label' => __('Search Placeholder', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => __('Search listings by keyword...', 'nexdirectory')],
                    'show_type' => array_merge(['label' => __('Show Type Filter', 'nexdirectory')], $select_yes_no),
                    'show_mode' => array_merge(['label' => __('Show Mode Filter', 'nexdirectory')], $select_yes_no),
                    'show_tag' => array_merge(['label' => __('Show Tag Filter', 'nexdirectory')], $select_yes_no),
                    'show_price' => array_merge(['label' => __('Show Price Filter', 'nexdirectory')], $select_yes_no),
                    'show_city' => array_merge(['label' => __('Show City Filter', 'nexdirectory')], $select_yes_no),
                    'show_date' => array_merge(['label' => __('Show Date Filter', 'nexdirectory')], $select_yes_no),
                    'show_official' => array_merge($select_yes_no, ['label' => __('Show Official Filter', 'nexdirectory'), 'default' => 'no']),
                    'show_excerpt' => array_merge($select_yes_no, ['label' => __('Show Excerpt in Results', 'nexdirectory'), 'default' => 'no']),
                    'show_meta' => array_merge(['label' => __('Show Date/Location Meta', 'nexdirectory')], $select_yes_no),
                    'excerpt_length' => ['label' => __('Excerpt Length (words)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 8, 'max' => 70, 'default' => 14],
                    'gap' => ['label' => __('Row Gap (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 40, 'default' => 14],
                    'radius' => ['label' => __('Row Radius (px)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 40, 'default' => 14],
                    'accent_color' => ['label' => __('Accent Color', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
                    'bg_color' => ['label' => __('Row Background', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
                    'text_color' => ['label' => __('Text Color', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::COLOR],
                    'custom_class' => ['label' => __('Custom CSS Class', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXT],
                    'custom_css' => ['label' => __('Custom CSS (use {{WRAPPER}})', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'rows' => 8],
                ]),
            ],
            [
                'name' => 'nexdirectory_submit_event',
                'title' => __('Submit Listing Form', 'nexdirectory'),
                'icon' => 'eicon-form-horizontal',
                'shortcode' => 'nexdirectory_submit_event',
                'atts' => ['show_nav' => 'show_nav'],
                'controls' => ['show_nav' => array_merge(['label' => __('Show Section Nav', 'nexdirectory')], $select_yes_no)],
            ],
            [
                'name' => 'nexdirectory_my_events',
                'title' => __('My Listings', 'nexdirectory'),
                'icon' => 'eicon-table',
                'shortcode' => 'nexdirectory_my_events',
                'atts' => ['limit' => 'limit'],
                'controls' => ['limit' => ['label' => __('Row Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 500, 'default' => 30]],
            ],
            [
                'name' => 'nexdirectory_my_tickets',
                'title' => __('Saved Listings', 'nexdirectory'),
                'icon' => 'eicon-check-circle',
                'shortcode' => 'nexdirectory_my_tickets',
                'atts' => ['limit' => 'limit'],
                'controls' => ['limit' => ['label' => __('Row Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 500, 'default' => 50]],
            ],
            [
                'name' => 'nexdirectory_account',
                'title' => __('Account', 'nexdirectory'),
                'icon' => 'eicon-user-circle-o',
                'shortcode' => 'nexdirectory_account',
                'atts' => [],
                'controls' => [],
            ],
            [
                'name' => 'nexdirectory_auth',
                'title' => __('Auth (Login / Signup)', 'nexdirectory'),
                'icon' => 'eicon-lock-user',
                'shortcode' => 'nexdirectory_auth',
                'atts' => ['redirect_to' => 'redirect_to'],
                'controls' => [
                    'redirect_to' => ['label' => __('Redirect URL', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::URL, 'show_external' => false, 'placeholder' => home_url('/')],
                ],
            ],
            [
                'name' => 'nexdirectory_auth_links',
                'title' => __('Auth Buttons / Menu', 'nexdirectory'),
                'icon' => 'eicon-menu-bar',
                'shortcode' => 'nexdirectory_auth_links',
                'atts' => ['redirect_to' => 'redirect_to', 'show_logout' => 'show_logout'],
                'controls' => [
                    'redirect_to' => ['label' => __('Redirect URL', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::URL, 'show_external' => false, 'placeholder' => home_url('/')],
                    'show_logout' => array_merge(['label' => __('Show Logout In Dropdown', 'nexdirectory')], $select_yes_no),
                ],
            ],
            [
                'name' => 'nexdirectory_user_management',
                'title' => __('User Management', 'nexdirectory'),
                'icon' => 'eicon-users',
                'shortcode' => 'nexdirectory_user_management',
                'atts' => ['limit' => 'limit'],
                'controls' => ['limit' => ['label' => __('User Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 500, 'default' => 100]],
            ],
            [
                'name' => 'nexdirectory_moderation_queue',
                'title' => __('Moderation Queue', 'nexdirectory'),
                'icon' => 'eicon-review',
                'shortcode' => 'nexdirectory_moderation_queue',
                'atts' => ['pending_limit' => 'pending_limit', 'reports_limit' => 'reports_limit'],
                'controls' => [
                    'pending_limit' => ['label' => __('Pending Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 500, 'default' => 100],
                    'reports_limit' => ['label' => __('Reports Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 200, 'default' => 30],
                ],
            ],
            [
                'name' => 'nexdirectory_organizers',
                'title' => __('Organizers', 'nexdirectory'),
                'icon' => 'eicon-person',
                'shortcode' => 'nexdirectory_organizers',
                'atts' => ['limit' => 'limit', 'verified_only' => 'verified_only'],
                'controls' => [
                    'limit' => ['label' => __('Organizer Limit', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 500, 'default' => 200],
                    'verified_only' => [
                        'label' => __('Verified Organizers Only', 'nexdirectory'),
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'options' => [
                            'no' => __('No', 'nexdirectory'),
                            'yes' => __('Yes', 'nexdirectory'),
                        ],
                        'default' => 'no',
                    ],
                ],
            ],
            [
                'name' => 'nexdirectory_claim_listing',
                'title' => __('Claim Listing', 'nexdirectory'),
                'icon' => 'eicon-site-search',
                'shortcode' => 'nexdirectory_claim_listing',
                'atts' => ['event_id' => 'event_id'],
                'controls' => [
                    'event_id' => ['label' => __('Event ID (optional)', 'nexdirectory'), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'default' => 0],
                ],
            ],
        ];
        $plugin = function_exists('nexdirectory_get_plugin_instance')
            ? nexdirectory_get_plugin_instance()
            : null;
        $widgets = (array) apply_filters('nexdirectory_extension_elementor_widget_configs', $widgets, $plugin);

        $premium_widgets = [
            'nexdirectory_event_webapp' => 'event_webapp',
        ];
        $widgets = array_values(array_filter($widgets, static function ($config) use ($premium_widgets) {
            $name = sanitize_key((string) ($config['name'] ?? ''));
            if (!isset($premium_widgets[$name])) {
                return true;
            }
            return function_exists('nexdirectory_is_pro_feature_enabled')
                && nexdirectory_is_pro_feature_enabled($premium_widgets[$name]);
        }));

        foreach ($widgets as $config) {
            $widget = new NEXDIRECTORY_Elementor_Widget_Shortcode([], ['nexdirectory_config' => (array) $config]);
            $widgets_manager->register($widget);
        }
    }
}
