const LOCALES_ARRAY={EN:"en",EN_NZ:"en-NZ",DE:"de",EL:"el",CS:"cs",IT:"it",KO:"ko",ES:"es",FR:"fr",FI:"fi",DE_CH:"de-CH",IT_CH:"it-CH",FR_CH:"fr-CH",NL:"nl"},availableLocales=Object.values(LOCALES_ARRAY);let defaultLanguage,pageLanguage;const urlParams=new URLSearchParams(window.location.search),langFromUrl=urlParams.get("lang"),fetchLocale=async a=>{try{const e=await fetch(a);if(!e.ok)throw new Error(`Failed to fetch: ${a}`);return await e.json()}catch(a){return console.warn(a),null}},defaultProductLanguage=async()=>{const a=await fetchLocale("../../localization/locales/defaultLang.json");return a?.lang||"en"},checkLocals=async a=>{const e=`./css/locales/${a}.css`,l=`../../localization/locales/${a}.json`,[t,n]=await Promise.allSettled([fetch(e),fetch(l)]);return{css:"fulfilled"===t.status&&t.value.ok,json:"fulfilled"===n.status&&n.value.ok}},loadLanguageCss=async a=>{a=a.slice(0,2);const e=await checkLocals(a),l=e.json&&e.css?`./css/locales/${a}.css`:`./css/locales/${defaultLanguage}.css`;try{const a=document.createElement("link");a.rel="stylesheet",a.href=l,a.type="text/css",document.head.appendChild(a)}catch(e){console.warn(`Failed to load CSS for ${a}:`,e)}},getLocales=async a=>{const e=`../../localization/locales/${a}.json`,l=`../../localization/locales/${defaultLanguage}.json`;return await fetchLocale(e)||await fetchLocale(l)||{}},updateElements=a=>{const e=a||{},l=document.querySelectorAll("[data-i18n]");document.documentElement.setAttribute("lang",a.lang),document.title=`${a.title}`;const t=(new Date).getFullYear();l.forEach(a=>{const l=a.getAttribute("data-i18n");if(!l)return;let n=l.split(".").reduce((a,e)=>a?a[e]:null,e);const c=n?n.match(/{(.*?)}/g):[];c&&c.forEach(e=>{const l=e.slice(1,-1);let c=a.getAttribute(`data-${l}`);"year"===l&&(c=t.toString()),null!=c&&null!==n&&(n=n.replace(e,c))}),a.innerHTML=n||""})},init=async()=>{if(defaultLanguage=await defaultProductLanguage(),langFromUrl)pageLanguage=availableLocales.includes(langFromUrl)?langFromUrl:defaultLanguage;else if(navigator.language){const a=navigator.language;pageLanguage=["en-NZ","de-CH","it-CH","fr-CH"].includes(a)?availableLocales.includes(a)?a:defaultLanguage:availableLocales.includes(a.slice(0,2))?a.slice(0,2):defaultLanguage}else pageLanguage=defaultLanguage;console.log("pageLanguage",pageLanguage);const[a,e]=await Promise.all([loadLanguageCss(pageLanguage),getLocales(pageLanguage)]);updateElements(e)},safeRun=async a=>{try{await a()}catch(a){console.error("Initialization error:",a)}};safeRun(init);export{};