<?php

namespace Unlockafe_addons\Widgets;

use Unlockafe_addons\Traits\Utils as UnlockafeUtils;
use Unlockafe_addons\Traits\Widget_helper;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use \Elementor\Group_Control_Typography;
use \Elementor\Group_Control_Background;
use \Elementor\Group_Control_Box_Shadow;
use \Elementor\Group_Control_Border;
use \Elementor\Repeater;

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

/**
 * Footer Links Widget
 *
 * @since 1.0.0
 */
class Footer_Links extends Widget_Base
{

    use Widget_helper, UnlockafeUtils;

    /**
     * Retrieve the widget name.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget name.
     */
    public function get_name()
    {
        return 'unlockafe-footer-links';
    }

    /**
     * Retrieve the widget title.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget title.
     */
    public function get_title()
    {
        return __('Footer Links', 'unlock-addons-for-elementor');
    }

    /**
     * Retrieve the widget icon.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return string Widget icon.
     */
    public function get_icon()
    {
        return 'eicon-menu ulicon';
    }

    /**
     * Retrieve the list of categories the widget belongs to.
     *
     * Used to determine where to display the widget in the editor.
     *
     * Note that currently Elementor supports only one category.
     * When multiple categories passed, Elementor uses the first one.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget categories.
     */
    public function get_categories()
    {
        return ['unlockafe-addons'];
    }

    /**
     * Retrieve the list of scripts the widget depended on.
     *
     * Used to set scripts dependencies required to run the widget.
     *
     * @since 1.0.0
     *
     * @access public
     *
     * @return array Widget scripts dependencies.
     */
    public function get_script_depends()
    {
        return [];
    }

    public function get_style_depends()
    {
        return ['unlockafe-global', 'unlockafe-footer-links-style'];
    }

    /**
     * Register the widget controls.
     *
     * Adds different input fields to allow the user to change and customize the widget settings.
     *
     * @since 1.0.0
     *
     * @access protected
     */

    protected function register_controls()
    {
        $this->register_controls_section();
        $this->style_tab_content();
    }

