import EventType from"./events/EventType.js";import PriorityQueue,{DROP}from"./structs/PriorityQueue.js";import TileState from"./TileState.js";class TileQueue extends PriorityQueue{constructor(t,e){super(function(e){return t.apply(null,e)},function(e){return e[0].getKey()}),this.boundHandleTileChange_=this.handleTileChange.bind(this),this.tileChangeCallback_=e,this.tilesLoading_=0,this.tilesLoadingKeys_={}}enqueue(e){var t=super.enqueue(e);if(t){const i=e[0];i.addEventListener(EventType.CHANGE,this.boundHandleTileChange_)}return t}getTilesLoading(){return this.tilesLoading_}handleTileChange(e){const t=e.target;var e=t.getState();(t.hifi&&e===TileState.LOADED||e===TileState.ERROR||e===TileState.EMPTY)&&(t.removeEventListener(EventType.CHANGE,this.boundHandleTileChange_),(e=t.getKey())in this.tilesLoadingKeys_&&(delete this.tilesLoadingKeys_[e],--this.tilesLoading_),this.tileChangeCallback_())}loadMoreTiles(e,t){let i=0,n,s;for(;this.tilesLoading_<e&&i<t&&0<this.getCount();)s=(n=this.dequeue()[0]).getKey(),n.getState()!==TileState.IDLE||s in this.tilesLoadingKeys_||(this.tilesLoadingKeys_[s]=!0,++this.tilesLoading_,++i,n.load())}}export default TileQueue;function getTilePriority(e,t,i,n,s){if(!(e&&i in e.wantedTiles))return DROP;if(!e.wantedTiles[i][t.getKey()])return DROP;i=e.viewState.center,t=n[0]-i[0],e=n[1]-i[1];return 65536*Math.log(s)+Math.sqrt(t*t+e*e)/s}export{getTilePriority};