document.addEventListener("nsnCookieManager", function (event) {
if (nsn_geolocation_restriction) {
function getCookie(name) {
const value = "; " + document.cookie;
const parts = value.split("; " + name + "=");
if (parts.length === 2) return JSON.parse(parts.pop().split(";").shift());
return null;
}
function createCustomOverlay(links, message) {
const overlay = document.createElement("div");
overlay.classList.add("block-overlay");
const container = document.createElement("div");
container.classList.add("block-message");
const messageDiv = document.createElement("div");
messageDiv.classList.add("message-text");
messageDiv.innerHTML = message;
const linksContainer = document.createElement("div");
linksContainer.classList.add("custom-links");
links.forEach(function (link, index) {
const linkDiv = document.createElement("div");
linkDiv.classList.add("custom-link");
if (index % 2 === 0) {
linkDiv.classList.add("even");
}
if (link.logo) {
const img = document.createElement("img");
img.src = link.logo;
img.alt = link.text || "";
linkDiv.appendChild(img);
}
if (link.text && link.url) {
const anchor = document.createElement("a");
anchor.href = link.url;
anchor.textContent = link.text;
linkDiv.appendChild(anchor);
}
linksContainer.appendChild(linkDiv);
});
container.appendChild(messageDiv);
container.appendChild(linksContainer);
overlay.appendChild(container);
document.body.appendChild(overlay);
}
function createDefaultOverlay(message, options = {}) {
if (options.displayCustomMessage === true) {
const overlay = document.createElement("div");
overlay.classList.add("block-overlay");
overlay.innerHTML = message;
document.body.appendChild(overlay);
} else {
const overlay = document.createElement("div");
overlay.classList.add("block-overlay");
const container = document.createElement("div");
container.classList.add("block-message");
const messageDiv = document.createElement("div");
messageDiv.classList.add("message-text");
messageDiv.textContent = message;
container.appendChild(messageDiv);
overlay.appendChild(container);
document.body.appendChild(overlay);
}
}
function isValidGeoData(geoData) {
return geoData && geoData.cC && geoData.cC !== 'N/A' && geoData.r && geoData.r !== 'N/A';
}
function handleGeoContent() {
const eventDetail = event && event.detail ? event.detail : null;
let userCountry, userRegion;
// Use the event values if both fields are valid otherwise, fetch from cookie
if (isValidGeoData(eventDetail)) {
userCountry = eventDetail.cC;
userRegion = eventDetail.r;
} else {
const userGeo = getCookie("_ad__RE");
if (!isValidGeoData(userGeo)) return;
userCountry = userGeo.cC;
userRegion = userGeo.r;
}
const metaGeoloc = nsn_geolocation_restriction.metaGeoloc;
const metaType = nsn_geolocation_restriction.metaGeolocType;
const messageData = nsn_geolocation_restriction.messageData;
const worldData = nsn_geolocation_restriction.worldData;
const shouldBeCustom = nsn_geolocation_restriction.shouldBeCustom;
const customLinks = nsn_geolocation_restriction.customLinks;
const customMessage = nsn_geolocation_restriction.customMessage ?? '';
const geoArray = Array.isArray(metaGeoloc) ? metaGeoloc : [];
const matches = geoArray.some(geo => {
const [geoCountry, geoRegion = "0"] = geo.r.split("-"); // "CA-ON" => ["CA", "ON"]
return geoCountry === userCountry && (geoRegion === userRegion || geoRegion === "0");
});
const shouldCreateOverlay = metaType === "exclude" && matches;
if (!shouldCreateOverlay) return;
let regionName = "";
if (worldData && worldData.data && Array.isArray(worldData.data)) {
const match = worldData.data.find(function (item) {
const parts = item.rCode.split("-"); // e.g: "CA-ON"
const itemRegion = parts.length > 1 ? parts[1] : item.rCode;
return itemRegion === userRegion;
});
if (match && match.region) {
regionName = match.region;
}
}
let customLinksForUser = [];
for (let key in customLinks) {
if (customLinks.hasOwnProperty(key)) {
const keyParts = key.split("|"); // e.g: "CA-ON|--Canada - Ontario"
const regionParts = keyParts[0].split("-");
const countryKey = regionParts[0];
const regionKey = regionParts[1];
if (countryKey === userCountry && (regionKey === userRegion || regionKey == "0")) {
customLinksForUser = customLinks[key];
break;
}
}
}
const foundRegionName = regionName !== "";
let overlayMessage = "";
if (shouldBeCustom && customLinksForUser.length > 0) {
overlayMessage = foundRegionName
? "You appear to be located in " +
regionName +
" where this page is not available.
Here are some pages that might interest you in your localisation."
: "You appear to be located in a country or region where this page is not available.
Here are some pages that might interest you in your localisation.";
createCustomOverlay(customLinksForUser, overlayMessage);
} else if (shouldBeCustom && customMessage && customMessage.trim() !== '' ) {
createDefaultOverlay(customMessage, { displayCustomMessage: true });
} else {
overlayMessage = foundRegionName
? "You appear to be located in " +
regionName +
" where the advertised operator or service is not available."
: "You appear to be located in a country or region where the advertised operator or service is not available.";
if (Array.isArray(messageData) && messageData.length > 0) {
for (let i = 0; i < messageData.length; i++) {
if (messageData[i].Country === userCountry) {
overlayMessage = messageData[i].Message;
break;
}
}
}
createDefaultOverlay(overlayMessage);
}
}
handleGeoContent();
} else {
console.error("nsn_geolocation_restriction is not defined.");
}
});