<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
var menuConfig = {
};

function setMenuAttributes(issuer, redirectUri, clientId, userApiBaseUrl, logoutUrl) {
    menuConfig.issuer = issuer;
    menuConfig.redirectUri = redirectUri;
    menuConfig.clientId = clientId;
    menuConfig.userApiBaseUrl = userApiBaseUrl;
    menuConfig.scope = 'openid';
    menuConfig.responseType = 'code';
    menuConfig.logoutUrl = logoutUrl;
}

var menu = document.querySelector('ponsse-menu-web');

var useGeoLocationFlag = false;

function activateGeoLocation() {
    useGeoLocationFlag = true;
    localStorage.removeItem('customLocation');
}

function onFavouriteContactClicked() {
    node = document.getElementsByClassName("favourite-contact")[0];

    if(node.children[0].textContent == "star_border") {
        activateGeoLocation();
    }
}

function checkDomNodeInsert(event) {
    var countryPanelOpened = false;
    var locationPanelOpened = false;
    if (event.target.classList !== undefined) {
       countryPanelOpened = event.target.classList.contains("locate");
       locationPanelOpened = event.target.classList.contains("favourite-contact");
    }
    if (countryPanelOpened == true) {
        event.target.addEventListener("click", activateGeoLocation);
    }
    if (locationPanelOpened == true) {
        event.target.addEventListener("click", onFavouriteContactClicked);
    }
}

function checkDomNodeRemove(event) {
    if (event.target.nodeName == "PM-COUNTRY-LANGUAGE-SELECTOR") {
        event.target.removeEventListener("click", activateGeoLocation);
    } else if (event.target.nodeName == "PM-LOCAL-CONTACT-CONTENT") {
        event.target.removeEventListener("click", onFavouriteContactClicked);
    }
}

document.addEventListener('DOMNodeInserted', checkDomNodeInsert, false);
document.addEventListener('DOMNodeRemoved', checkDomNodeRemove, false);

navigator.geolocation.getCurrentPosition(function(position) {
    if (!window.localStorage.getItem("geolocationAllowed")) {
        window.location.reload();
    }
    window.localStorage.setItem("geolocationAllowed", true);
}, function() {
    window.localStorage.setItem("geolocationAllowed", false);
});

var cookieConsentLevels = JSON.parse(window.localStorage.getItem("cookieConsentLevels"));
if (cookieConsentLevels !== null) {
    menu.disableFavoriteContactSelection = !cookieConsentLevels.includes(2)
    menu.disableCountrySelection = !cookieConsentLevels.includes(2)
}
menu.addEventListener('signIn', function () {
    return idmLogin();
});
menu.addEventListener('signOut', function () {
    return idmLogout();
});
menu.addEventListener('languageChange', function (event) {
    // Use AUI().ready function to wait until everything is set on liferay side
    AUI().ready(
        'liferay-sign-in-modal',
        'event-outside',
        'transition',
        function(A) {
            changeLanguage(event.detail.code.substring(0, 2))
        }
    );
});

menu.addEventListener('countryChange', function (event) {
    var flag = useGeoLocationFlag;
    useGeoLocationFlag = false;
       if (window.store &amp;&amp; !useGeoLocationFlag) {
        //Access Redux store directly via window
        window.store.dispatch({
            type: "SET_CUSTOM_LOCATION",
            payload: event.detail,
        })
    }
    setTimeout(() =&gt; {
            if (!flag &amp;&amp; window.sessionStorage.getItem("manualCountry") === 'true') {
                window.localStorage.setItem("customLocation", JSON.stringify(event.detail));
            } else {
                localStorage.removeItem('customLocation');
            }
    }, 2000);
});

menu.addEventListener('favoriteContactChange', function (event) {
    //Access Redux store directly via window
    if(window.store){
        window.store.dispatch({
            type: "CHANGE_FAVOURITE_CONTACT",
            payload: event.detail,
        })
        window.localStorage.setItem("favourite-contact", JSON.stringify(event.detail));
    }
});

function idmLogin() {
    window.location.href = menuConfig.issuer + '?' + ('response_type=' + menuConfig.responseType) + ('&amp;redirect_uri=' + menuConfig.redirectUri) + ('&amp;client_id=' + menuConfig.clientId) + ('&amp;scope=' + menuConfig.scope);
}

function idmSilentAuth() {
    url = menuConfig.issuer + '?' + ('prompt=none') + ('&amp;response_type=' + menuConfig.responseType) + ('&amp;redirect_uri=' + menuConfig.redirectUri) + ('&amp;client_id=' + menuConfig.clientId) + ('&amp;scope=' + menuConfig.scope);
    window.location.href = url;
}

function idmLogout() {
    window.location.href = menuConfig.logoutUrl;
}

function getOidcCode() {
    var queryString = window.location.search;
    var urlParams = new URLSearchParams(queryString);
    return urlParams.get('code');
}

function getError() {
    var queryString = window.location.search;
    var urlParams = new URLSearchParams(queryString);
    return urlParams.get('error');
}

async function getToken(code) {
    var response = await fetch(menuConfig.userApiBaseUrl + 'oidc/token/' + code);
    return await response.json();
}

async function getUserInformation(userId, idToken) {
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('bearer', idToken);

    var response = await fetch(menuConfig.userApiBaseUrl + 'rest-api/agent/full-permissions?agentId=' + userId, {headers: headers});
    return await response.json();
}

function cleanOidcCodeFromUrl() {
    var url = new URL(window.location.href);
    url.searchParams.delete('code');
    window.history.replaceState(null, null, url);
}

function setMenuUser(user) {
    menu.userProfile = user;
}

function currentURLLang() {
    return window.location.pathname.split('/')[1];
}

function URLStartsWithLang(languages) {
    // if url starts with any of the possible language codes

    if (languages &amp;&amp; languages.some(function (language) {
        if (currentURLLang() === language.code.substring(0, 2)) {
            return true;
        } else {
            return false;
        }
    })) {
        return true;
    } else {
        return false;
    }
}

function changeLanguage(languageCode) {
    if (currentURLLang() === languageCode) {
        return;
    }

    var langObject = window.Liferay.Language.available;
    var languages = Object.keys(langObject).map(function (key) {
        return {code: key, value: langObject[key]};
    });

    if (window.location.pathname === "/") {
        window.location.pathname = "/" + languageCode + "/";
    } else if (URLStartsWithLang(languages)) {
        var oldPath = window.location.pathname;
        var newPath = window.location.pathname.replace(/^.{3}/g, "/" + languageCode);
        window.location.href = window.location.href.replace(oldPath, newPath);
    } else {
        var _oldPath = window.location.pathname;
        var _newPath = "/" + languageCode + window.location.pathname;
        window.location.href = window.location.href.replace(_oldPath, _newPath);
    }
}
</pre></body></html>