text: define(["exports","../../../@polymer/polymer/lib/utils/debounce.js","../../../@polymer/polymer/lib/utils/async.js"],(function(e,t,i){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.DataProviderMixin=e.ItemCache=void 0;
  /**
    @license
    Copyright (c) 2017 Vaadin Ltd.
    This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
    */
  const s=class ItemCache{constructor(e,t,i){this.grid=e,this.parentCache=t,this.parentItem=i,this.itemCaches={},this.items={},this.effectiveSize=0,this.size=0,this.pendingRequests={}}isLoading(){return Object.keys(this.pendingRequests).length||Object.keys(this.itemCaches).filter(e=>this.itemCaches[e].isLoading())[0]}getItemForIndex(e){const{cache:t,scaledIndex:i}=this.getCacheAndIndex(e);return t.items[i]}updateSize(){this.effectiveSize=!this.parentItem||this.grid._isExpanded(this.parentItem)?this.size+Object.keys(this.itemCaches).reduce((e,t)=>{const i=this.itemCaches[t];return i.updateSize(),e+i.effectiveSize},0):0}ensureSubCacheForScaledIndex(e){if(!this.itemCaches[e]){const t=new ItemCache(this.grid,this,this.items[e]);this.itemCaches[e]=t,this.grid._loadPage(0,t)}}getCacheAndIndex(e){let t=e;const i=Object.keys(this.itemCaches);for(var s=0;s<i.length;s++){const e=Number(i[s]),a=this.itemCaches[e];if(t<=e)return{cache:this,scaledIndex:t};if(t<=e+a.effectiveSize)return a.getCacheAndIndex(t-e-1);t-=a.effectiveSize}return{cache:this,scaledIndex:t}}};e.ItemCache=s;e.DataProviderMixin=e=>class DataProviderMixin extends e{static get properties(){return{pageSize:{type:Number,value:50,observer:"_pageSizeChanged"},dataProvider:{type:Object,notify:!0,observer:"_dataProviderChanged"},loading:{type:Boolean,notify:!0,readOnly:!0,reflectToAttribute:!0},_cache:{type:Object,value:function(){return new s(this)}},itemIdPath:{type:String,value:null},expandedItems:{type:Object,notify:!0,value:()=>[]}}}static get observers(){return["_sizeChanged(size)","_expandedItemsChanged(expandedItems.*)"]}_sizeChanged(e){const t=e-this._cache.size;this._cache.size+=t,this._cache.effectiveSize+=t,this._effectiveSize=this._cache.effectiveSize}_updateRowItem(e,t){t.children.forEach(t=>{t._instance&&(t._instance.item=e)})}_getItem(e,t){if(e>=this._effectiveSize)return;t.index=e;const{cache:i,scaledIndex:s}=this._cache.getCacheAndIndex(e),a=i.items[s];a?(this._toggleAttribute("loading",!1,t),this._updateItem(t,a),this._isExpanded(a)&&i.ensureSubCacheForScaledIndex(s)):(this._toggleAttribute("loading",!0,t),this._loadPage(this._getPageForIndex(s),i))}_pagesForPhysicalItems(){return[this._getPageForIndex(this._firstVisibleIndex+this._vidxOffset)].concat(this._physicalItems.filter(e=>e.index).items(e=>this._getPageForIndex(e.index))).reduce((e,t)=>(-1===e.indexOf(t)&&e.push(t),e),[])}_expandedInstanceChangedCallback(e,t){void 0!==e.item&&(t?this.expandItem(e.item):this.collapseItem(e.item))}getItemId(e){return this.itemIdPath?this.get(this.itemIdPath,e):e}_isExpanded(e){return this.expandedItems&&this._getItemIndexInArray(e,this.expandedItems)>-1}_expandedItemsChanged(e){this._cache.updateSize(),this._effectiveSize=this._cache.effectiveSize,this._assignModels()}expandItem(e){this._isExpanded(e)||this.push("expandedItems",e)}collapseItem(e){this._isExpanded(e)&&this.splice("expandedItems",this._getItemIndexInArray(e,this.expandedItems),1)}_getIndexLevel(e){let{cache:t}=this._cache.getCacheAndIndex(e),i=0;for(;t.parentCache;)t=t.parentCache,i++;return i}_canPopulate(){return this._hasData&&this._columnTree}_loadPage(e,t){if(!t.pendingRequests[e]&&this.dataProvider){this._setLoading(!0),t.pendingRequests[e]=!0;const i={page:e,pageSize:this.pageSize,sortOrders:this._mapSorters(),filters:this._mapFilters(),parentItem:t.parentItem};this.dataProvider(i,(s,a)=>{void 0!==a?t.size=a:i.parentItem&&(t.size=s.length),s.forEach((i,s)=>{const a=e*this.pageSize+s;t.items[a]=i,this._isExpanded(i)&&t.ensureSubCacheForScaledIndex(a)}),this._hasData=!0,delete t.pendingRequests[e],this._cache.isLoading()||(this._setLoading(!1),this._cache.updateSize(),this._effectiveSize=this._cache.effectiveSize,Array.from(this.$.items.children).filter(e=>!e.hidden).forEach(e=>{const t=this._cache.getItemForIndex(e.index);t&&(this._toggleAttribute("loading",!1,e),this._updateItem(e,t))}),this._increasePoolIfNeeded(0)),this.__itemsReceived()})}}_getPageForIndex(e){return Math.floor(e/this.pageSize)}clearCache(){this._cache=new s(this),Array.from(this.$.items.children).forEach(e=>{Array.from(e.children).forEach(e=>{e._instance&&e._instance._setPendingProperty("item",{},!1)})}),this._cache.size=this.size||0,this._cache.updateSize(),this._hasData=!1,this._assignModels(),this._effectiveSize||this._loadPage(0,this._cache)}_flushItemsDebouncer(){this._debouncerLoad&&this._debouncerLoad.flush()}_pageSizeChanged(e,t){void 0!==t&&e!==t&&this.clearCache()}_checkSize(){void 0===this.size&&0===this._effectiveSize&&console.warn("The <vaadin-grid> needs the total number of items in order to display rows. Set the total number of items to the `size` property, or provide the total number of items in the second argument of the `dataProvider`’s `callback` call.")}_dataProviderChanged(e,s){void 0!==s&&this.clearCache(),e&&this.items&&this.items.length&&this._scrollToIndex(this._firstVisibleIndex),this._ensureFirstPageLoaded(),this._debouncerCheckSize=t.Debouncer.debounce(this._debouncerCheckSize,i.timeOut.after(2e3),this._checkSize.bind(this)),this._scrollHandler()}_ensureFirstPageLoaded(){this._hasData||this._loadPage(0,this._cache,()=>{const e=this._hasData;this._hasData=!0,e||this.notifyResize()})}_itemsEqual(e,t){return this.getItemId(e)===this.getItemId(t)}_getItemIndexInArray(e,t){let i=-1;return t.forEach((t,s)=>{this._itemsEqual(t,e)&&(i=s)}),i}}}));