/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */
import{GridBaseLayout as t}from"./shared/GridBaseLayout.js";export const masonry=t=>Object.assign({type:MasonryLayout},t);const s="MIN";export class MasonryLayout extends t{constructor(){super(...arguments),this._RANGE_MAP_GRANULARITY=100,this._positions=new Map,this._rangeMap=new Map}_getDefaultConfig(){return Object.assign({},super._getDefaultConfig(),{getAspectRatio:()=>1})}set getAspectRatio(t){this._getAspectRatio=t}_setItems(t){t!==this._items&&this._scheduleLayoutUpdate(),super._setItems(t)}_getItemSize(t){return{[this._sizeDim]:this._metrics.itemSize1,[this._secondarySizeDim]:this._metrics.itemSize2}}_updateLayout(){super._updateLayout(),this._layOutChildren()}_getRangeMapKey(t,i){const e=this._RANGE_MAP_GRANULARITY;return i===s?Math.floor(t/e)*e:Math.ceil(t/e)*e}_layOutChildren(){const t=this._RANGE_MAP_GRANULARITY;this._positions.clear(),this._rangeMap.clear();const{rolumns:i,padding1:e,itemSize2:a,gap1:o,positions:h}=this._metrics;let n=e.start;const _=new Array(i).fill(null).map((t=>n));let r=0,c=0,p=1/0,l=-1/0;if(this.items.forEach(((i,g)=>{const M=this._getAspectRatio(i),m="horizontal"===this.direction?a*M:a/M,u=_[r],A=h[r];this._positions.set(g,{[this._positionDim]:u,[this._secondaryPositionDim]:A,[this._sizeDim]:m,[this._secondarySizeDim]:a});const y=u+m,R=this._getRangeMapKey(u,s);R<p&&(p=R);const f=this._getRangeMapKey(y,"MAX");f>l&&(l=f);for(let s=R;s<=f;s+=t){const[t,i]=this._rangeMap.get(s)??[1/0,-1/0];this._rangeMap.set(s,[Math.min(g,t),Math.max(g,i)])}c=Math.max(c,y+e.end),_[r]+=m+o,n=1/0,_.forEach(((t,s)=>{t<n&&(n=t,r=s)}))})),p!==1/0)for(let s=0;s<p;s+=t)this._rangeMap.set(s,[-1,-1]);if(l!==-1/0){const s=this._rangeMap.get(l);for(let i=l+t;i<c+t;i+=t)this._rangeMap.set(i,s)}this._scrollSize=c}_getActiveItems(){const t=this._metrics,{rolumns:i}=t;if(0===i||0===this._rangeMap.size)this._first=-1,this._last=-1,this._physicalMin=0,this._physicalMax=0;else{const t=Math.max(0,this._scrollPosition-this._overhang),i=Math.min(this._scrollSize,this._scrollPosition+this._viewDim1+this._overhang),e=this.items.length-1,a=this._getRangeMapKey(t,s),o=this._getRangeMapKey(i,"MAX");let h=e,n=0;for(let t=a;t<=o;t+=this._RANGE_MAP_GRANULARITY){const[s,i]=this._rangeMap.get(t)??[e,0];h=Math.min(h,s),n=Math.max(n,i)}this._first=h,this._last=n}}_getItemPosition(t){return this._positions.get(t)}_updateScrollSize(){}}