;(function($) {
    'use strict';

    /**
     * Initialize Business Hour Widget
     */
    function initBusinessHour($scope) {
        const $wrapper = $scope.find('.unlockafe-business-hour-wrapper');
        
        if ($wrapper.length === 0) {
            return;
        }

        const highlightToday = $wrapper.data('highlight-today') === 'yes';
        const timeFormat = $wrapper.data('time-format') || '12';
        const $items = $wrapper.find('.unlockafe-business-hour-item');

        // Get current day name
        const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
        const today = new Date().getDay();
        const currentDayName = days[today];

        // Highlight today
        if (highlightToday) {
            $items.each(function() {
                const $item = $(this);
                const day = $item.data('day');
                
                if (day === currentDayName) {
                    $item.addClass('today');
                }
            });
        }

        // Update status in real-time
        function updateStatus() {
            $items.each(function() {
                const $item = $(this);
                const $status = $item.find('.unlockafe-business-hour-status');
                
                if ($status.length && $item.hasClass('today') && !$item.hasClass('is-closed')) {
                    // Check if currently open
                    const timeText = $item.find('.unlockafe-business-hour-time').text();
                    
                    if (timeText && timeText !== 'Closed' && timeText.indexOf('-') > -1) {
                        const times = timeText.split(' - ');
                        if (times.length === 2) {
                            const openTime = times[0].trim();
                            const closeTime = times[1].trim();
                            
                            // Simple check (can be enhanced)
                            const now = new Date();
                            const currentHour = now.getHours();
                            const currentMinute = now.getMinutes();
                            
                            // Parse times (basic implementation)
                            // This is a simplified version - you may want to enhance this
                            const status = $status.hasClass('status-open') ? 'open' : 'closed';
                            // Status is already set from PHP, just ensure it's visible
                        }
                    }
                }
            });
        }

        // Update status every minute
        if (highlightToday) {
            updateStatus();
            setInterval(updateStatus, 60000); // Update every minute
        }
    }

    // Initialize on Elementor frontend
    $(window).on('elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction('frontend/element_ready/unlockafe-business-hour.default', function($scope) {
            initBusinessHour($scope);
        });
    });

    // Also initialize on document ready (for non-Elementor pages)
    $(document).ready(function() {
        $('.elementor-widget-unlockafe-business-hour').each(function() {
            var $scope = $(this);
            initBusinessHour($scope);
        });
    });

    // Re-initialize on Elementor preview refresh
    if (typeof elementorFrontend !== 'undefined') {
        elementorFrontend.hooks.addAction('frontend/element_ready/global', function($scope) {
            if ($scope.find('.unlockafe-business-hour-wrapper').length) {
                setTimeout(function() {
                    initBusinessHour($scope);
                }, 100);
            }
        });
    }

})(jQuery);





