'use strict'; // Message-based redirection (function () { // Initial load (auto redirect by iframe) const SUPPORTED_LANGS = new Set(['en', 'pt-BR', 'es-ES']); const SWITCHER_LANG_MAP = { 'en-US': 'en', es: 'es-ES' }; const DEFAULT_LANG = [...SUPPORTED_LANGS][0]; const CLIP_APP_URL = 'https://clip.opus.pro'; function getCurrentLang() { const [, lang] = window.location.pathname.match(/\/([a-z]{2}-[a-z]{2})(\/|$)/) || []; const detectedLang = lang || DEFAULT_LANG; console.log('[LANGUAGE SYNC]: Current language detected:', detectedLang); return detectedLang; } function normalizePath(path) { return path || '/'; } function buildNewPath(targetLang, currentLang, currentPath) { let newPath = currentPath; if (currentLang !== DEFAULT_LANG) { newPath = newPath.replace(`/${currentLang}`, ''); } if (targetLang !== DEFAULT_LANG) { newPath = `/${targetLang}${newPath === '/' ? '' : newPath}`; } return normalizePath(newPath); } function redirectToLang(lang) { console.log( '[LANGUAGE SYNC]: Received request to redirect to language:', lang ); const targetLang = SUPPORTED_LANGS.has(lang) ? lang : DEFAULT_LANG; const normalizedTargetLang = targetLang.toLowerCase(); // fallback to default lang if language is not supported if (!SUPPORTED_LANGS.has(lang)) { console.log( '[LANGUAGE SYNC]: Language not supported:', lang, 'redirecting to default:', DEFAULT_LANG ); } const currentLang = getCurrentLang(); if (currentLang === normalizedTargetLang) { console.log( '[LANGUAGE SYNC]: Language matches current page, no redirect needed' ); return; } const currentUrl = `${window.location.pathname}${window.location.search}${window.location.hash}`; const newPath = buildNewPath( normalizedTargetLang, currentLang, window.location.pathname ); const newUrl = `${newPath}${window.location.search}${window.location.hash}`; if (newUrl !== currentUrl) { console.log('[LANGUAGE SYNC]: Performing redirect to:', newUrl); window.location.href = newUrl; } } function toggleLangSwitcherVisibility(shouldDisplay) { console.log( '[LANGUAGE SYNC]: Setting language switcher visibility:', shouldDisplay ); const langSwitcher = document.querySelector( '[data-element="footer-locale-switcher"]' ); if (langSwitcher) { langSwitcher.style.display = shouldDisplay ? 'block' : 'none'; } } function handleMessage(event) { if (event.origin !== CLIP_APP_URL) return; if (event.data.type === 'lang') { const requestedLang = event.data.content.lang; redirectToLang(requestedLang); } if (event.data.type === 'lang_switcher') { const displayValue = event.data.content?.display; if (typeof displayValue === 'boolean') { toggleLangSwitcherVisibility(displayValue); } } } window.addEventListener('message', handleMessage); // User-initiated language change (from parent to iframe) function getLangCode(hreflang) { return SWITCHER_LANG_MAP[hreflang] || hreflang; } function sendLangMessage(langCode) { const iframe = document.querySelector(`iframe[src*="${CLIP_APP_URL}"]`); if (iframe?.contentWindow) { iframe.contentWindow.postMessage( { type: 'change_language', content: { lang: langCode } }, CLIP_APP_URL ); } } function handleLangLinkClick(e) { e.preventDefault(); const langCode = getLangCode(e.currentTarget.getAttribute('hreflang')); sendLangMessage(langCode); } function setupLangLinks() { const langLinks = document.querySelectorAll('a[hreflang]'); langLinks.forEach(function (link) { link.addEventListener('click', handleLangLinkClick); }); } document.addEventListener('DOMContentLoaded', setupLangLinks); })();