/**
 * @license
 * Copyright 2021 Google LLC
 * SPDX-License-Identifier: BSD-3-Clause
 */
import{SizeCache as t}from"./shared/SizeCache.js";import{SizeGapPaddingBaseLayout as i}from"./shared/SizeGapPaddingBaseLayout.js";export const layout1dFlex=t=>Object.assign({type:FlexWrapLayout},t);export class FlexWrapLayout extends i{constructor(){super(...arguments),this._itemSizes=[],this._chunkLength=null,this._chunks=[],this._chunkSizeCache=new t,this._rolumnSizeCache=new t,this._rolumnLengthCache=new t({roundAverageSize:!1}),this._aspectRatios={},this._numberOfAspectRatiosMeasured=0,this.listenForChildLoadEvents=!0,this.measureChildren=function(t,i){const{naturalWidth:s,naturalHeight:e}=t;if(void 0!==s&&null!=e)return{width:s,height:e};const{o_width:h,o_height:n}=i;return void 0!==h&&void 0!==n?{width:h,height:n}:{width:-1,height:-1}}}set gap(t){this._setGap(t)}updateItemSizes(t){let i;Object.keys(t).forEach((s=>{const e=Number(s),h=this._getChunk(e),n=t[e],o=this._itemSizes[e];n.width&&n.height&&(o&&o.width===n.width&&o.height===n.height||(h._dirty=!0,i=!0,this._itemSizes[e]=t[e],this._recordAspectRatio(t[e])))})),i&&this._scheduleLayoutUpdate()}_newChunk(){return{_rolumns:[],_itemPositions:[],_size:0,_dirty:!1}}_getChunk(t){return this._chunks[Math.floor(Number(t)/this._chunkLength)]||this._newChunk()}_recordAspectRatio(t){if(t.width&&t.height){const i=Math.round(t.width/t.height*10)/10;this._aspectRatios[i]?this._aspectRatios[i]++:this._aspectRatios[i]=1,this._numberOfAspectRatiosMeasured++}}_getRandomAspectRatio(){if(0===this._numberOfAspectRatiosMeasured)return{width:1,height:1};const t=Math.random()*this._numberOfAspectRatiosMeasured,i=Object.keys(this._aspectRatios);let s=-1,e=0;for(;e<t&&s<i.length;)e+=this._aspectRatios[i[++s]];return{width:Number(i[s]),height:1}}_getActiveItems(){const t=this._getChunk(0);if(0===t._rolumns.length)return;const i=Math.max(0,Math.min(this._scrollPosition,this._scrollSize-this._viewDim1)),s=Math.max(0,i-this._overhang),e=Math.min(this._scrollSize,i+this._viewDim1+this._overhang),h=(s+e)/2;let n,o=Math.round(h/this._scrollSize*t._rolumns.length);for(;t._rolumns[o]._startPos<s;)o++;for(;t._rolumns[o]._startPos>s;)o--;for(this._first=t._rolumns[o]._startIdx,this._physicalMin=t._rolumns[o]._startPos;(n=t._rolumns[o]._startPos+t._rolumns[o]._size+2*this._gap)<e;)o++;this._last=t._rolumns[o]._endIdx,this._physicalMax=n}_getItemPosition(t){return this._getChunk(0)._itemPositions[t]}_getItemSize(t){const i=this._getChunk(0),{width:s,height:e}=i._itemPositions[t];return{width:s,height:e}}_getNaturalItemDims(t){let i=this._itemSizes[t];return void 0!==i&&-1!==i.width&&-1!==i.height||(i=this._getRandomAspectRatio()),i}_layOutChunk(t,i){const s=this._newChunk(),e=this._gap;let h=e,n=0,o=0,_=1/0;const finishRolumn=i=>{const n={_startIdx:t,_endIdx:i,_startPos:h-e,_size:0};s._rolumns.push(n);let o=this._gap;for(let n=t;n<=i;n++){const t=s._itemPositions[n];t.width=t.width*_,t.height=t.height*_,t.left="left"===this._positionDim?h:o,t.top="top"===this._positionDim?h:o,o+=t[this._secondarySizeDim]+e}n._size=s._itemPositions[i][this._sizeDim]};for(;n<=i;){const r=this._getNaturalItemDims(n),a=this._viewDim2-e*(n-t+2),u=r[this._sizeDim],c=r[this._secondarySizeDim],l=this._idealSize/u,m=l*u,d=l*c;s._itemPositions[n]={left:0,top:0,width:"width"===this._sizeDim?m:d,height:"height"===this._sizeDim?m:d};const g=a/(o+d);Math.abs(1-g)>Math.abs(1-_)?(finishRolumn(n-1),t=n,h+=this._idealSize*_+e,_=(this._viewDim2-2*e)/d,o=d):(o+=d,_=g),n===i&&finishRolumn(n),n++}const r=s._rolumns[s._rolumns.length-1];return s._size=r._startPos+r._size,s}_updateLayout(){if(0===this._viewDim2)return;this._chunkLength=Math.ceil(this._viewDim1*this._viewDim2*2/(this._idealSize*this._idealSize)),console.log("chunkLength",this._chunkLength);const t=this._layOutChunk(0,this._chunkLength-1);this._chunks[0]=t,this._chunkSizeCache.set(0,t._size),t._rolumns.forEach(((t,i)=>{const s=`0:${i}`;this._rolumnSizeCache.set(s,t._size),this._rolumnLengthCache.set(s,t._endIdx-t._startIdx+1)}))}_updateScrollSize(){const t=this._chunks[0];this._scrollSize=t&&0!==t._rolumns.length?t._size+2*this._gap:1}}