import e from"/static/quotquot/glue/espr.js";import s from"/static/logging/index.js";const t=s.getLogger("@quotquot/i18n");class r{constructor(e){this.el=e,this.classes=new Map,this.init()}init(){const s=this.el.dataset.class;try{var r=e.parse(s)}catch(e){return void t.error(`invalid expression syntax "${s}"`)}if("Program"===r.type&&1===r.body.length&&"ExpressionStatement"===r.body[0].type&&"ObjectExpression"===r.body[0].expression.type)for(const a of r.body[0].expression.properties){if("Property"!==a.type||"Identifier"!==a.key.type&&"Literal"!==a.key.type){t.error(`invalid property format "${s}"`);continue}const r="Identifier"===a.key.type?"name":"value";this.classes.set(a.key[r],{ast:a.value,vars:e.variables(a.value)})}else t.error(`invalid expression format "${s}"`)}async render(s,t,r){for(const[s,{ast:a,vars:o}]of this.classes.entries())if(!r||r.some((e=>o.has(e)))){const r=e.evaluate(a,t)?"add":"remove";this.el.classList[r](s)}}clear(){for(const e of this.classes.keys())this.el.classList.remove(e)}}class a{constructor(e){this.handlers=new Map,e.renderers.push(this)}async _addHandler(e,s,t){const a=new r(e);this.handlers.set(e,a),await a.render(s,t)}async _processMutations(e,s){for(const r of s||this.observer.takeRecords()){const s=r.target,a=s.getAttributeNode("data-class"),o=this.handlers.get(s);null===r.oldValue?(a||t.warn("unexpected: element should have data-class"),o&&t.warn("unexpected: handler should not exist"),await this._addHandler(s,e,e.state)):a?r.oldValue!==a.value&&(o.init(),await o.render(e,e.state)):(o.clear(),this.handlers.delete(s))}}async setup(e){this.observer=new MutationObserver((s=>this._processMutations(e,s))),this.observer.observe(e.shadowRoot,{subtree:!0,attributeFilter:["data-class"],attributeOldValue:!0})}async cleanup(e){this.observer&&(this.observer.disconnect(),this.observer=null);for(const e of this.handlers.values())e.clear();this.handlers.clear()}async renderElement(e,s,t,r,a,o){if(!e.hasAttribute("data-class"))return;const i=this.handlers.get(e);i?await i.render(s,t,r):await this._addHandler(e,s,t,r)}}export{a as default};
