import{r as t,e as i,w as s,f as e,h,H as r,c as n}from"./p-38dcedc4.js";import{c as o}from"./p-5a5a5c96.js";const a=(t,i)=>{const s=l(t,i);return s&&t.ownerDocument?t.ownerDocument.importNode(s.content,!0).children[0]:null},l=(t,i)=>{switch(i){case"item":return t.querySelector("template:not([name])");case"header":return t.querySelector("template[name=header]");case"footer":return t.querySelector("template[name=footer]")}},c=(t,i,s,e,h,r,n,o,a,l,c,u)=>{const d=[],m=u+c;for(let u=c;u{this.updateVirtualScroll()}}itemsChanged(){this.calcCells(),this.updateVirtualScroll()}async connectedCallback(){const t=this.el.closest("ion-content");t?(this.scrollEl=await t.getScrollElement(),this.contentEl=t,this.calcCells(),this.updateState()):console.error(" must be used inside an ")}componentDidUpdate(){this.updateState()}disconnectedCallback(){this.scrollEl=void 0}onResize(){this.calcCells(),this.updateVirtualScroll()}positionForItem(t){return Promise.resolve(((t,i,s)=>{const e=i.find((i=>"item"===i.type&&i.index===t));return e?s[e.i]:-1})(t,this.cells,this.getHeightIndex()))}async checkRange(t,i=-1){if(!this.items)return;const s=-1===i?this.items.length-t:i,e=((t,i)=>0===i?0:i===(t.length>0?t[t.length-1].index:0)+1?t.length:t.findIndex((t=>t.index===i)))(this.cells,t),h=c(this.items,this.itemHeight,this.headerHeight,this.footerHeight,this.headerFn,this.footerFn,this.approxHeaderHeight,this.approxFooterHeight,this.approxItemHeight,e,t,s);this.cells=((t,i,s)=>{if(0===s&&i.length>=t.length)return i;for(let e=0;e{const s=i.top,e=i.bottom;let h=0;for(;hs);h++);const r=Math.max(h-2-1,0);for(;h=e);h++);return{offset:r,length:Math.min(h+2,t.length)-r}})(s,i);var r,n;((t,i,s)=>t<=s.offset+s.length||i.offset!==s.offset||i.length!==s.length)(t,this.range,h)&&(this.range=h,((t,i,s,e)=>{for(const i of t)i.change=0,i.d=!0;const h=[],r=e.offset+e.length;for(let n=e.offset;nt.d&&t.cell===e));if(r){const t=i[n];t!==r.top&&(r.top=t,r.change=1),r.d=!1}else h.push(e)}const n=t.filter((t=>t.d));for(const s of h){const e=n.find((t=>t.d&&t.cell.type===s.type)),h=s.i;e?(e.d=!1,e.change=2,e.cell=s,e.top=i[h]):t.push({d:!1,cell:s,visible:!0,change:2,top:i[h]})}t.filter((t=>t.d&&-9999!==t.top)).forEach((t=>{t.change=1,t.top=-9999}))})(this.virtualDom,s,this.cells,h),this.nodeRender?((t,i,s,e)=>{const h=Array.from(t.children).filter((t=>"TEMPLATE"!==t.tagName)),r=h.length;let n;for(let o=0;o0&&(e(c,n),c.reads--)}})(this.el,this.nodeRender,this.virtualDom,this.updateCellHeight.bind(this)):this.domRender?this.domRender(this.virtualDom):this.renderItem&&e(this))}updateCellHeight(t,i){const s=()=>{if(i.$ionCell===t){const s=window.getComputedStyle(i),e=i.offsetHeight+parseFloat(s.getPropertyValue("margin-bottom"));this.setCellHeight(t,e)}};i?o(i,s):s()}setCellHeight(t,i){const s=t.i;t===this.cells[s]&&(t.height===i&&!0===t.visible||(t.visible=!0,t.height=i,this.indexDirty=Math.min(this.indexDirty,s),this.scheduleUpdate()))}scheduleUpdate(){clearTimeout(this.timerUpdate),this.timerUpdate=setTimeout((()=>this.updateVirtualScroll()),100)}updateState(){const t=!(!this.scrollEl||!this.cells);t!==this.isEnabled&&(this.enableScrollEvents(t),t&&this.updateVirtualScroll())}calcCells(){this.items&&(this.lastItemLen=this.items.length,this.cells=c(this.items,this.itemHeight,this.headerHeight,this.footerHeight,this.headerFn,this.footerFn,this.approxHeaderHeight,this.approxFooterHeight,this.approxItemHeight,0,0,this.lastItemLen),this.indexDirty=0)}getHeightIndex(){return this.indexDirty!==1/0&&this.calcHeightIndex(this.indexDirty),this.heightIndex}calcHeightIndex(t=0){this.heightIndex=((t,i)=>{if(!t)return new Uint32Array(i);if(t.length===i)return t;if(i>t.length){const s=new Uint32Array(i);return s.set(t),s}return t.subarray(0,i)})(this.heightIndex,this.cells.length),this.totalHeight=((t,i,s)=>{let e=t[s];for(let h=s;h{i.removeEventListener("scroll",this.onScroll)})}renderVirtualNode(t){const{type:i,value:s,index:e}=t.cell;switch(i){case"item":return this.renderItem(s,e);case"header":return this.renderHeader(s,e);case"footer":return this.renderFooter(s,e)}}render(){return h(r,{style:{height:`${this.totalHeight}px`}},this.renderItem&&h(d,{dom:this.virtualDom},this.virtualDom.map((t=>this.renderVirtualNode(t)))))}get el(){return n(this)}static get watchers(){return{itemHeight:["itemsChanged"],headerHeight:["itemsChanged"],footerHeight:["itemsChanged"],items:["itemsChanged"]}}},d=({dom:t},i,s)=>s.map(i,((i,s)=>{const e=t[s],h=i.vattrs||{};let r=h.class||"";return r+="virtual-item ",e.visible||(r+="virtual-loading"),Object.assign(Object.assign({},i),{vattrs:Object.assign(Object.assign({},h),{class:r,style:Object.assign(Object.assign({},h.style),{transform:`translate3d(0,${e.top}px,0)`})})})}));u.style="ion-virtual-scroll{display:block;position:relative;width:100%;contain:strict;user-select:none}ion-virtual-scroll>.virtual-loading{opacity:0}ion-virtual-scroll>.virtual-item{position:absolute !important;top:0 !important;right:0 !important;left:0 !important;transition-duration:0ms;will-change:transform}";export{u as ion_virtual_scroll}