// Jinja2 variable for the application name
const appName = "{{ js_application_name }}";
const cookieName = appName + "SSONoticeShown";

// Function to set a cookie with expiration (in days)
function setCookie(name, value, days) {
    const d = new Date();
    d.setTime(d.getTime() + (days*24*60*60*1000));
    const expires = "expires="+ d.toUTCString();
    document.cookie = name + "=" + value + ";" + expires + ";path=/";
}

// Function to read a cookie
function getCookie(name) {
    const cname = name + "=";
    const decodedCookie = decodeURIComponent(document.cookie);
    const ca = decodedCookie.split(';');
    for(let i = 0; i < ca.length; i++) {
        let c = ca[i].trim();
        if (c.indexOf(cname) === 0) {
            return c.substring(cname.length, c.length);
        }
    }
    return "";
}

// Main logic: show notice only once
window.addEventListener("DOMContentLoaded", function() {
    if (!getCookie(cookieName)) {
        alert("Notice: " + appName + " is not integrated into the SSO. Login is not possible.");
        // Set cookie for 365 days
        setCookie(cookieName, "true", 365);
    }
});