/**
 * Copyright 2021 The Pennsylvania State University
 * @license Apache-2.0, see License.md for full text
 */
import{LitElement as e}from"../../lit/index.js";globalThis.I18NManagerStore=globalThis.I18NManagerStore||{},globalThis.I18NManagerStore.requestAvailability=()=>(!globalThis.I18NManagerStore.instance&&globalThis.document&&globalThis.document.body&&(globalThis.I18NManagerStore.instance=globalThis.document.createElement("i18n-manager"),globalThis.document.body.appendChild(globalThis.I18NManagerStore.instance)),globalThis.I18NManagerStore.instance);export const I18NManagerStore=globalThis.I18NManagerStore.requestAvailability();class I18NManager extends e{constructor(){super(),this.windowControllers=new AbortController,this.fetchTargets={},this.elements=[],this.locales=new Set([]),this.lang=this.documentLang,this.dir=this.documentDir}get documentLang(){return globalThis.document.body.getAttribute("xml:lang")||globalThis.document.body.getAttribute("lang")||globalThis.document.documentElement.getAttribute("xml:lang")||globalThis.document.documentElement.getAttribute("lang")||globalThis.navigator.language||"en"}get documentDir(){return globalThis.document.body.getAttribute("xml:dir")||globalThis.document.body.getAttribute("dir")||globalThis.document.documentElement.getAttribute("xml:dir")||globalThis.document.documentElement.getAttribute("dir")||"ltr"}connectedCallback(){this.__ready=!0,globalThis.addEventListener("i18n-manager-register-element",this.registerLocalizationEvent.bind(this),{signal:this.windowControllers.signal}),globalThis.addEventListener("languagechange",this.changeLanguageEvent.bind(this),{signal:this.windowControllers.signal})}disconnectedCallback(){this.windowControllers.abort()}changeLanguageEvent(e){this.lang=e.detail}registerLocalizationEvent(e){let t=this.detailNormalize(e.detail);t.namespace&&t.localesPath&&t.locales&&this.registerLocalization(t)}detailNormalize(e){if(!e.namespace&&e.context&&(e.namespace=e.context.tagName.toLowerCase()),!e.updateCallback&&e.context&&(e.context.requestUpdate?e.updateCallback="requestUpdate":e.context.render&&(e.updateCallback="render")),!e.localesPath&&e.basePath&&(e.localesPath=`${decodeURIComponent(e.basePath)}/../locales`),e.context&&e.namespace){e.context.t&&(e.context._t={...e.context.t});let t=this.elements.filter((t=>{if(t.namespace==e.namespace&&t.localesPath&&t.locales)return!0}));t&&t.length&&t[0]&&(e.localesPath=t[0].localesPath,e.locales=t[0].locales)}return e}registerLocalization(e){(!e.context&&0===this.elements.filter((t=>t.namespace===e.namespace)).length||0===this.elements.filter((t=>t.context===e.context)).length)&&(e=this.detailNormalize(e),this.elements.push(e),e&&e.locales&&e.locales.forEach(this.locales.add,this.locales),this.lang&&this.__ready&&e.locales&&e.locales.includes(this.lang)&&(clearTimeout(this._debounce),this._debounce=setTimeout((()=>{this.updateLanguage(this.lang)}),0)))}static get tag(){return"i18n-manager"}async loadNamespaceFile(e,t=this.lang){const a=t.split("-");let l=this.elements.filter((l=>l.namespace===e&&(l.locales.includes(t)||l.locales.includes(a[0]))));if(l&&1===l.length){const e=l[0];var n="";return e.locales.includes(t)?n=`${e.localesPath}/${e.namespace}.${t}.json`:e.locales.includes(a[0])&&(n=`${e.localesPath}/${e.namespace}.${a[0]}.json`),""==n?{}:(this.fetchTargets[n]||(this.fetchTargets[n]=await fetch(n).then((e=>!(!e||!e.json)&&e.json()))),this.fetchTargets[n])}}async updateLanguage(e){if(e){const n=e.split("-"),s=this.elements.filter((t=>{try{return t.locales.includes(e)||t.locales.includes(n[0])}catch(e){console.error("i18n registration incorrect in:",t,e)}})),i=this.elements.filter((t=>!t.locales.includes(e)&&!t.locales.includes(n[0])));if(0!==i.length)for(var t in i){let e=i[t];e.context&&(e.context.t={...e.context._t},e.updateCallback&&e.context[e.updateCallback]())}for(var t in s){let i=s[t];var a="";if(i.locales.includes(e)?a=`${i.localesPath}/${i.namespace}.${e}.json`:i.locales.includes(n[0])&&(a=`${i.localesPath}/${i.namespace}.${n[0]}.json`),this.fetchTargets[a]){if(i.context){let e=this.fetchTargets[a];for(var l in e)i.context.t[l]=e[l];i.context.t={...i.context.t},i.updateCallback&&i.context[i.updateCallback]()}}else if(this.fetchTargets[a]=await fetch(a).then((e=>!(!e||!e.json)&&e.json())),i.context){for(var l in this.fetchTargets[a])i.context.t[l]=this.fetchTargets[a][l];i.context.t={...i.context.t},i.updateCallback&&i.context&&i.context[i.updateCallback]()}}}}static get observedAttributes(){return["lang","dir"]}attributeChangedCallback(e,t,a){"lang"!==e&&"dir"!==e||this.dispatchEvent(new CustomEvent(`${e}-changed`,{detail:{value:a}})),"lang"===e&&a&&this.__ready&&this.updateLanguage(a)}get lang(){return this.getAttribute("lang")}set lang(e){e?this.setAttribute("lang",e):this.removeAttribute("lang")}get dir(){return this.getAttribute("dir")}set dir(e){e?this.setAttribute("dir",e):this.removeAttribute("dir")}}globalThis.customElements.define(I18NManager.tag,I18NManager);export{I18NManager};