"use strict";var TinyQueue=require("tinyqueue");const calcLimitNodeSearch=(containerWidth,targetRowHeight)=>containerWidth>500?Math.round(containerWidth/targetRowHeight/1.5)+8:5;const calcCommonHeight=param=>{let{aspectRatioList,containerWidth,horizontalBoxSpacing}=param;const rowWidth=containerWidth-aspectRatioList.length*horizontalBoxSpacing;const totalAspectRatio=aspectRatioList.reduce((acc,ratio)=>acc+ratio,0);return rowWidth/totalAspectRatio};const calcCost=param=>{let{aspectRatioList,start,end,containerWidth,targetRowHeight,horizontalBoxSpacing}=param;const row=aspectRatioList.slice(start,end);const commonHeight=calcCommonHeight({aspectRatioList:row,containerWidth,horizontalBoxSpacing});return Math.pow(Math.abs(commonHeight-targetRowHeight),2)};const buildPrecedentsMap=(graphBuilder,startNode,endNode)=>{const precedentsMap=new Map;const visited=new Set;const storedShortestPaths={};storedShortestPaths[startNode]=0;const pQueue=new TinyQueue(undefined,(a,b)=>a.weight-b.weight);pQueue.push({id:startNode,weight:0});while(pQueue.length!==0){const shortestNode=pQueue.pop();if(!shortestNode)continue;const shortestNodeId=shortestNode.id;if(visited.has(shortestNodeId))continue;const neighboringNodes=graphBuilder(shortestNodeId)||new Map;visited.add(shortestNodeId);for(const[neighbor,neighborValue]of neighboringNodes){const newTotalWeight=shortestNode.weight+neighborValue;if(typeof storedShortestPaths[neighbor]==="undefined"||storedShortestPaths[neighbor]>newTotalWeight){storedShortestPaths[neighbor]=newTotalWeight;pQueue.push({id:neighbor,weight:newTotalWeight});precedentsMap.set(neighbor,shortestNodeId)}}}if(typeof storedShortestPaths[endNode]==="undefined"){throw new Error(`There is no path from ${startNode} to ${endNode}`)}return precedentsMap};const getPathFromPrecedentsMap=(precedentsMap,endNode)=>{let n=endNode;const nodes=[n];while(n){n=precedentsMap.get(n);nodes.push(n)}return nodes.reverse()};const findShortestPath=(graph,startNode,endNode)=>{const precedentsMap=buildPrecedentsMap(graph,startNode,endNode);return getPathFromPrecedentsMap(precedentsMap,endNode)};const makeGetNeighbors=param=>{let{aspectRatioList,targetRowHeight,containerWidth,limitNodeSearch,horizontalBoxSpacing}=param;return start=>{const results=new Map;results.set(start,0);for(let i=start+1;ilimitNodeSearch)break;results.set(i,calcCost({aspectRatioList,start,end:i,containerWidth,targetRowHeight,horizontalBoxSpacing}))}return results}};const photoFlexLayout=param=>{let{items,containerWidth,targetRowHeight,boxSpacing}=param;const aspectRatioList=items.map(x=>x.width/x.height);const limitNodeSearch=calcLimitNodeSearch(containerWidth,targetRowHeight);const horizontalBoxSpacing=typeof boxSpacing==="number"?boxSpacing:boxSpacing.horizontal;const verticalBoxSpacing=typeof boxSpacing==="number"?boxSpacing:boxSpacing.vertical;const getNeighbors=makeGetNeighbors({targetRowHeight,containerWidth,aspectRatioList,limitNodeSearch,horizontalBoxSpacing});const path=findShortestPath(getNeighbors,0,aspectRatioList.length);const result=[];let containerHeight=0;for(let rowId=0;rowIdtargetRowHeight*1.3){height=targetRowHeight}let left=0;for(let aspectRatioId=path[rowId];aspectRatioId