const COUNTRY_KEY = "country";
const COUNTRY_TIMESTAMP_KEY = "country_timestamp";

document.addEventListener("DOMContentLoaded", () => {
    const currentCountry = localStorage.getItem(COUNTRY_KEY) || null;
    const countryTimestamp =
        localStorage.getItem(COUNTRY_TIMESTAMP_KEY) || null;

    const currentDate = new Date();
    const oneWeekDate = new Date(
        currentDate.getTime() + 7 * 24 * 60 * 60 * 1000
    );

    if (currentCountry) {
        if (countryTimestamp <= oneWeekDate.getTime()) {return;}
    }

    localStorage.removeItem(COUNTRY_KEY);
    localStorage.removeItem(COUNTRY_TIMESTAMP_KEY);

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            ({ coords: { latitude: lat, longitude: lng } }) => {
                fetch(
                    `https://api.bigdatacloud.net/data/reverse-geocode-client?latitude=${lat}&longitude=${lng}&localityLanguage=en`
                )
                    .then((res) => res.json())
                    .then(({countryCode}) => {
                        if (!countryCode)
                            throw new Error("Country code not found");
                        localStorage.setItem(COUNTRY_KEY, countryCode);
                        localStorage.setItem(
                            COUNTRY_TIMESTAMP_KEY,
                            Date.now().toString()
                        );
                        // if (countryCode === 'GB') {
                        //     $('.currEx i').removeClass("icon-dollar-sign icon-euro-sign").addClass("icon-pound-sign");
                        //     var currenCookieSet = $.cookie('exp__currenCook', 'GBP', {
                        //         path: '/'
                        //     });
                        //     var CookieSet = $.cookie('exp__currency', 'GB', {
                        //         path: '/'
                        //     });
                        // }
                        // else if (countryCode === 'US') {
                        //     $('.currEx i').removeClass("icon-pound-sign icon-euro-sign").addClass("icon-dollar-sign");
                        //     var currenCookieSet = $.cookie('exp__currenCook', 'USD', {
                        //         path: '/'
                        //     });
                        //     var CookieSet = $.cookie('exp__currency', 'US', {
                        //         path: '/'
                        //     });
                        // }
                        // else if (countryCode === 'AD' || countryCode === 'AT' || countryCode === 'BE' || countryCode === 'CY' || countryCode === 'EE' || countryCode === 'FI' || countryCode === 'FR' || countryCode === 'DE' || countryCode === 'GR' || countryCode === 'IE' || countryCode === 'IT' || countryCode === 'XK' || countryCode === 'LV' || countryCode === 'LU' || countryCode === 'MT' || countryCode === 'MC' || countryCode === 'ME' || countryCode === 'NL' || countryCode === 'PT' || countryCode === 'SM' || countryCode === 'SK' || countryCode === 'SI' || countryCode === 'ES' || countryCode === 'VA') {
                        //     $('.currEx i').removeClass("icon-dollar-sign icon-pound-sign").addClass("icon-euro-sign");
                        //     var currenCookieSet = $.cookie('exp__currenCook', 'EUR', {
                        //         path: '/'
                        //     });
                        //     var CookieSet = $.cookie('exp__country', 'EU', {
                        //         path: '/'
                        //     });
                        // }
                        console.log('The COUNTRY_KEY is ' + countryCode);
                    })
                    .catch((err) =>
                        console.error("Error fetching country data:", err)
                    );
            },
            (err) => console.error("Error getting user location:", err)
        );
    } else {
        alert("Please enable location services in your browser settings.");
    }
    
});