import{processDOM as P,removeBindings as I,Internals as O,DirectiveRegistry as j}from"../index.js";const{block:k,internals:a,repeatCtx:u}=O,H=(e=[],t)=>{e.forEach(n=>{I(t,n),n[a][y]()})},d=Symbol(),y=Symbol(),T=e=>e.forEach(t=>t()),U=(e,t)=>{let n=t,i="";for(;e>1;)e&1&&(i+=n),e>>=1,n+=n;return i+n},_=e=>({tpl:document.createElement("template"),ptr:0,pool:[],alloc(t){const n=t-this.pool.length,{tpl:i}=this;n>0&&(i.innerHTML=U(n,e),this.pool=this.pool.concat(Array.from(i.content.children)))},get(t){return this.ptr+t>this.pool.length&&this.alloc(this.ptr+t-this.pool.length),this.ptr+=t,this.pool.slice(this.ptr-t,this.ptr)},dump(t){return this.ptr-=t,this.ptr<0&&(this.ptr=0),this.pool.slice(this.ptr)}}),x="*repeat",B="*repeat-cleanup",b=new Range;let g=[];const F={attribute:(e,t)=>t===x,process:({targetNode:e,scopeNode:t,expression:n})=>{let i=parseInt(e.getAttribute(B))||5e3;e[k]="abort",e.removeAttribute(x),e.removeAttribute(B);const D=e.outerHTML,E=document.createComment("*repeat"),A=e.parentElement||e.parentNode||t.shadowRoot||t,c=_(D);A.insertBefore(E,e);let m;const v=document.createDocumentFragment();let o=[],R,C;function w(l=[],L=!1){m&&clearTimeout(m);const f=Math.abs(l.length-o.length);c.alloc(l.length),l.length<o.length&&(g=c.dump(f),b.setStartBefore(g[0]),b.setEndAfter(g[f-1]),b.deleteContents(),o.length=l.length);for(let r=0;r<o.length;r++){const p=o[r],h=p[a],s=l[r];(L||p[u]!==s)&&(p[u]=s,T(h[d]))}if(l.length>o.length){const r=c.get(f),p=r[Symbol.iterator]();for(let h=o.length;h<l.length;h++){const s=p.next().value,M=l[h];s[u]=M,s[a]||({bounds:R,clear:C}=P(t,s),Object.assign(s[a],{[d]:R,[y]:C})),T(s[a][d])}o=o.concat(r),v.append(...r),A.insertBefore(v,E)}m=setTimeout(()=>{H(g.concat(),t),c.pool=c.pool.slice(0,c.ptr),g.length=0},i)}return{update:w,removeNode:!0}}};j.add(F,!0);
