/**
 * My Account Full Widget - Tabs Navigation
 * Storzen - WooCommerce Elementor Addon
 */

(function($) {
    'use strict';

    /**
     * Initialize My Account tabs
     */
    function initMyAccountTabs() {
        $('.szp-myaccount-full-nav a[data-endpoint]').on('click', function(e) {
            // Check if we're in Elementor editor mode
            var isEditor = typeof elementor !== 'undefined' && elementor.isEditMode;
            
            if (isEditor) {
                e.preventDefault();
                
                var $link = $(this);
                var endpoint = $link.data('endpoint');
                
                // Remove active class from all links
                $('.szp-myaccount-full-nav a').removeClass('active');
                
                // Add active class to clicked link
                $link.addClass('active');
                
                // Show a message in editor mode
                var $content = $link.closest('.szp-myaccount-full').find('.szp-myaccount-full-content');
                $content.html('<div class="szp-myaccount-editor-notice"><p><strong>Preview Mode:</strong> Tab navigation works on the frontend. Current tab: <em>' + endpoint + '</em></p></div>');
                
                return false;
            }
            
            // On frontend, allow normal navigation
            // The active state will be handled by PHP based on the current endpoint
        });
    }

    /**
     * Handle responsive navigation
     */
    function handleResponsiveNav() {
        var $wrapper = $('.szp-myaccount-full-wrapper');
        
        if ($(window).width() < 992) {
            $wrapper.addClass('szp-myaccount-mobile');
        } else {
            $wrapper.removeClass('szp-myaccount-mobile');
        }
    }

    /**
     * Initialize on document ready
     */
    $(document).ready(function() {
        initMyAccountTabs();
        handleResponsiveNav();
        
        // Handle window resize
        $(window).on('resize', function() {
            handleResponsiveNav();
        });
    });

    /**
     * Initialize for Elementor editor
     */
    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/storzen-my-account-full.default', function($scope) {
            initMyAccountTabs();
            handleResponsiveNav();
        });
    });

})(jQuery);
