import{__decorate as t}from"tslib";import{html as i}from"lit";import{state as s,query as e,property as h,customElement as r}from"lit/decorators.js";import{s as o}from"./nile-grid.css.esm.js";import{N as l}from"../internal/nile-element.esm.js";import{s as a,c as n,a as d,u as c,r as u,b as m,l as v,d as p,g as b,e as y,f}from"./nile-grid.width.esm.js";import"../internal/accessibility/a11y.state.enum.esm.js";import"../internal/accessibility/a11y.property.enum.esm.js";import"../internal/accessibility/role.enum.esm.js";import"./types/nile-grid.enums.esm.js";let w=class extends l{static get styles(){return[o]}constructor(){super(),this.columnWidths=[],this.enableScroll=!1,this.hoverable=!1,this.overflowScrollBehaviour="none",this.raf=0,this.minColumnWidth=40,this.stickyLeftIndexes=[],this.stickyRightIndexes=[],this.cellHeight=null,this.headHeight=null,this.widthsInitialized=!1,this.lockedWidthColumns=[],this.prefixSumColumnsWidth=[],this.stickyColumns=[],this.bodyRows=[],this.headRows=[],this.needsStructureLayout=!1,this.needsWidthLayout=!1,this.needsSpanLayout=!1,this.colCount=0,this.isGridReady=!1,this.stickyLeftHeadEls=[],this.stickyRightHeadEls=[],this.stickyLeftBodyEls=[],this.stickyRightBodyEls=[],this.resizeTimer=0,this.shadowRaf=0,this.resizeBaseColumnWidths=[],this.syncStickyShadowsRaf=()=>{this.shadowRaf||(this.shadowRaf=requestAnimationFrame((()=>{this.shadowRaf=0,a(this)})))},this.onResize=t=>{t.stopPropagation(),this.emit("nile-resize",{columnNumber:t.detail.col,columnWidth:t.detail.widthPx});const{col:i,widthPx:s}=t.detail;if(!this.resizeBaseColumnWidths)return;n(this.columnWidths,this.resizeBaseColumnWidths);const{prevW:e,newW:h,delta0:r}=d({target:t.target,col:i,widthPx:s,floor:this.minColumnWidth,columnWidths:this.columnWidths});0!==r?(this.enableScroll?m({grid:this,col:i,newW:h}):u({grid:this,col:i,prevW:e,newW:h,floor:this.minColumnWidth}),c(this,i)):c(this,i)}}connectedCallback(){super.connectedCallback(),this.emit("nile-init")}updateOverscrollBehavior(){const t=this.scrollHeight>this.clientHeight;this.style.overscrollBehavior=t?this.overflowScrollBehaviour:""}scheduleLayout(){this.raf&&cancelAnimationFrame(this.raf),this.raf=requestAnimationFrame((()=>{this.raf=0,v(this,this.slotEl,this.ensureWidths.bind(this),this.stickyLeftIndexes,this.stickyRightIndexes),a(this)}))}ensureWidths(t){this.columnWidths.length!==t&&(this.columnWidths=Array(t).fill(0),this.lockedWidthColumns=Array(t).fill(!1))}firstUpdated(){this.setAttribute("role","table"),p(this,this.onResize,this.slotEl,this._attachObserverAndLayout.bind(this)),this.addEventListener("scroll",this.syncStickyShadowsRaf),this.resizeObserverStickyShadows=new ResizeObserver((()=>{a(this),this.updateOverscrollBehavior()})),this.resizeObserverStickyShadows.observe(this),this.syncStickyShadowsRaf()}updated(t){t.has("overflowScrollBehaviour")&&this.updateOverscrollBehavior()}_attachObserverAndLayout(){this.mo?.disconnect(),this.mo=new MutationObserver((()=>{this.needsStructureLayout=!0,this.needsWidthLayout=!0,this.needsSpanLayout=!0,this.scheduleLayout()})),this.resizeObserver?.disconnect(),this.resizeObserver=new ResizeObserver((()=>{this.resizeTimer&&clearTimeout(this.resizeTimer),this.resizeTimer=setTimeout((()=>{this.resizeTimer=0,this.needsWidthLayout=!0,this.scheduleLayout()}),16)}));const t=b(this.slotEl),i=y(this.slotEl);if(!t&&!i)return;const s=t??this;this.mo.observe(s,{childList:!0,subtree:!0,attributes:!0,attributeFilter:["colspan","rowspan","width","lockwidth","sticky"]}),this.resizeObserver?.observe(this),this.colCount&&this.columnWidths?.length||(this.needsStructureLayout=!0,this.needsSpanLayout=!0,this.needsWidthLayout=!0,this.scheduleLayout())}render(){return i`
      <div part="grid-base" class="nile-grid-base">
        <div part="grid-slot-wrapper">
          <slot part="grid-slot"></slot>
        </div>
        <div part="resize-start" class="nile-resize-start"></div>
        <div part="resize-end" class="nile-resize-end"></div>
      </div>
    `}disconnectedCallback(){this.emit("nile-destroy"),super.disconnectedCallback?.(),this.mo?.disconnect(),this.resizeObserver?.disconnect(),this.resizeObserverStickyShadows?.disconnect(),this.raf&&cancelAnimationFrame(this.raf),f(this,this.onResize,this.slotEl,this._attachObserverAndLayout.bind(this))}};t([s()],w.prototype,"columnWidths",void 0),t([e("slot")],w.prototype,"slotEl",void 0),t([e(".nile-resize-start")],w.prototype,"resizeStart",void 0),t([e(".nile-resize-end")],w.prototype,"resizeEnd",void 0),t([e(".shadow-left")],w.prototype,"shadowLeft",void 0),t([e(".shadow-right")],w.prototype,"shadowRight",void 0),t([h({type:Boolean,attribute:!0,reflect:!0})],w.prototype,"enableScroll",void 0),t([h({type:Boolean,attribute:!0,reflect:!0})],w.prototype,"hoverable",void 0),t([h({type:String,attribute:!0,reflect:!0})],w.prototype,"overflowScrollBehaviour",void 0),t([h({type:Number,reflect:!0,attribute:!0})],w.prototype,"minColumnWidth",void 0),t([s()],w.prototype,"stickyLeftIndexes",void 0),t([s()],w.prototype,"stickyRightIndexes",void 0),t([s()],w.prototype,"cellHeight",void 0),t([s()],w.prototype,"headHeight",void 0),t([s()],w.prototype,"widthsInitialized",void 0),t([s()],w.prototype,"lockedWidthColumns",void 0),t([s()],w.prototype,"prefixSumColumnsWidth",void 0),t([s()],w.prototype,"stickyColumns",void 0),t([s()],w.prototype,"bodyRows",void 0),t([s()],w.prototype,"headRows",void 0),t([s()],w.prototype,"needsStructureLayout",void 0),t([s()],w.prototype,"needsWidthLayout",void 0),t([s()],w.prototype,"needsSpanLayout",void 0),t([s()],w.prototype,"colCount",void 0),w=t([r("nile-grid")],w);export{w as N};
