UNPKG

taggedjs

Version:

tagged template reactive html

1 lines 27.1 kB
var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function r(t,e,r,n){return e.renderWithSupport(t,r,n)}function n(t){const e={beforeRender:t.beforeRender||(()=>{}),beforeRedraw:t.beforeRedraw||(()=>{}),afterRender:t.afterRender||(()=>{}),beforeDestroy:t.beforeDestroy||(()=>{})};n.tagUse.push(e)}function o(t,e){n.tagUse.forEach((r=>r.beforeRender(t,e)))}function a(t,e){n.tagUse.forEach((r=>r.afterRender(t,e)))}function s(t,e){n.tagUse.forEach((r=>r.beforeRedraw(t,e)))}function i(t){return u(t,new WeakMap)}function u(t,e){if(null===t||"object"!=typeof t)return t;if(e.has(t))return e.get(t);if(t instanceof Date)return new Date(t);if(t instanceof RegExp)return new RegExp(t);const r=Array.isArray(t)?[]:Object.create(Object.getPrototypeOf(t));if(e.set(t,r),Array.isArray(t))for(let n=0;n<t.length;n++)r[n]=u(t[n],e);else for(const n in t)t.hasOwnProperty(n)&&(r[n]=u(t[n],e));return r}function c(t,e){return l(t,e,new WeakMap)}function l(t,e,r){if(t===e||(o=e,(n=t)instanceof Function&&o instanceof Function&&n.toString()===o.toString()))return!0;var n,o;if("object"!=typeof t||"object"!=typeof e||null===t||null===e)return!1;const a=Object.keys(t),s=Object.keys(e);if(a.length!==s.length)return!1;if(r.has(t))return!0;r.set(t,0);for(const n of a)if(!s.includes(n)||!l(t[n],e[n],r))return!1;if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(!l(t[n],e[n],r))return!1}else if(Array.isArray(t)||Array.isArray(e))return!1;return!0}function p(t,e){if(t===e)throw new Error("something here");const r=t.propsConfig.latest;if(function(t,e,r){if(void 0===t&&t===r)return!1;let n=t,o=e;if("object"==typeof t){if(!e)return!0;if(n={...t},o={...e||{}},!Object.entries(n).every((([t,e])=>{let r=o[t];return!(e instanceof Function)||r instanceof Function&&(r.original&&(r=r.original),e.original&&(e=e.original),e.toString()===r.toString()&&(delete n[t],delete o[t],!0))})))return!0}return!c(e,t)}(e.propsConfig.latest,t.propsConfig.latestCloned,r))return!0;const n=function(t,e){const r=t.propsConfig.lastClonedKidValues,n=e.propsConfig.lastClonedKidValues,o=r.every(((t,e)=>{const r=n[e];return t.every(((t,e)=>t===r[e]))}));return!o}(t,e);return n}function g(t){t.tagSupport.memory.providers.filter((t=>!c(t.instance,t.clone))).forEach((e=>{!function(t,e){d(t,e).forEach((({tag:t,renderCount:e,provider:r})=>{e===t.tagSupport.memory.renderCount&&(r.clone=i(r.instance),t.tagSupport.render())}))}(t.getAppElement(),e),e.clone=i(e.instance)}))}function d(t,e,r=[]){const n=t.tagSupport.memory.providers.find((t=>t.constructMethod===e.constructMethod));return n&&r.push({tag:t,renderCount:t.tagSupport.memory.renderCount,provider:n}),t.children.forEach((t=>d(t,e,r))),r}function f(t,e,r){const n=r.memory.renderCount;if(g(t),n!==r.memory.renderCount)return!0;const o=p(t.tagSupport,e.tagSupport),a=r.templater;return r.newest=a.redraw(),e.newest=r.newest,!o}t.d(e,{W9:()=>C,ei:()=>E,B7:()=>O,vw:()=>Tt,PC:()=>S,uX:()=>x,Ot:()=>$,eQ:()=>Ut,cF:()=>Wt,qy:()=>jt,u2:()=>ht,zl:()=>yt,_4:()=>b,iO:()=>v,mn:()=>y,Yn:()=>w,sA:()=>$t,MG:()=>Pt,r5:()=>Ft,iz:()=>r,vJ:()=>o,hZ:()=>R,Un:()=>Vt,Ie:()=>Bt,AI:()=>n,Tc:()=>I,Hf:()=>m,_A:()=>M,wB:()=>Rt}),n.tagUse=[],n.memory={};const h=[];function m(t,e,r){const n=h.findIndex((t=>t.element===e));n>=0&&(h[n].tag.destroy(),h.splice(n,1),console.warn("Found and destroyed app element already rendered to element",{element:e}));const s=function(t){const e=t.tagSupport;o(e,void 0);const r=t.wrapper(e);return a(e,r),{tag:r,tagSupport:e}}(t(r)),{tag:i,tagSupport:u}=s;i.appElement=e,i.tagSupport.oldest=i,function(t,e){t.templater.redraw=()=>{const r=e,{retag:n}=t.templater.renderWithSupport(t,r,{});return e.updateByTag(n),n},t.mutatingRender=()=>(f(e,t.templater,t),e)}(i.tagSupport,i);const c=document.createElement("template");return c.setAttribute("id","app-tag-"+h.length),c.setAttribute("app-tag-detail",h.length.toString()),e.appendChild(c),i.buildBeforeElement(c),e.setUse=t.original.setUse,h.push({element:e,tag:i}),{tag:i,tags:t.original.tags}}function y(t){return!0===t?.isTemplater}function w(t){return!0===t?.isTag}function b(t){return!(!0!==t?.isSubject&&!t?.subscribe)}function v(t){return t instanceof Array&&t.every((t=>w(t)))}class S extends Error{details;constructor(t,e,r={}){super(t),this.name=S.name,this.details={...r,errorCode:e}}}class C extends S{constructor(t,e){super(t,"array-no-key-error",e),this.name=C.name}}class E extends S{constructor(t,e){super(t,"state-mismatch-error",e),this.name=E.name}}function T(t,e){return r=>(e.callback=r||(e=>[t,t=e]),t)}function A(t){const e=t.callback;if(!e)return t.defaultValue;const r=e(j),[n]=r,[o]=e(n);if(o!==j){const a='State property not used correctly. Second item in array is not setting value as expected.\n\nFor "let" state use `let name = state(default)(x => [name, name = x])`\n\nFor "const" state use `const name = state(default)()`\n\nProblem state:\n'+(e?e.toString():JSON.stringify(t))+"\n";throw console.error(a,{state:t,callback:e,oldState:r,oldValue:n,checkValue:o}),new Error(a)}return n}n.memory.stateConfig={array:[],rearray:[]},n({beforeRender:t=>V(t),beforeRedraw:t=>V(t),afterRender:t=>{const e=t.memory.state,r=n.memory.stateConfig;if(r.rearray.length&&r.rearray.length!==r.array.length){const e=`States lengths mismatched ${r.rearray.length} !== ${r.array.length}`;throw new E(e,{oldStates:r.array,newStates:r.rearray,component:t.templater?.wrapper.original})}r.rearray=[],e.newest=[...r.array],r.array=[]}});class j{}function V(t){const e=t.memory.state,r=n.memory.stateConfig;if(r.rearray.length){const n="last array not cleared";throw console.error(n,{config:r,component:t.templater?.wrapper.original,state:e,expectedClearArray:r.rearray}),new E(n,{config:r,component:t.templater?.wrapper.original,state:e,expectedClearArray:r.rearray})}r.rearray=[],e?.newest.length&&r.rearray.push(...e.newest)}function R(t){const e=n.memory.stateConfig;let r;const o=e.rearray[e.array.length];if(o){let t=A(o);r=e=>[t,t=e];const n={callback:r,lastValue:t,defaultValue:o.defaultValue};return e.array.push(n),t}let a=(t instanceof Function?t:()=>t)();r=t=>[a,a=t];const s={callback:r,lastValue:a,defaultValue:a};return e.array.push(s),a}class x{templater;children;propsConfig;memory={context:{},state:{newest:[]},providers:[],renderCount:0};updateState(){this.memory.state.newest.forEach((t=>{t.lastValue=A(t)}))}constructor(t,e,r){this.templater=t,this.children=e;const n=B(r,t);this.propsConfig={latest:r,latestCloned:n,clonedProps:n,lastClonedKidValues:e.value.map((t=>F(t.values)))},w(r)||(this.propsConfig.latestCloned=i(n),this.propsConfig.clonedProps=this.propsConfig.latestCloned)}oldest;newest;mutatingRender(){const t='Tag function "render()" was called in sync but can only be called async';throw console.error(t,{tagSupport:this}),new Error(t)}render(){return++this.memory.renderCount,this.mutatingRender()}}function F(t){return t.map((t=>{const e=t;return w(e)?F(e.values):y(e)?i(e.tagSupport.propsConfig.latestCloned):v(e)?F(e):i(t)}))}class k{tagged;wrapper;insertBefore;newest;oldest;tagSupport;constructor(t,e){this.tagSupport=new x(this,e,t)}redraw;isTemplater=!0;renderWithSupport(t,e,r){++t.memory.renderCount;const i=e?.ownerTag||r;e?(t.propsConfig={...e.tagSupport.propsConfig},s(t,e)):(o(t,i),n.memory.providerConfig.ownerTag=i);const u=this.wrapper(t);return a(t,u),this.newest=u,u.ownerTag=i,t.newest=u,{remit:!0,retag:u}}}function B(t,e){const r=function(t,r){if("object"!=typeof t)return t;const n=t;return Object.entries(n).forEach((([t,r])=>{if(r instanceof Function){if(n[t].original)return;return n[t]=(...t)=>function(t,r){const n=t(...r),o=e.newest,a=o?.ownerTag?.tagSupport;return a.render(),n}(r,t),void(n[t].original=r)}})),n}(w(t)?0:t);return r}class O{value;isSubject=!0;subscribers=[];constructor(t){this.value=t}subscribe(t){this.subscribers.push(t),N.globalSubs.push(t);const e=N.globalSubCount$;N.globalSubCount$.set(e.value+1);const r=()=>{r.unsubscribe()};return r.unsubscribe=()=>{P(this.subscribers,t),P(N.globalSubs,t),N.globalSubCount$.set(e.value-1),r.unsubscribe=()=>{}},r}set(t){this.value=t,this.subscribers.forEach((e=>{e.value=t,e(t)}))}next=this.set}function P(t,e){const r=t.indexOf(e);-1!==r&&t.splice(r,1)}const N=O;N.globalSubs=[],N.globalSubCount$=new O,N.globalSubCount$.set(0);class $ extends O{value;constructor(t){super(t),this.value=t}subscribe(t){const e=super.subscribe(t);return t(this.value),e}}function U(t,e){if(t.isChildOverride)return t;const r=(r,n)=>_(t,e,r,n);return r.tagFunction=t,r}function _(t,e,r,n){const o=e.tagSupport,a=o?o.memory.renderCount:0,s=t.bind(r)(...n),i=a===o.memory.renderCount;if(!o||i)return o.render(),s instanceof Promise?s.then((()=>(o.render(),"promise-no-data-ever"))):"no-data-ever"}const M=[];let W=0;function I(t){const e=function(e,r){(w(e)||v(e))&&(r=e,e=void 0);const{childSubject:n,madeSubject:o}=function(t){if(b(t))return{childSubject:t,madeSubject:!1};if(v(t))return{childSubject:new $(t),madeSubject:!0};const e=t;return e?(e.arrayValue=0,{childSubject:new $([e]),madeSubject:!0}):{childSubject:new $([]),madeSubject:!0}}(r);n.isChildSubject=!0;const a=new k(e,n),s=t=>{const e=s.original,r=a.oldest;let u=t.propsConfig.latest,c=B(u,a);const l=e(c,n);t.mutatingRender===x.prototype.mutatingRender&&(t.oldest=l,a.oldest=l,t.mutatingRender=()=>{if(f(a.oldest,a,t))return l;if(l.ownerTag){const t=l.ownerTag.tagSupport.render();return l.ownerTag.tagSupport.newest=t,l}return l}),l.tagSupport=new x(a,t.children);const p=i(c);return l.tagSupport.propsConfig={latest:u,latestCloned:p,clonedProps:p,lastClonedKidValues:l.tagSupport.propsConfig.lastClonedKidValues},l.tagSupport.memory=t.memory,l.tagSupport.mutatingRender=t.mutatingRender,t.newest=l,t.propsConfig={...l.tagSupport.propsConfig},r&&(r.tagSupport.propsConfig={...l.tagSupport.propsConfig}),o&&n.value.forEach((t=>{t.values.forEach(((e,r)=>{e instanceof Function&&(t.values[r].isChildOverride||(t.values[r]=function(...t){_(e,l.ownerTag,this,t)},t.values[r].isChildOverride=!0))}))})),l};return s.original=t,a.tagged=!0,a.wrapper=s,a};return function(t,e){t.isTag=!0,t.original=e}(e,t),function(t){t.tags=M,t.setUse=n,t.tagIndex=W++}(t),M.push(t),e}function L(t,e){e.parentNode.insertBefore(t,e)}function D(t,e,r){const n=t.split(".");if("style"===n[0]&&(r.style[n[1]]=e),"class"===n[0])return n.shift(),void(e?n.forEach((t=>r.classList.add(t))):n.forEach((t=>r.classList.remove(t))))}new class{};const K=/^\s*{__tagvar/,z=/}\s*$/;function H(t){return t&&t.search(K)>=0&&t.search(z)>=0}function J(t,e,r,n,o,a){if(H(e))return function(t,e,r,n,o,a){return q(t,X(n,e),r,o,a)}(t,e,r,n,o,a);if(H(t)){let e;const s=X(n,t).subscribe((t=>{!function(t,e,r,n,o){if(e&&e!=t&&("string"==typeof e?r.removeAttribute(e):e instanceof Object&&Object.entries(e).forEach((([t])=>r.removeAttribute(t)))),"string"!=typeof t)t instanceof Object&&Object.entries(t).forEach((([t,e])=>q(t,e,r,n,o)));else{if(!t.length)return;q(t,"",r,n,o)}}(t,e,r,o,a),e=t}));return o.cloneSubs.push(s),void r.removeAttribute(t)}return G(t)?D(t,e,r):void 0}function X(t,e){return t[e.replace("{","").split("").reverse().join("").replace("}","").split("").reverse().join("")]}function q(t,e,r,n,o){const a=G(t);if(e instanceof Function){const n=function(...t){return e(r,t)};r[t].action=n}if(b(e)){r.removeAttribute(t);const s=e=>function(t,e,r,n,o){if(t instanceof Function)return e[r]=function(...r){return t(e,r)},void(e[r].tagFunction=t);if(n)return void D(r,t,e);if(t)return void o(e,r,t);void 0===t||!1===t||null===t?e.removeAttribute(r):o(e,r,t)}(e,r,t,a,o),i=e.subscribe(s);n.cloneSubs.push(i)}else o(r,t,e)}function G(t){return t.search(/^(class|style)(\.)/)>=0}function Q(t,e,r){t.setAttribute(e,r)}function Y(t,e,r){t[e]=r}function Z(t,e,r){const n=t.getAttributeNames();let o=Q;n.forEach((n=>{"INPUT"===t.nodeName&&"value"===n&&(o=Y),J(n,t.getAttribute(n),t,e,r,o),o=Q}))}const tt=/(?:<[^>]*?(?:(?:\s+\w+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^>\s]+)))*\s*)\/?>)|({__tagvar[^}]+})/g;function et(t,e,r,{counts:n,forceElement:o}){const a=e,s=a.tag;if(s&&!o&&s.isLikeTag(t)){if(e instanceof Function){const t=e(s.tagSupport);return s.updateByTag(t),[]}return s.updateByTag(t),[]}const i=t.buildBeforeElement(r,{counts:n,forceElement:o});return a.tag=a.tag||t,i}function rt(t,e){const r=t.lastArray;if(r&&!v(e))return r.forEach((({tag:t})=>ot(t,{added:0,removed:0}))),delete t.lastArray,1;const n=t,o=n.tag;if(o){const r=w(e);return w(t.value)&&r?(s=o,((a=e).strings.length!==s.strings.length||!a.strings.every(((t,e)=>s.strings[e]===t))||a.values.length!==s.values.length)&&(nt(o,n),2)):!y(e)&&(nt(o,n),3)}var a,s;const i=t,u="lastValue"in i,c=i.lastValue;return!(!u||c===e)&&(function(t,e){const r=e.clone,n=r.parentNode;n.insertBefore(t,r),n.removeChild(r),delete e.clone,delete e.lastValue}(i.template,i),4)}function nt(t,e){delete e.tag,delete e.tagSupport,t.destroy()}function ot(t,e){t.children.forEach((t=>t.destroy({stagger:e.removed++}))),t.destroy({stagger:e.removed})}function at(t,e,r,n,o){const a=n.clones;t.lastArray=t.lastArray||[],t.template=r;let s=0;t.lastArray=t.lastArray.filter(((r,n)=>{const a=e.length-1<n-s,i=e[n-s],u=i?.arrayValue;return!(a||!st(u,r.tag.arrayValue))||(ot(t.lastArray[n].tag,o.counts),++s,!1)}));const i=r||r.clone;return e.forEach(((r,s)=>{const u=t.lastArray[s],c=r.tagSupport||u?.tag.tagSupport;r.tagSupport=c||new x({},new $([])),c?c.newest=r:(r.tagSupport.mutatingRender=()=>(n.tagSupport.render(),r),n.children.push(r)),r.ownerTag=n;const l=r.arrayValue;if(l?.isArrayValueNeverSet){const t={template:r.getTemplate().string,array:e,ownerTagContent:n.lastTemplateString},o="Use html`...`.key(item) instead of html`...` to template an Array";throw console.error(o,t),new C(o,t)}if(t.lastArray.length>s)return st(u.tag.arrayValue,r.arrayValue)?(r.tagSupport=r.tagSupport||u.tag.tagSupport,u.tag.updateByTag(r),[]):[];const p=function(t,e,r,n,o){const a={tag:e,index:n};r.lastArray.push(a);const s={added:o.counts.added+n,removed:o.counts.removed},i=t;return e.buildBeforeElement(i,{counts:s,forceElement:o.forceElement})}(i,r,t,s,o);a.push(...p)})),a}function st(t,e){return t===e||!!(t instanceof Array&&e instanceof Array&&t.length==e.length)&&t.every(((t,r)=>t==e[r]))}function it(t,e,r){e.template=r;const n=e.clone||r;e.lastValue=t;const o=function(t,e){const r=e.parentNode;void 0!==t&&!1!==t&&null!==t||(t="");const n=document.createTextNode(t);return r.insertBefore(n,e),r.removeChild(e),n}(t,n);return e.clone=o,[]}var ut;function ct(t,e,r,n,o){return t.tagSupport||(t.tagSupport=new x({},new $([])),t.tagSupport.mutatingRender=()=>{n.tagSupport.render()},t.tagSupport.oldest=t.tagSupport.oldest||t,n.children.push(t),t.ownerTag=n),e.template=r,et(t,e,r,o)}!function(t){t.tag="tag",t.tagArray="tag-array",t.tagComponent="tag-component",t.value="value"}(ut||(ut={}));const lt=new RegExp('\\s*<template interpolate end id="__tagvar(\\d{1,4})"([^>]*)></template>(\\s*)');function pt(t,e,r,n,o){const a=[];if(!t.hasAttribute("end"))return{clones:a};const s=t.getAttribute("id");if(s?.substring(0,bt.length)!==bt)return{clones:a};const i=e[s];return y(i.value)?{clones:a,tagComponent:{ownerTag:r,subject:i,insertBefore:t}}:(gt(t,i,r,a,n,{isForceElement:o.forceElement}),{clones:a})}function gt(t,e,r,a,i,{isForceElement:u}){const c=e.subscribe((c=>{const l=e.clone;l&&(t=l);const p=function(t,e,r,a,i){const u=function(t){return y(t)?ut.tagComponent:w(t)?ut.tag:v(t)?ut.tagArray:ut.value}(t);switch(u){case ut.tag:return ct(t,e,r,a,i);case ut.tagArray:return at(e,t,r,a,i);case ut.tagComponent:return function(t,e,r,a,i){if(!0!==t.tagged){let e=t.wrapper.original.name||t.wrapper.original.constructor?.name;"Function"===e&&(e=void 0);const r=e||t.wrapper.original.toString().substring(0,120);throw new Error(`Not a tag component. Wrap your function with tag(). Example tag(props => html\`\`) on component:\n\n${r}\n\n`)}const u=t;u.insertBefore=r;const c=t.tagSupport;let l=u.newest;return n.memory.providerConfig.ownerTag=a,(!l||i.forceElement)&&(l?s(c,u.oldest):o(c,a),l=u.renderWithSupport(c,e.tag,a).retag,u.newest=l),a.children.push(l),c.templater=l.tagSupport.templater,et(l,e,r,i)}(t,e,r,a,i)}return it(t,e,r)}(c,e,t,r,{counts:{...i},forceElement:u});u&&(u=!1),a.push(...p)}));return r.cloneSubs.push(c),a}function dt(t,e,r,n){if(!t.getAttribute)return;"TEXTAREA"===t.nodeName&&function(t,e,r){const n=t.value;if(n.search(lt)>=0){const o=n.match(/__tagvar(\d{1,4})/),a="{"+(o?o[0]:"")+"}";t.value="",t.setAttribute("text-var-value",a);const s=(e,r,n)=>t.value=n;J("text-var-value",a,t,e,r,s)}}(t,r,n);let o=e.counts.added;e.forceElement||(o=function(t,e){const r=t.oninit;if(!r)return e.added;const n=r.tagFunction;if(!n)return e.added;const o=n.tagFunction;return o?(o({target:t,stagger:e.added}),++e.added):e.added}(t,e.counts)-o),t.children&&(e.counts.added,e.counts.removed,new Array(...t.children).forEach(((t,o)=>dt(t,{...e,counts:e.counts},r,n))))}function ft(t,e,r,n,o){if(!o||"TEMPLATE"===t.tagName)return{clones:[],tagComponents:[]};const a=n.counts,s=[],i=[];return new Array(...o).forEach((t=>{const{clones:o,tagComponent:u}=pt(t,e,r,a,n);s.push(...o),u?i.push(u):t.children&&new Array(...t.children).forEach(((t,o)=>{if(function(t){return"TEMPLATE"===t.tagName&&void 0!==t.getAttribute("interpolate")&&void 0!==t.getAttribute("end")}(t)){const{tagComponent:o}=pt(t,e,r,a,n);o&&i.push(o)}const{clones:u,tagComponents:c}=ft(t,e,r,n,t.children);s.push(...u),i.push(...c)}))})),{clones:s,tagComponents:i}}function ht(t,e,r,n,o){const a=[],s=[],i=r.interpolation,u=t.children[0].content.children;if(i.keys.length){const{clones:r,tagComponents:i}=ft(t,e,n,o,u);a.push(...r),s.push(...i)}return Z(t,e,n),mt(u,e,n),{clones:a,tagComponents:s}}function mt(t,e,r){new Array(...t).forEach((t=>{Z(t,e,r),t.children&&mt(t.children,e,r)}))}function yt(t){const e=function(t){const e=[];return{string:t.replace(tt,((t,r)=>{if(t.startsWith("<"))return t;const n=r.substring(1,r.length-1);return e.push(n),`<template interpolate end id="${n}"></template>`})),keys:e}}(t);return e.string=e.string.replace(Ct,bt),e}function wt(t,e,r){t.redraw=()=>{const n=e.tag,o=n?.tagSupport||t.tagSupport,{retag:a}=t.renderWithSupport(o,n,r);return e.set(t),a}}const bt="__tagvar",vt="--"+bt+"--",St=new RegExp(bt,"g"),Ct=new RegExp(vt,"g");class Et{isArrayValueNeverSet=!0}class Tt{strings;values;isTag=!0;clones=[];cloneSubs=[];children=[];tagSupport;ownerTag;appElement;arrayValue=new Et;constructor(t,e){this.strings=t,this.values=e}key(t){return this.arrayValue=t,this}async destroy(t={stagger:0,byParent:!1}){!!this.tagSupport&&function(t,e){n.tagUse.forEach((r=>r.beforeDestroy(t,e)))}(this.tagSupport,this),this.destroySubscriptions();const e=this.children.map((e=>e.destroy({...t,byParent:!0})));return this.children.length=0,this.ownerTag&&(this.ownerTag.children=this.ownerTag.children.filter((t=>t!==this))),t.byParent||(t.stagger=await this.destroyClones(t)),await Promise.all(e),t.stagger}destroySubscriptions(){this.cloneSubs.forEach((t=>t.unsubscribe())),this.cloneSubs.length=0}async destroyClones({stagger:t}={stagger:0}){let e=!1;const r=this.clones.reverse().map(((r,n)=>{let o;r.ondestroy&&(o=function(t,e){const r=t.ondestroy;if(!r)return;const n=r.tagFunction;if(!n)return;const o=n.tagFunction;return o?o({target:t,stagger:e}):void 0}(r,t));const a=()=>{r.parentNode?.removeChild(r);const t=this.ownerTag;t&&(t.clones=t.clones.filter((t=>t!==r)))};return o instanceof Promise?(e=!0,o.then(a)):a(),o}));return this.clones.length=0,e&&await Promise.all(r),t}updateByTag(t){this.updateConfig(t.strings,t.values),this.tagSupport.templater=t.tagSupport.templater,this.tagSupport.propsConfig={...t.tagSupport.propsConfig},this.tagSupport.newest=t,this.tagSupport.templater.newest=t}lastTemplateString=void 0;updateConfig(t,e){this.strings=t,this.updateValues(e)}getTemplate(){const t=yt(this.strings.map(((t,e)=>(t.replace(St,vt)+(this.values.length>e?`{${bt}${e}}`:"")).replace(/>\s*/g,">").replace(/\s*</g,"<"))).join(""));return this.lastTemplateString=t.string,{interpolation:t,string:t.string,strings:this.strings,values:this.values,context:this.tagSupport?.memory.context||{}}}isLikeTag(t){const{string:e}=t.getTemplate();if(!this.lastTemplateString)throw new Error("no template here");return e===this.lastTemplateString&&t.values.length===this.values.length&&!!t.values.every(((t,e)=>{const r=this.values[e];return!(t instanceof Function&&r instanceof Function)||!(t.toString()!==r.toString())}))}update(){return this.updateContext(this.tagSupport.memory.context)}updateValues(t){return this.values=t,this.updateContext(this.tagSupport.memory.context)}updateContext(t){const e=[];return this.strings.map(((r,n)=>{const o=bt+n,u=this.values.length>n,c=this.values[n],l=o in t;if(e.push(o),l)return function(t,e,r){const n=t.value,o=t,u=t,c=o.isChildSubject,l=y(e);if(c&&(e=e.value),rt(t,e),l)return function(t,e,r,n){let o=r.tag;if(!o)return wt(e,r,t),void e.redraw();const a=o.tagSupport.templater.wrapper,s=e.wrapper;let u=!1;a&&s&&(u=a.original===s.original);const c=e.tagSupport.propsConfig.latest,l=o.tagSupport;if(l.propsConfig.latest=c,u){const t=n?.tagSupport;let r=t.props;if(w(t.props)||(r=i(t.props)),o&&!p(l,e.tagSupport))return}else nt(o,r);wt(e,r,t),l.templater=e;const g=e.redraw();o.isLikeTag(g)||(o.destroy(),r.tagSupport=g.tagSupport,r.tag=g,l.oldest=g),l.newest=g,l.propsConfig={...e.tagSupport.propsConfig}}(r,e,u,n);const g=u.tag;if(g)!function(t,e,r,n){const o=t.tagSupport.templater.wrapper,i=r?.wrapper,u=o&&i&&o?.original===i?.original,c=r&&t.lastTemplateString===r.lastTemplateString,l=r&&r.getTemplate&&t.isLikeTag(r);c||l?ct(r,e,e.template,n,{counts:{added:0,removed:0}}):u?function(t,e,r){const n=e.tagSupport,o=n.oldest;n.newest,s(o.tagSupport,o);const i=t.wrapper(n);t.newest=i,n.newest=i,a(o.tagSupport,o),e.updateByTag(i),r.set(t)}(r,t,e):it(r,e,e.template)}(g,t,e,r);else if(v(e)){const n=at(t,e,o.template||u.tag?.tagSupport.templater.insertBefore,r,{counts:{added:0,removed:0}});r.clones.push(...n)}else e instanceof Function?u.set(U(e,r)):b(e)?u.set(e.value):u.set(e)}(t[o],c,this);!function(t,e,r,n,o){if(y(e))wt(e,r[n]=new $(e),o);else if(e instanceof Function)r[n]=function(t,e){return new $(U(t,e))}(e,o);else if(t)w(e)?(e.ownerTag=o,o.children.push(e),r[n]=new $(e)):b(e)?r[n]=e:r[n]=new $(e)}(u,c,t,o,this)})),Object.entries(t).forEach((([t,r])=>{e.includes(t)||rt(r,void 0)})),t}getAppElement(){let t=this;for(;t.ownerTag;)t=t.ownerTag;return t}rebuild(){const t=this.tagSupport.templater.insertBefore;if(!t){const t=new Error("Cannot rebuild. Previous insertBefore element is not defined on tag");throw t.tag=this,t}this.buildBeforeElement(t,{forceElement:!0,counts:{added:0,removed:0}})}buildBeforeElement(t,e={forceElement:!1,counts:{added:0,removed:0}}){this.tagSupport.templater.insertBefore=t;const r=this.update(),n=this.getTemplate(),o=document.createElement("div");o.id="tag-temp-holder",o.innerHTML=`<template id="temp-template-tag-wrap">${n.string}</template>`;const{clones:a,tagComponents:s}=ht(o,r,n,this,{forceElement:e.forceElement,counts:e.counts});this.clones.length=0,At(o,t,this,a),this.clones.forEach((t=>dt(t,e,r,this)));let i=e.forceElement;return s.forEach((r=>{gt(r.insertBefore,r.subject,r.ownerTag,a,e.counts,{isForceElement:i}),At(o,t,this,a)})),a}}function At(t,e,r,n,o,a){const s=function(t,e){const r=[];let n=t.children[0].content.firstChild;for(;n;){const t=n.nextSibling;L(n,e),r.push(n),n=t}return r}(t,e);r.clones.push(...s),n.length&&(r.clones=r.clones.filter((t=>!n.find((e=>e===t)))))}function jt(t,...e){return new Tt(t,e)}function Vt(t){const e=n.memory.stateConfig;let r;const o=e.rearray[e.array.length];if(o){let t=A(o);r=e=>[t,t=e];const n={callback:r,lastValue:t,defaultValue:o.defaultValue};return e.array.push(n),T(t,n)}let a=(t instanceof Function?t:()=>t)();r=t=>[a,a=t];const s={callback:r,lastValue:a,defaultValue:a};return e.array.push(s),T(a,s)}function Rt(t,e){let r=Vt(void 0)((t=>[r,r=t]));return void 0===r?(e(t,r),r=t,t):(t.every(((t,e)=>t===r[e]))||(e(t,r),r=t),t)}function xt(t){return n.memory.providerConfig.providers.find((e=>e.constructMethod===t))}n.memory.providerConfig={providers:[],ownerTag:void 0};const Ft={create:t=>{const e=xt(t);if(e)return e.clone=i(e.instance),e.instance;const r=t.constructor?new t:t();return n.memory.providerConfig.providers.push({constructMethod:t,instance:r,clone:i(r)}),r},inject:t=>{const e=xt(t);if(e)return e.instance;const r=n.memory.providerConfig;let o={ownerTag:r.ownerTag};for(;o.ownerTag;){const e=o.ownerTag.tagSupport.memory.providers.find((e=>{if(e.constructMethod===t)return!0}));if(e)return e.clone=i(e.instance),r.providers.push(e),e.instance;o=o.ownerTag}const a=`Could not inject provider: ${t.name} ${t}`;throw console.warn(`${a}. Available providers`,r.providers),new Error(a)}};function kt(t,e){const r=n.memory.providerConfig;r.ownerTag=e,t.memory.providers.length&&(r.providers.length=0,r.providers.push(...t.memory.providers))}function Bt(t){const e=n.memory.stateConfig,[r]=t(void 0);t(r);const o=e.rearray[e.array.length];if(o){let n=o.watch,a=A(o);const s={callback:t,lastValue:a,watch:o.watch};return r!=n&&(s.watch=r,a=s.lastValue=r),e.array.push(s),t(a),a}const a={callback:t,lastValue:r,watch:r};return e.array.push(a),r}function Ot(t){n.memory.initCurrentSupport=t}function Pt(t){n.memory.initCurrentSupport.memory.init||(n.memory.initCurrentSupport.memory.init=t,t())}let Nt;function $t(t){Nt.memory.destroyCallback=t}n({beforeRender:(t,e)=>{kt(t,e)},beforeRedraw:(t,e)=>{kt(t,e.ownerTag)},afterRender:t=>{const e=n.memory.providerConfig;t.memory.providers=[...e.providers],e.providers.length=0}}),n({beforeRender:t=>Ot(t),beforeRedraw:t=>Ot(t)}),n({beforeRender:t=>Nt=t,beforeRedraw:t=>Nt=t,beforeDestroy:(t,e)=>{const r=t.memory.destroyCallback;r&&r()}});let Ut=()=>t=>()=>{throw new Error("The real callback function was called and that should never occur")};function _t(t,e){t.forEach(((t,r)=>{const n=A(t),o=e[r].callback;o&&o(n),e[r].lastValue=n}))}function Mt(t){Ut=()=>{const e=n.memory.stateConfig.array;return r=>(...n)=>function(t,e,r,...n){const o=t.memory.state.newest;_t(o,r);const a=e(...n);_t(r,o),t.render(),a instanceof Promise&&a.finally((()=>{_t(r,o),t.render()}))}(t,r,e,...n)}}n({beforeRender:t=>Mt(t),beforeRedraw:t=>Mt(t)});const Wt={tagElement:m,redrawTag:r};var It=e.W9,Lt=e.ei,Dt=e.B7,Kt=e.vw,zt=e.PC,Ht=e.uX,Jt=e.Ot,Xt=e.eQ,qt=e.cF,Gt=e.qy,Qt=e.u2,Yt=e.zl,Zt=e._4,te=e.iO,ee=e.mn,re=e.Yn,ne=e.sA,oe=e.MG,ae=e.r5,se=e.iz,ie=e.vJ,ue=e.hZ,ce=e.Un,le=e.Ie,pe=e.AI,ge=e.Tc,de=e.Hf,fe=e._A,he=e.wB;export{It as ArrayNoKeyError,Lt as StateMismatchError,Dt as Subject,Kt as Tag,zt as TagError,Ht as TagSupport,Jt as ValueSubject,Xt as getCallback,qt as hmr,Gt as html,Qt as interpolateElement,Yt as interpolateString,Zt as isSubjectInstance,te as isTagArray,ee as isTagComponent,re as isTagInstance,ne as onDestroy,oe as onInit,ae as providers,se as redrawTag,ie as runBeforeRender,ue as set,ce as setLet,le as setProp,pe as setUse,ge as tag,de as tagElement,fe as tags,he as watch};