jQuery(document).ready(function($) {
    // Function to detect timezone using browser-specific APIs
    function detectTimezone() {
        var timezone;
        if (typeof Intl === 'object' && typeof Intl.DateTimeFormat === 'function') {
            timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        } else if (typeof navigator === 'object' && typeof navigator.languages === 'object') {
            //timezone = navigator.languages[0].substring(0, 2);
            timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
        } else {
            // Fallback to using IPinfo.io API
            fetchTimezoneFromIPinfo();
           //timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
            return;
        }
        
        // If timezone is successfully detected, store it in a cookie
        setTimezoneCookie(timezone);

        // If timezone is successfully detected, send it to the server via AJAX
        var nonce = ajax_object.nonce; // Retrieve nonce value from hidden input field
        sendTimezoneToServer(timezone, nonce);
    }
    
    // Function to set a cookie with the timezone
    function setTimezoneCookie(timezone) {
        var date = new Date();
        date.setTime(date.getTime() + (365*24*60*60*1000)); // Cookie expires in 1 year
        document.cookie = "user_timezone=" + timezone + "; expires=" + date.toUTCString() + "; path=/";
    }

    // Attempt to detect timezone using browser-specific APIs
    //disabled:
    detectTimezone();

    // Function to fetch timezone from IPinfo.io
    function fetchTimezoneFromIPinfo() {
        // Attempt to get user's timezone using IP address
        $.ajax({
            url: 'https://ipinfo.io/json',
            type: 'GET',
            success: function(response) {
                // Extract timezone from response
                var timezone = response.timezone;
                //set timezone into cookie for further use
                setTimezoneCookie(timezone);
                // Get the nonce value
                var nonce = ajax_object.nonce; // Retrieve nonce value from hidden input field
                // Send the timezone to the server via AJAX
                sendTimezoneToServer(timezone, nonce);
                console.log('Time sent to ipinfo.io');
            },
            error: function(xhr, status, error) {
                console.error('Error fetching user timezone using IP address: ' + error);
                
                // Fallback to UTC timezone
                var nonce = $('#timezone_nonce').val(); // Retrieve nonce value from hidden input field
                console.log(nonce);
                // Send the UTC timezone to the server via AJAX
                sendTimezoneToServer('UTC', nonce);
            }
        });
    }

    // Function to send timezone information to the server
    function sendTimezoneToServer(timezone,nonce) {
        // Send the timezone to the server via AJAX
        $.ajax({
            type: 'POST',
            url: ajax_object.ajax_url,
            data: {
                action: 'capture_timezone_offset_callback',
                timezone: timezone,
                nonce: nonce // Include nonce in the data object
            },
            success: function(response) {
                console.log('Timezone done! ' + timezone);
            },
            error: function(xhr, status, error) {
                console.error('Error sending timezone to server: ' + error);
            }
        });
    }
});