import e from"/static/translation/index.js";import t from"/static/logging/index.js";import s from"/static/security/sanitizer.js";const a=t.getLogger("@quotquot/i18n");async function n(t,a,n){const i=t.dataset.i18n,r=t.dataset.i18nOptions,o=Object.assign({},n,JSON.parse(r||"{}")),d=[],l=await e.getFixedT(a);for(let e of i.split(";"))if(e=e.trim(),0!==e.length)if("["===e[0]){const a=e.split("]");e=a[0].slice(1);const n=l(a[1],o);if("html"===e)t.innerHTML=s.sanitize(n),d.push(...t.children);else if("append"===e){const e=t.lastChild;e&&e.nodeType===Node.TEXT_NODE&&t.removeChild(e);const s=document.createTextNode(n);t.appendChild(s),d.push(s)}else{const s=document.createAttribute(e);s.value=n,t.setAttributeNode(s),d.push(s)}}else{t.firstChild&&t.firstChild.nodeType===Node.TEXT_NODE&&t.removeChild(t.firstChild);const s=document.createTextNode(l(e,o));t.insertBefore(s,t.firstChild),d.push(s)}return d}class i{constructor(e){this.el=e,this.nodes=[]}async render(e,t,s){let i=!1;if(s){const e=this.el.dataset.i18nDependencies;if(e){const t=new Set(e.split());i=s.some((e=>t.has(e))),i&&a.debug(`deps changed for ${this.el.dataset.i18n}:`,t)}}else i=!0;i&&(this.clear(),this.nodes=await n(this.el,t.language,t))}clear(){for(const e of this.nodes)e.nodeType===Node.ATTRIBUTE_NODE?e.ownerElement.removeAttributeNode(e):e.remove();this.nodes.length=0}}class r{constructor(e){this.handlers=new Map,this.update=t=>{e.update({language:t}).catch((t=>e.onerror(t,"plugin","i18n","update")))},e.renderers.push(this)}async _addHandler(e,t,s){const a=new i(e);this.handlers.set(e,a),await a.render(t,s)}async _processMutations(e,t){const s=[],n=[];for(const e of t||this.observer.takeRecords())("data-i18n"===e.attributeName?s:n).push(e);const i=new Set;for(const t of s){const s=t.target,n=s.getAttributeNode("data-i18n"),r=this.handlers.get(s);null===t.oldValue?(n||a.warn("unexpected: element should have data-i18n"),r&&a.warn("unexpected: handler should not exist"),await this._addHandler(s,e,e.state),i.add(s)):n?t.oldValue!==n.value&&(await r.render(e,e.state),i.add(s)):(r.clear(),this.handlers.delete(s))}for(const t of n){const s=t.target,n=t.attributeName;if(!this.handlers.has(s)){s.hasAttribute("data-i18n")&&a.warn("unexpected: no handler for i18n element"),a.warn(`unused ${n} attribute`);continue}if(i.has(s))continue;const r=this.handlers.get(s);await r.render(e,e.state),i.add(s)}}async setup(t){t.state.language=await e.getLanguage(),e.subscribe(this.update),this.observer=new MutationObserver((e=>this._processMutations(t,e).catch((e=>t.onerror(e,"plugin","i18n","mutation"))))),this.observer.observe(t.shadowRoot,{subtree:!0,attributeFilter:["data-i18n","data-i18n-options"],attributeOldValue:!0})}async cleanup(t){this.observer&&(this.observer.disconnect(),this.observer=null);for(const e of this.handlers.values())e.clear();this.handlers.clear(),e.unsubscribe(this.update)}async renderElement(e,t,s,a,n,i){if(!s.language||!e.hasAttribute("data-i18n"))return;const r=this.handlers.get(e);r?await r.render(t,s,a):await this._addHandler(e,t,s,a)}}export{r as default};
