// @bun
var{defineProperty:w,getOwnPropertyNames:h,getOwnPropertyDescriptor:u}=Object,I=Object.prototype.hasOwnProperty;function g(z){return this[z]}var r=(z)=>{var A=(b??=new WeakMap).get(z),F;if(A)return A;if(A=w({},"__esModule",{value:!0}),z&&typeof z==="object"||typeof z==="function"){for(var K of h(z))if(!I.call(A,K))w(A,K,{get:g.bind(z,K),enumerable:!(F=u(z,K))||F.enumerable})}return b.set(z,A),A},b;var x=(z)=>z;function d(z,A){this[z]=x.bind(null,A)}var l=(z,A)=>{for(var F in A)w(z,F,{get:A[F],enumerable:!0,configurable:!0,set:d.bind(A,F)})};var c=(z,A)=>()=>(z&&(A=z(z=0)),A);var n=import.meta.require;var p={};l(p,{useVirtualList:()=>E,processVirtualListDirectives:()=>R,processVirtualGridDirectives:()=>v,processInfiniteListDirectives:()=>f,generateVirtualListCSS:()=>L,default:()=>a,createVirtualList:()=>C});function C(z,A){let{itemHeight:F,containerHeight:K,overscan:P=3,keyProperty:$="id",dynamicHeight:U=!1,estimatedItemHeight:W=F,direction:G="vertical"}=A,k=[...z],j=0,B=new Map;function _(){if(U)return Y();let q=Math.max(0,Math.floor(j/F)-P),J=Math.ceil(K/F),X=Math.min(k.length-1,q+J+P*2);return{startIndex:q,endIndex:X}}function Y(){let q=0,J=0,X=k.length-1;for(let Z=0;Z<k.length;Z++){let V=B.get(Z)||W;if(q+V>j){J=Math.max(0,Z-P);break}q+=V}q=0;for(let Z=0;Z<k.length;Z++){let V=B.get(Z)||W;if(q+=V,q>j+K){X=Math.min(k.length-1,Z+P);break}}return{startIndex:J,endIndex:X}}function S(){if(U){let q=0;for(let J=0;J<k.length;J++)q+=B.get(J)||W;return q}return k.length*F}function O(q){if(U){let J=0;for(let X=0;X<q;X++)J+=B.get(X)||W;return J}return q*F}function N(){let{startIndex:q,endIndex:J}=_();return{startIndex:q,endIndex:J,scrollTop:j,totalHeight:S(),visibleItems:k.slice(q,J+1),itemHeights:new Map(B)}}function T(q,J="auto"){let X=O(q);Q(X,J)}function Q(q,J="auto"){j=Math.max(0,Math.min(q,S()-K))}function M(){_()}function y(q){k=[...q],B.clear(),M()}function D(){k=[],B.clear()}return{scrollToIndex:T,scrollToOffset:Q,getState:N,refresh:M,setItems:y,destroy:D}}function R(z,A={},F){let K=/@virtualList\s*\(\s*([^,]+)\s*(?:,\s*(\{[^}]*\}))?\s*\)\s*([\s\S]*?)@endvirtualList/gi;return z.replace(K,(P,$,U,W)=>{let G=$.trim(),k=A[G];if(!k||!Array.isArray(k))return`<!-- virtualList: items "${G}" not found or not an array -->`;let j={itemHeight:50,containerHeight:400,overscan:3};if(U)try{let Q=U.trim(),M=Function(`return ${Q}`)();j={...j,...M}}catch{}let B=`vlist-${Math.random().toString(36).substring(2,9)}`,_=Math.ceil(j.containerHeight/j.itemHeight),Y=j.overscan||3,S=0,O=Math.min(k.length-1,_+Y),N="";for(let Q=S;Q<=O&&Q<k.length;Q++){let M=k[Q],y=W.replace(/\{\{\s*item\s*\}\}/g,typeof M==="object"?JSON.stringify(M):String(M)).replace(/\{\{\s*item\.(\w+)\s*\}\}/g,(D,q)=>{if(typeof M==="object"&&M!==null)return String(M[q]??"");return""}).replace(/\{\{\s*index\s*\}\}/g,String(Q));N+=`
        <div class="virtual-item ${j.itemClass||""}"
             style="position: absolute; top: ${Q*j.itemHeight}px; height: ${j.itemHeight}px; width: 100%;"
             data-index="${Q}">
          ${y}
        </div>`}let T=k.length*j.itemHeight;return`
      <div id="${B}"
           class="virtual-list-container ${j.containerClass||""}"
           style="position: relative; height: ${j.containerHeight}px; overflow-y: auto;"
           data-item-height="${j.itemHeight}"
           data-total-items="${k.length}"
           data-overscan="${Y}">
        <div class="virtual-list-viewport ${j.viewportClass||""}"
             style="position: relative; height: ${T}px; width: 100%;">
          ${N}
        </div>
      </div>
      <script data-virtual-list="${B}">
        (function() {
          const container = document.getElementById('${B}');
          if (!container) return;

          const viewport = container.querySelector('.virtual-list-viewport');
          const itemHeight = ${j.itemHeight};
          const totalItems = ${k.length};
          const overscan = ${Y};
          const containerHeight = ${j.containerHeight};
          const items = ${JSON.stringify(k)};
          const template = ${JSON.stringify(W.trim())};

          let lastStartIndex = 0;

          function renderItems(scrollTop) {
            const startIndex = Math.max(0, Math.floor(scrollTop / itemHeight) - overscan);
            const visibleCount = Math.ceil(containerHeight / itemHeight);
            const endIndex = Math.min(totalItems - 1, startIndex + visibleCount + overscan * 2);

            if (startIndex === lastStartIndex) return;
            lastStartIndex = startIndex;

            // Clear and re-render
            viewport.innerHTML = '';

            for (let i = startIndex; i <= endIndex; i++) {
              const item = items[i];
              const div = document.createElement('div');
              div.className = 'virtual-item ${j.itemClass||""}';
              div.style.cssText = 'position: absolute; top: ' + (i * itemHeight) + 'px; height: ' + itemHeight + 'px; width: 100%;';
              div.dataset.index = i;

              let html = template
                .replace(/\\{\\{\\s*item\\s*\\}\\}/g, typeof item === 'object' ? JSON.stringify(item) : String(item))
                .replace(/\\{\\{\\s*item\\.(\\w+)\\s*\\}\\}/g, (_, prop) => {
                  return typeof item === 'object' && item !== null ? String(item[prop] ?? '') : '';
                })
                .replace(/\\{\\{\\s*index\\s*\\}\\}/g, String(i));

              div.innerHTML = html;
              viewport.appendChild(div);
            }
          }

          container.addEventListener('scroll', function() {
            requestAnimationFrame(() => renderItems(container.scrollTop));
          });

          // Expose API
          container.__virtualList = {
            scrollToIndex: function(index) {
              container.scrollTop = index * itemHeight;
            },
            refresh: function() {
              renderItems(container.scrollTop);
            }
          };
        })();
      </script>
    `})}function L(){return`
    .virtual-list-container {
      position: relative;
      overflow-y: auto;
      overflow-x: hidden;
    }

    .virtual-list-container::-webkit-scrollbar {
      width: 8px;
    }

    .virtual-list-container::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 4px;
    }

    .virtual-list-container::-webkit-scrollbar-thumb {
      background: rgba(0, 0, 0, 0.3);
      border-radius: 4px;
    }

    .virtual-list-container::-webkit-scrollbar-thumb:hover {
      background: rgba(0, 0, 0, 0.5);
    }

    .virtual-list-viewport {
      position: relative;
      width: 100%;
    }

    .virtual-item {
      position: absolute;
      left: 0;
      right: 0;
      box-sizing: border-box;
    }

    /* Loading placeholder for items */
    .virtual-item-placeholder {
      background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
      background-size: 200% 100%;
      animation: virtual-shimmer 1.5s infinite;
    }

    @keyframes virtual-shimmer {
      0% { background-position: 200% 0; }
      100% { background-position: -200% 0; }
    }
  `}function E(z,A){let F=null,K=()=>{if(!F)F=C(z(),A);return F};return{visibleItems:()=>{return K().getState().visibleItems},totalHeight:()=>{return K().getState().totalHeight},containerProps:()=>({style:{height:`${A.containerHeight}px`,overflow:"auto",position:"relative"},onScroll:(P)=>{let $=P.target;if(F)F.scrollToOffset($.scrollTop)}}),itemProps:(P)=>({style:{position:"absolute",top:`${P*A.itemHeight}px`,height:`${A.itemHeight}px`,width:"100%"},"data-index":P}),scrollToIndex:(P)=>{K().scrollToIndex(P)}}}function f(z,A={},F){let K=/@infiniteList\s*\(\s*([^,]+)\s*(?:,\s*(\{[^}]*\}))?\s*\)\s*([\s\S]*?)@loading\s*([\s\S]*?)@endinfiniteList/gi;return z.replace(K,(P,$,U,W,G)=>{let k=$.trim(),j=A[k];if(!j||!Array.isArray(j))return`<!-- infiniteList: items "${k}" not found or not an array -->`;let B={itemHeight:50,containerHeight:400,overscan:3,threshold:100,hasMore:!0};if(U)try{let S=Function(`return ${U.trim()}`)();B={...B,...S}}catch{}let _=`ilist-${Math.random().toString(36).substring(2,9)}`,Y=R(`@virtualList(${k}, ${JSON.stringify({itemHeight:B.itemHeight,containerHeight:B.containerHeight,overscan:B.overscan,containerClass:B.containerClass})})${W}@endvirtualList`,A);return Y=Y.replace("</script>",`
          // Infinite scroll detection
          container.addEventListener('scroll', function() {
            const scrollBottom = container.scrollTop + container.clientHeight;
            const threshold = ${B.threshold};
            const totalHeight = viewport.offsetHeight;

            if (scrollBottom >= totalHeight - threshold) {
              container.dispatchEvent(new CustomEvent('loadmore', { bubbles: true }));
            }
          });
        </script>`),`
      <div id="${_}" class="infinite-list-wrapper">
        ${Y}
        <div class="infinite-list-loading" style="display: none;">
          ${G}
        </div>
      </div>
    `})}function v(z,A={},F){let K=/@virtualGrid\s*\(\s*([^,]+)\s*(?:,\s*(\{[^}]*\}))?\s*\)\s*([\s\S]*?)@endvirtualGrid/gi;return z.replace(K,(P,$,U,W)=>{let G=$.trim(),k=A[G];if(!k||!Array.isArray(k))return`<!-- virtualGrid: items "${G}" not found or not an array -->`;let j={columns:3,rowHeight:100,containerHeight:400,gap:10,overscan:2};if(U)try{let M=Function(`return ${U.trim()}`)();j={...j,...M}}catch{}let B=`vgrid-${Math.random().toString(36).substring(2,9)}`,_=Math.ceil(k.length/j.columns),Y=_*(j.rowHeight+(j.gap||0)),S=Math.ceil(j.containerHeight/(j.rowHeight+(j.gap||0))),O=j.overscan||2,N=0,T=Math.min(_-1,S+O),Q="";for(let M=N;M<=T;M++)for(let y=0;y<j.columns;y++){let D=M*j.columns+y;if(D>=k.length)break;let q=k[D],J=y*(100/j.columns),X=M*(j.rowHeight+(j.gap||0)),Z=W.replace(/\{\{\s*item\s*\}\}/g,typeof q==="object"?JSON.stringify(q):String(q)).replace(/\{\{\s*item\.(\w+)\s*\}\}/g,(V,H)=>{if(typeof q==="object"&&q!==null)return String(q[H]??"");return""}).replace(/\{\{\s*index\s*\}\}/g,String(D));Q+=`
          <div class="virtual-grid-item"
               style="position: absolute; top: ${X}px; left: ${J}%; width: calc(${100/j.columns}% - ${j.gap||0}px); height: ${j.rowHeight}px;"
               data-index="${D}">
            ${Z}
          </div>`}return`
      <div id="${B}"
           class="virtual-grid-container"
           style="position: relative; height: ${j.containerHeight}px; overflow-y: auto;">
        <div class="virtual-grid-viewport"
             style="position: relative; height: ${Y}px; width: 100%;">
          ${Q}
        </div>
      </div>
      <script data-virtual-grid="${B}">
        (function() {
          const container = document.getElementById('${B}');
          if (!container) return;

          const viewport = container.querySelector('.virtual-grid-viewport');
          const columns = ${j.columns};
          const rowHeight = ${j.rowHeight};
          const gap = ${j.gap||0};
          const totalItems = ${k.length};
          const overscan = ${O};
          const containerHeight = ${j.containerHeight};
          const items = ${JSON.stringify(k)};
          const template = ${JSON.stringify(W.trim())};

          let lastStartRow = 0;

          function renderItems(scrollTop) {
            const startRow = Math.max(0, Math.floor(scrollTop / (rowHeight + gap)) - overscan);
            const visibleRowCount = Math.ceil(containerHeight / (rowHeight + gap));
            const endRow = Math.min(Math.ceil(totalItems / columns) - 1, startRow + visibleRowCount + overscan * 2);

            if (startRow === lastStartRow) return;
            lastStartRow = startRow;

            viewport.innerHTML = '';

            for (let row = startRow; row <= endRow; row++) {
              for (let col = 0; col < columns; col++) {
                const index = row * columns + col;
                if (index >= totalItems) break;

                const item = items[index];
                const div = document.createElement('div');
                div.className = 'virtual-grid-item';
                div.style.cssText = 'position: absolute; top: ' + (row * (rowHeight + gap)) + 'px; left: ' + (col * (100 / columns)) + '%; width: calc(' + (100 / columns) + '% - ' + gap + 'px); height: ' + rowHeight + 'px;';
                div.dataset.index = index;

                let html = template
                  .replace(/\\{\\{\\s*item\\s*\\}\\}/g, typeof item === 'object' ? JSON.stringify(item) : String(item))
                  .replace(/\\{\\{\\s*item\\.(\\w+)\\s*\\}\\}/g, (_, prop) => {
                    return typeof item === 'object' && item !== null ? String(item[prop] ?? '') : '';
                  })
                  .replace(/\\{\\{\\s*index\\s*\\}\\}/g, String(index));

                div.innerHTML = html;
                viewport.appendChild(div);
              }
            }
          }

          container.addEventListener('scroll', function() {
            requestAnimationFrame(() => renderItems(container.scrollTop));
          });
        })();
      </script>
    `})}var a;var m=c(()=>{a={createVirtualList:C,processVirtualListDirectives:R,processInfiniteListDirectives:f,processVirtualGridDirectives:v,generateVirtualListCSS:L,useVirtualList:E}});m();export{E as useVirtualList,R as processVirtualListDirectives,v as processVirtualGridDirectives,f as processInfiniteListDirectives,L as generateVirtualListCSS,a as default,C as createVirtualList};