    protected function register_controls_section()
    {

        $this->start_controls_section(
            'wpr_footer_social_section_layout',
            [
                'label' => esc_html__('Layout', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'wpr_design_style',
            [
                'label' => esc_html__('Design Style', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'layout-1' => esc_html__('Layout 1', 'unlock-addons-for-elementor'),
                    'layout-2' => esc_html__('Layout 2', 'unlock-addons-for-elementor'),
                    'layout-3' => esc_html__('Layout 3', 'unlock-addons-for-elementor'),
                ],
                'default' => 'layout-1',
            ]
        );

        $this->add_responsive_control(
            'wpr_footer_links_direction',
            [
                'label' => esc_html__('Layout Direction', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'row' => esc_html__('Row (Horizontal)', 'unlock-addons-for-elementor'),
                    'column' => esc_html__('Column (Vertical)', 'unlock-addons-for-elementor'),
                ],
                'default' => 'row',
                'selectors' => [
                    '{{WRAPPER}}.unlock_addon_footer_links_direction-row .unlock_addon_footer_links_list' => 'grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)) !important;',
                    '{{WRAPPER}}.unlock_addon_footer_links_direction-column .unlock_addon_footer_links_list' => 'grid-template-columns: 1fr !important;',
                    '{{WRAPPER}}.unlock_addon_footer_links_layout-2.unlock_addon_footer_links_direction-row .unlock_addon_footer_links_list' => 'grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;',
                    '{{WRAPPER}}.unlock_addon_footer_links_layout-2.unlock_addon_footer_links_direction-column .unlock_addon_footer_links_list' => 'grid-template-columns: 1fr !important;',
                    '{{WRAPPER}}.unlock_addon_footer_links_layout-3.unlock_addon_footer_links_direction-row .unlock_addon_footer_links_list' => 'grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)) !important;',
                    '{{WRAPPER}}.unlock_addon_footer_links_layout-3.unlock_addon_footer_links_direction-column .unlock_addon_footer_links_list' => 'grid-template-columns: 1fr !important;',
                ],
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'wpr_footer_links_sec',
            [
                'label' => esc_html__('Menu List Controls', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $repeater = new Repeater();

        $repeater->add_control(
            'repeater_condition',
            [
                'label' => __('Field condition', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'style_1' => __('Style 1', 'unlock-addons-for-elementor'),
                    'style_2' => __('Style 2', 'unlock-addons-for-elementor'),
                    'style_3' => __('Style 3', 'unlock-addons-for-elementor'),
                ],
                'default' => 'style_1',
                'frontend_available' => true,
                'style_transfer' => true,
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_label',
            [
                'label'   => esc_html__('List Label', 'unlock-addons-for-elementor'),
                'type'        => Controls_Manager::TEXT,
                'default'     => esc_html__('Demo Label', 'unlock-addons-for-elementor'),
                'label_block' => true,
                'condition' => [
                    'repeater_condition' => 'style_2',
                ]
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_number',
            [
                'label'   => esc_html__('Menu List Number', 'unlock-addons-for-elementor'),
                'type'        => Controls_Manager::TEXT,
                'default'     => esc_html__('01', 'unlock-addons-for-elementor'),
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_title',
            [
                'label'   => esc_html__('Menu List Title', 'unlock-addons-for-elementor'),
                'type'        => Controls_Manager::TEXT,
                'default'     => esc_html__('Demo Title', 'unlock-addons-for-elementor'),
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_link_type',
            [
                'label' => esc_html__('Menu Link Type', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    '1' => 'Custom Link',
                    '2' => 'Internal Page',
                ],
                'default' => '1',
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_link',
            [
                'label' => esc_html__('Menu link', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::URL,
                'dynamic' => [
                    'active' => true,
                ],
                'placeholder' => esc_html__('https://your-link.com', 'unlock-addons-for-elementor'),
                'show_external' => false,
                'default' => [
                    'url' => '#',
                    'is_external' => false,
                    'nofollow' => false,
                ],
                'condition' => [
                    'wpr_footer_links_link_type' => '1',
                ],
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'wpr_footer_links_page_link',
            [
                'label' => esc_html__('Select Menu Page', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SELECT2,
                'label_block' => true,
                'options' => $this->tp_get_all_types_post('page'),
                'condition' => [
                    'wpr_footer_links_link_type' => '2',
                ]
            ]
        );


        $this->add_control(
            'wpr_footer_links_list',
            [
                'label'       => esc_html__('Link List', 'unlock-addons-for-elementor'),
                'type'        => Controls_Manager::REPEATER,
                'fields'      => $repeater->get_controls(),
                'default'     => [
                    [
                        'wpr_footer_links_title'   => esc_html__('Projects', 'unlock-addons-for-elementor'),
                    ],
                    [
                        'wpr_footer_links_title'   => esc_html__('What We Do', 'unlock-addons-for-elementor'),
                    ],
                    [
                        'wpr_footer_links_title'   => esc_html__('About', 'unlock-addons-for-elementor'),
                    ],
                ],
                'title_field' => '{{{ wpr_footer_links_title }}}',
            ]
        );


        $this->end_controls_section();
    }

    // style_tab_content
    protected function style_tab_content()
    {
        $this->start_controls_section(
            'unlockafe_footer_links_container',
            [
                'label' => esc_html__('Container', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_links_alignment',
            [
                'label' => esc_html__('Alignment', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'flex-start' => [
                        'title' => esc_html__('Left', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => esc_html__('Center', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'flex-end' => [
                        'title' => esc_html__('Right', 'unlock-addons-for-elementor'),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'flex-start',
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links' => 'justify-content: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_links_padding',
            [
                'label' => esc_html__('Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_links_bg',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links',
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'unlockafe_footer_links_border',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links',
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'unlockafe_footer_links_shadow',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'unlockafe_footer_links_list_style',
            [
                'label' => esc_html__('Item', 'unlock-addons-for-elementor'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_links_number_typo',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_number',
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_number_color',
            [
                'label' => esc_html__('Number Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_number' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_links_title_typo',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_title',
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_title_color',
            [
                'label' => esc_html__('Title Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_title' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'unlockafe_footer_links_label_typo',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_label',
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_label_color',
            [
                'label' => esc_html__('Label Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_label' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_links_item_gap',
            [
                'label' => esc_html__('Item Gap', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 80,
                    ],
                ],
                'default' => [
                    'unit' => 'px',
                    'size' => 18,
                ],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_list' => 'gap: {{SIZE}}{{UNIT}} !important;',
                ],
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_links_item_padding',
            [
                'label' => esc_html__('Item Padding', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', 'em', '%'],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'unlockafe_footer_links_item_radius',
            [
                'label' => esc_html__('Item Radius', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_link' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'unlockafe_footer_links_item_border',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_link',
            ]
        );

        $this->start_controls_tabs('unlockafe_footer_links_item_tabs');

        $this->start_controls_tab(
            'unlockafe_footer_links_item_normal',
            [
                'label' => esc_html__('Normal', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_item_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_link' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_links_item_bg',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_link',
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'unlockafe_footer_links_item_hover',
            [
                'label' => esc_html__('Hover', 'unlock-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_item_hover_color',
            [
                'label' => esc_html__('Text Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_link:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'unlockafe_footer_links_item_hover_bg',
                'selector' => '{{WRAPPER}} .unlock_addon_footer_links_link:hover',
            ]
        );

        $this->add_control(
            'unlockafe_footer_links_item_hover_border_color',
            [
                'label' => esc_html__('Border Color', 'unlock-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .unlock_addon_footer_links_link:hover' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();
    }

    /**
     * Render the widget output on the frontend.
     *
     * Written in PHP and used to generate the final HTML.
     *
     * @since 1.0.0
     *
     * @access protected
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display(); ?>

        <?php
        $layout = !empty($settings['wpr_design_style']) ? $settings['wpr_design_style'] : 'layout-1';
        $direction = !empty($settings['wpr_footer_links_direction']) ? $settings['wpr_footer_links_direction'] : 'row';
        
        $wrapper_classes = [
            'unlock_addon_footer_links',
            'unlock_addon_footer_links_' . $layout,
            'unlock_addon_footer_links_direction-' . $direction,
        ];
        
        // Calculate grid columns based on direction and layout
        $grid_columns = 'repeat(auto-fit, minmax(180px, 1fr))';
        if ($direction === 'column') {
            $grid_columns = '1fr';
        } elseif ($layout === 'layout-2' && $direction === 'row') {
            $grid_columns = 'repeat(auto-fit, minmax(140px, 1fr))';
        } elseif ($layout === 'layout-3' && $direction === 'row') {
            $grid_columns = 'repeat(auto-fit, minmax(220px, 1fr))';
        } elseif ($layout === 'layout-2' && $direction === 'column') {
            $grid_columns = '1fr';
        } elseif ($layout === 'layout-3' && $direction === 'column') {
            $grid_columns = '1fr';
        }
        
        $list_style = 'grid-template-columns: ' . esc_attr($grid_columns) . ' !important;';
        $widget_id = $this->get_id();
        ?>
        <div class="<?php echo esc_attr(implode(' ', array_map('sanitize_html_class', $wrapper_classes))); ?>" data-widget-id="<?php echo esc_attr($widget_id); ?>" data-direction="<?php echo esc_attr($direction); ?>" data-layout="<?php echo esc_attr($layout); ?>">
            <?php if (!empty($settings['wpr_footer_links_list'])) : ?>
                <ul class="unlock_addon_footer_links_list" style="<?php echo esc_attr($list_style); ?>">
                    <?php foreach ($settings['wpr_footer_links_list'] as $index => $menu) :
                        if ('2' === $menu['wpr_footer_links_link_type'] && !empty($menu['wpr_footer_links_page_link'])) {
                            $link = get_permalink($menu['wpr_footer_links_page_link']);
                            $target = '_self';
                            $rel = 'nofollow';
                        } else {
                            $link = !empty($menu['wpr_footer_links_link']['url']) ? $menu['wpr_footer_links_link']['url'] : '#';
                            $target = !empty($menu['wpr_footer_links_link']['is_external']) ? '_blank' : '_self';
                            $rel = !empty($menu['wpr_footer_links_link']['nofollow']) ? 'nofollow' : '';
                        }

                        $number = !empty($menu['wpr_footer_links_number']) ? $menu['wpr_footer_links_number'] : sprintf('%02d', $index + 1);
                        $title = !empty($menu['wpr_footer_links_title']) ? $menu['wpr_footer_links_title'] : esc_html__('Menu Item', 'unlock-addons-for-elementor');
                        $label = (!empty($menu['wpr_footer_links_label']) && $menu['repeater_condition'] === 'style_2') ? $menu['wpr_footer_links_label'] : '';
                    ?>
                        <li class="unlock_addon_footer_links_item">
                            <a class="unlock_addon_footer_links_link" href="<?php echo esc_url($link); ?>" target="<?php echo esc_attr($target); ?>" <?php echo !empty($rel) ? 'rel="' . esc_attr($rel) . '"' : ''; ?>>
                                <span class="unlock_addon_footer_links_number"><?php echo esc_html($number); ?></span>
                                <span class="unlock_addon_footer_links_content">
                                    <?php if (!empty($label)) : ?>
                                        <small class="unlock_addon_footer_links_label"><?php echo esc_html($label); ?></small>
                                    <?php endif; ?>
                                    <span class="unlock_addon_footer_links_title"><?php echo esc_html($title); ?></span>
                                </span>
                                <span class="unlock_addon_footer_links_icon" aria-hidden="true">
                                    <i class="fa-solid fa-arrow-up-right"></i>
                                </span>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php else : ?>
                <p class="unlock_addon_footer_links_empty"><?php esc_html_e('Add footer links from the widget settings panel.', 'unlock-addons-for-elementor'); ?></p>
            <?php endif; ?>
        </div>

        <script>
        (function() {
            var widgetId = '<?php echo esc_js($widget_id); ?>';
            var currentDirection = '<?php echo esc_js($direction); ?>';
            var currentLayout = '<?php echo esc_js($layout); ?>';
            
            // Function to get direction from Elementor control or widget
            function getDirection($widget) {
                // Try to get from Elementor control first
                var $control = jQuery('.elementor-control-wpr_footer_links_direction select');
                if ($control.length) {
                    var val = $control.val();
                    if (val && val !== '') return val;
                }
                
                // Try responsive variants
                $control = jQuery('.elementor-control-wpr_footer_links_direction_tablet select');
                if ($control.length) {
                    var val = $control.val();
                    if (val && val !== '') return val;
                }
                
                $control = jQuery('.elementor-control-wpr_footer_links_direction_mobile select');
                if ($control.length) {
                    var val = $control.val();
                    if (val && val !== '') return val;
                }
                
                // Fallback to data attribute
                if ($widget && $widget.length) {
                    var dir = $widget.data('direction');
                    if (dir) return dir;
                }
                
                return currentDirection;
            }
            
            // Function to get layout
            function getLayout($widget) {
                var $control = jQuery('.elementor-control-wpr_design_style select');
                if ($control.length) {
                    var val = $control.val();
                    if (val) return val;
                }
                
                if ($widget && $widget.length) {
                    var lay = $widget.data('layout');
                    if (lay) return lay;
                }
                
                return currentLayout;
            }
            
            // Function to update grid columns
            function updateFooterLinksDirection($scope) {
                var $widget = $scope || jQuery('[data-widget-id="' + widgetId + '"]');
                if (!$widget.length) {
                    $widget = jQuery('.elementor-element-' + widgetId);
                }
                if (!$widget.length) {
                    $widget = jQuery('.elementor-widget-unlockafe-footer-links');
                }
                
                if ($widget.length) {
                    var direction = getDirection($widget);
                    var layout = getLayout($widget);
                    var $list = $widget.find('.unlock_addon_footer_links_list');
                    
                    if ($list.length) {
                        var gridColumns = 'repeat(auto-fit, minmax(180px, 1fr))';
                        if (direction === 'column') {
                            gridColumns = '1fr';
                        } else if (layout === 'layout-2' && direction === 'row') {
                            gridColumns = 'repeat(auto-fit, minmax(140px, 1fr))';
                        } else if (layout === 'layout-3' && direction === 'row') {
                            gridColumns = 'repeat(auto-fit, minmax(220px, 1fr))';
                        }
                        
                        // Apply with !important
                        $list.each(function() {
                            var el = this;
                            if (el && el.style) {
                                el.style.setProperty('grid-template-columns', gridColumns, 'important');
                            }
                        });
                    }
                }
            }
            
            // Use Elementor's frontend hooks for instant updates
            if (typeof elementorFrontend !== 'undefined') {
                // Hook into Elementor's element ready - this fires when widget is rendered/updated
                elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-footer-links.default', function($scope) {
                    setTimeout(function() {
                        updateFooterLinksDirection($scope);
                    }, 10);
                    setTimeout(function() {
                        updateFooterLinksDirection($scope);
                    }, 100);
                });
                
                // Global hook for any widget updates
                elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
                    if ($scope.find('.unlock_addon_footer_links_list').length) {
                        setTimeout(function() {
                            updateFooterLinksDirection($scope);
                        }, 10);
                        setTimeout(function() {
                            updateFooterLinksDirection($scope);
                        }, 100);
                    }
                });
            }
            
            // Apply immediately
            if (typeof jQuery !== 'undefined') {
                // Run on document ready
                jQuery(document).ready(function() {
                    updateFooterLinksDirection();
                });
                
                // Apply in Elementor editor mode
                if (typeof elementorFrontend !== 'undefined' && elementorFrontend.isEditMode && elementorFrontend.isEditMode()) {
                    setTimeout(function() { updateFooterLinksDirection(); }, 100);
                    setTimeout(function() { updateFooterLinksDirection(); }, 300);
                    setTimeout(function() { updateFooterLinksDirection(); }, 500);
                    
                    // Listen for Elementor preview refresh
                    if (typeof elementor !== 'undefined' && elementor.on) {
                        elementor.on('preview:loaded', function() {
                            setTimeout(function() { updateFooterLinksDirection(); }, 100);
                            setTimeout(function() { updateFooterLinksDirection(); }, 300);
                        });
                    }
                }
                
                // Listen to control changes - trigger Elementor preview refresh
                var controlSelector = '.elementor-control-wpr_footer_links_direction select';
                jQuery(document).on('change', controlSelector, function() {
                    // Force update after Elementor re-renders
                    setTimeout(function() { updateFooterLinksDirection(); }, 50);
                    setTimeout(function() { updateFooterLinksDirection(); }, 150);
                    setTimeout(function() { updateFooterLinksDirection(); }, 300);
                });
                
                // Listen to responsive controls
                var responsiveSelector = '.elementor-control-wpr_footer_links_direction_tablet select, .elementor-control-wpr_footer_links_direction_mobile select';
                jQuery(document).on('change', responsiveSelector, function() {
                    setTimeout(function() { updateFooterLinksDirection(); }, 50);
                    setTimeout(function() { updateFooterLinksDirection(); }, 150);
                });
                
                // Listen to design style change
                jQuery(document).on('change', '.elementor-control-wpr_design_style select', function() {
                    setTimeout(function() { updateFooterLinksDirection(); }, 50);
                    setTimeout(function() { updateFooterLinksDirection(); }, 150);
                });
                
                // Listen for Elementor element changes
                if (typeof elementor !== 'undefined' && elementor.channels && elementor.channels.editor) {
                    elementor.channels.editor.on('element:change', function(model) {
                        if (model && model.get('widgetType') === 'unlockafe-footer-links') {
                            var settings = model.get('settings');
                            if (settings) {
                                var $widget = jQuery('[data-widget-id="' + widgetId + '"]');
                                if (!$widget.length) {
                                    $widget = jQuery('.elementor-element-' + widgetId);
                                }
                                if (!$widget.length) {
                                    $widget = jQuery('.elementor-widget-unlockafe-footer-links');
                                }
                                if ($widget.length) {
                                    if (settings.wpr_footer_links_direction) {
                                        $widget.attr('data-direction', settings.wpr_footer_links_direction);
                                    }
                                    if (settings.wpr_design_style) {
                                        $widget.attr('data-layout', settings.wpr_design_style);
                                    }
                                }
                            }
                            // Update after Elementor re-renders
                            setTimeout(function() { updateFooterLinksDirection(); }, 50);
                            setTimeout(function() { updateFooterLinksDirection(); }, 150);
                            setTimeout(function() { updateFooterLinksDirection(); }, 300);
                        }
                    });
                }
            }
        })();
        </script>

<?php

    }
}


