var e,t;e=this,t=function(){"use strict";class e{static evaluate(e,t){if("string"!=typeof e)return e;if(!e.trim())return"";let n=this._cache.get(e);if(!n)try{n=Function("data",`with(data) { return ${e}; }`),this._cache.set(e,n)}catch{return""}try{return n(t)}catch{return""}}}e._cache=new Map;class t{get value(){return this._value}set value(e){this._value!==e&&(this._value=e,this._notify())}watch(e){return this._watchers.add(e),()=>this._watchers.delete(e)}_notify(){for(let e of this._watchers)e(this._value)}constructor(e){this._value=e,this._watchers=new Set}}class n{on(e,t){let n=this._events.get(e);return n||this._events.set(e,n=new Set),n.add(t),()=>this.off(e,t)}off(e,t){if(this._events.has(e))if(t){let n=this._events.get(e);n.delete(t),0===n.size&&this._events.delete(e)}else this._events.delete(e)}emit(e,...t){let n=this._events.get(e);if(n)for(let e of n)e(...t)}constructor(){this._events=new Map}}let i=["value","checked","selected"];class o{patchDOM(e,t){this._tempContainer.innerHTML=t,this._diff(e,this._tempContainer),this._tempContainer.innerHTML=""}_diff(e,t){if(!e.firstChild&&!t.firstChild)return;let n=Array.from(e.childNodes),i=Array.from(t.childNodes),o=0,a=0,s=n.length-1,r=i.length-1,l=null;for(;o<=s&&a<=r;){let t=n[o],r=i[a];if(!t){o++;continue}if(this._isSameNode(t,r))this._patchNode(t,r),o++,a++;else{l||(l=this._createKeyMap(n,o,s));let i=this._getNodeKey(r),u=i?l.get(i):null;u&&u.nodeName===r.nodeName?(this._patchNode(u,r),e.insertBefore(u,t),n[n.indexOf(u)]=null):e.insertBefore(r.cloneNode(!0),t),a++}}if(o>s){let t=i[r+1]?n[o]:null;for(let n=a;n<=r;n++)i[n]&&e.insertBefore(i[n].cloneNode(!0),t)}else if(a>r)for(let t=o;t<=s;t++)n[t]&&this._removeNode(e,n[t])}_patchNode(e,t){e._eleva_instance||(3===e.nodeType?e.nodeValue!==t.nodeValue&&(e.nodeValue=t.nodeValue):1===e.nodeType&&(this._updateAttributes(e,t),this._diff(e,t)))}_removeNode(e,t){"STYLE"===t.nodeName&&t.hasAttribute("data-e-style")||e.removeChild(t)}_updateAttributes(e,t){for(let n of t.attributes)if("@"!==n.name[0]&&(e.getAttribute(n.name)!==n.value&&e.setAttribute(n.name,n.value),n.name in e))try{let t="boolean"==typeof e[n.name]?"false"!==n.value:n.value;e[n.name]!==t&&(e[n.name]=t)}catch{continue}for(let n=e.attributes.length-1;n>=0;n--){let i=e.attributes[n].name;t.hasAttribute(i)||e.removeAttribute(i)}for(let n of i)n in t&&e[n]!==t[n]&&(e[n]=t[n])}_isSameNode(e,t){if(!e||!t)return!1;let n=this._getNodeKey(e),i=this._getNodeKey(t);return n&&i?n===i&&e.nodeName===t.nodeName:!n&&!i&&e.nodeType===t.nodeType&&e.nodeName===t.nodeName}_getNodeKey(e){return e?.nodeType===1?e.getAttribute("key"):null}_createKeyMap(e,t,n){let i=new Map;for(let o=t;o<=n;o++){let t=this._getNodeKey(e[o]);t&&i.set(t,e[o])}return i}constructor(){this._tempContainer=document.createElement("div")}}return class{use(e,t={}){if(!e?.install||"function"!=typeof e.install)throw Error("Eleva: plugin must have an install function");this._plugins.set(e.name,e);let n=e.install(this,t);return void 0!==n?n:this}component(e,t){if(!e||"string"!=typeof e)throw Error("Eleva: component name must be a non-empty string");if(!t?.template)throw Error(`Eleva: component "${e}" must have a template`);return this._components.set(e,t),this}async mount(e,n,i={}){if(!e?.nodeType)throw Error("Eleva: container must be a DOM element");if(e._eleva_instance)return e._eleva_instance;let o="string"==typeof n?this._components.get(n):n;if(!o)throw Error(`Component "${n}" not registered.`);let a=`c${++this._componentCounter}`,{setup:s,template:r,style:l,children:u}=o,h={props:i,emitter:this.emitter,signal:e=>new this.signal(e)},c=async n=>{let i={...h,...n},o=[],s=[],c=[],f=!1,m=!1,d=()=>{m||(m=!0,queueMicrotask(async()=>{m=!1,await p()}))},p=async()=>{let t="function"==typeof r?await r(i):r;f?await i.onBeforeUpdate?.({container:e,context:i}):await i.onBeforeMount?.({container:e,context:i}),this.renderer.patchDOM(e,t);let n=[];for(let t=s.length-1;t>=0;t--){let i=s[t];e.contains(i.container)||(s.splice(t,1),n.push(i))}n.length&&await Promise.allSettled(n.map(e=>e.unmount())),this._processEvents(e,i,c),l&&this._injectStyles(e,a,l,i),u&&await this._mountComponents(e,u,s,i),f?await i.onUpdate?.({container:e,context:i}):(await i.onMount?.({container:e,context:i}),f=!0)};for(let e of Object.values(n))e instanceof t&&o.push(e.watch(d));await p();let _={container:e,data:i,async unmount(){for(let t of(await i.onUnmount?.({container:e,context:i,cleanup:{watchers:o,listeners:c,children:s}}),o))t();for(let e of c)e();for(let e of s)await e.unmount();e.innerHTML="",delete e._eleva_instance}};return e._eleva_instance=_,_},f="function"==typeof s?await s(h):{};return await c(f)}_processEvents(e,t,n){for(let i of e.querySelectorAll("*")){let e=i.attributes;for(let o=e.length-1;o>=0;o--){let a=e[o];if(!a.name.startsWith("@"))continue;let s=a.name.slice(1),r=a.value,l=t[r]||this.templateEngine.evaluate(r,t);"function"==typeof l&&(i.addEventListener(s,l),i.removeAttribute(a.name),n.push(()=>i.removeEventListener(s,l)))}}}_injectStyles(e,t,n,i){let o="function"==typeof n?n(i):n,a=e.querySelector(`style[data-e-style="${t}"]`);a&&a.textContent===o||(a||((a=document.createElement("style")).setAttribute("data-e-style",t),e.appendChild(a)),a.textContent=o)}_extractProps(e,t){if(!e.attributes)return{};let n={},i=e.attributes;for(let o=i.length-1;o>=0;o--){let a=i[o];a.name.startsWith(":")&&(n[a.name.slice(1)]=this.templateEngine.evaluate(a.value,t),e.removeAttribute(a.name))}return n}async _mountComponents(e,t,n,i){for(let[o,a]of Object.entries(t))if(o)for(let t of e.querySelectorAll(o)){if(!(t instanceof HTMLElement))continue;let e=this._extractProps(t,i),o=await this.mount(t,a,e);o&&!n.includes(o)&&n.push(o)}}constructor(i){if(!i||"string"!=typeof i)throw Error("Eleva: name must be a non-empty string");this.name=i,this.emitter=new n,this.signal=t,this.templateEngine=e,this.renderer=new o,this._components=new Map,this._plugins=new Map,this._componentCounter=0}}},"object"==typeof exports&&"u">typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="u">typeof globalThis?globalThis:e||self).Eleva=t();
//# sourceMappingURL=eleva.umd.min.js.map
