/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../../Graphic.js";import{getOrCreateMapValue as t}from"../../../../core/MapUtils.js";import s from"../../../../core/pbf.js";import{PooledRBush as r}from"../../../../core/libs/rbush/PooledRBush.js";import i from"../../../../graphic/VectorTileGraphicOrigin.js";import{tileSizeInPixels as o,tileSizeInTileUnits as l}from"./constants.js";import n from"./Feature.js";import{getTileMargins as a}from"./GeometryUtils.js";import y from"./SourceLayerData.js";import{IndexItem as c}from"./style/StyleLayer.js";const u=(e,t)=>{const s=e.vtlSymbol.sourceTile,r=t.vtlSymbol.sourceTile;return s.level!==r.level?s.level-r.level:s.row!==r.row?s.row-r.row:s.col!==r.col?s.col-r.col:e.styleLayerUID-t.styleLayerUID};class h{constructor(e,t,s,r,i){this.tileKey=e,this._tileLayerData=t,this._styleRepository=s,this._tileHandler=r,this._parentLayer=i,this._index=null,this._tileKeyToPBF=new Map}static create(e,t,s,r,i){return new h(e,t,s,r,i)}clear(){this._index?.clear(),this._tileKeyToPBF.clear()}async queryAttributes(e,t,s,i,o,l=1){if(0===this._tileLayerData.size||!this._styleRepository||!this._tileHandler)return[];null===this._index&&(this._index=new r(100,m),await this._indexLayers());const n=[];return this._queryIndex(n,e,t,s,this.tileKey.level,i,l),o&&o?.length>0&&await this._getSymbolsAttributes(n,o),n}async _indexLayers(){const e=this.tileKey,t=this._styleRepository.layers,s=await this._getTilePayload(e);for(const[r,i]of this._tileLayerData){const o=t[r],l=s.find(e=>e.sourceName===o.source);if(!l)continue;const{protobuff:n,key:a}=l;if(3!==i.type){const t=1<<e.level-a.level,s=e.row-a.row*t,r=e.col-a.col*t;this._indexLayer(o,n,e.level,t,s,r)}}}_indexLayer(e,t,r,i,u,h){const m=e.sourceLayer,f=e.getFeatureFilter(),d=r,_=r+1,p=a(d),g=new s(new Uint8Array(t),new DataView(t));for(;g.next();)switch(g.tag()){case 3:{const t=g.getMessage(),s=new y(t);if(t.release(),s.name!==m)continue;const a=s.getData(),b=s.extent/i,w=b*h-p,x=b*u-p,L=w+b+2*p,v=x+b+2*p,I=b/o,T=l/b,D=b*h,S=b*u;for(;a.nextTag(2);){const t=a.getMessage(),i=new n(t,s);if(t.release(),f&&!f.filter(i,r))continue;const o=i.values||{},l=o._minzoom,y=o._maxzoom;if(l&&l>=10*_||y&&y<=10*d)continue;const u=e.getFeatureInflatedBounds(i,d,s.extent,I);null==u||u[0]>L||u[1]>v||u[2]<w||u[3]<x||(u[0]=(u[0]-D)*T,u[1]=(u[1]-S)*T,u[2]=(u[2]-D)*T,u[3]=(u[3]-S)*T,this._index.insert(new c(e,i,u,T,D,S)))}break}default:g.skip()}g.release()}async _getSymbolsAttributes(e,t){if(!t||0===t.length)return e;const s=[];if(t.sort(u),t.length>0){let e=0,{styleLayerUID:r}=t[0];for(let o=1;o<t.length;o++){const{styleLayerUID:i}=t[o];i!==r&&(s.push({from:e,to:o,styleLayerUID:r,sourceTileKey:t[o-1].vtlSymbol.sourceTile}),e=o,r=i)}const i=t.length-1;s.push({from:e,to:t.length,styleLayerUID:r,sourceTileKey:t[i].vtlSymbol.sourceTile})}const r=this._styleRepository.layers;for(const i of s){const s=await this._getTilePayload(i.sourceTileKey),o=r[i.styleLayerUID],l=!!o&&s.find(e=>e.sourceName===o.source);l&&this._addSymbolsAttributes(e,t.slice(i.from,i.to).map(e=>e.vtlSymbol.featureIndex),i.styleLayerUID,l)}return e}_addSymbolsAttributes(t,s,r,o){const l=this._styleRepository.layers,n=o.key,a=this.tileKey,y=1<<a.level-n.level,c=a.row-n.row*y,u=a.col-n.col*y;this._getSymbolAttributes(o.protobuff,s,r,y,c,u).forEach(s=>{const{attributes:o,tilePoint:n}=s;t.push({layerId:l[r].id,layerIndex:r,graphic:new e({attributes:o,origin:new i(this._parentLayer,l[r].id,r)}),tilePoint:n})})}_getSymbolAttributes(e,t,r,i,o,a){const c=[],u=this._styleRepository.layers;let h=0;t.sort((e,t)=>e-t);const m=new s(new Uint8Array(e),new DataView(e));for(;m.next();)switch(m.tag()){case 3:{const e=m.getMessage(),s=new y(e);if(e.release(),s.name!==u[r].sourceLayer)continue;const f=s.getData(),d=s.extent/i,_=l/d,p=d*a,g=d*o;let b=0;for(;f.nextTag(2);){const e=f.getMessage();if(b++===t[h]){const t=new n(e,s),r=t.values,i=t.getGeometry(),o=null!=i?[_*(i[0][0].x-p),_*(i[0][0].y-g)]:null;c.push({attributes:r,tilePoint:o}),h++}if(e.release(),h===t.length)return c}break}default:m.skip()}return m.release(),c}_queryIndex(t,s,r,o,l,n,a){return this._index?.search({minX:s-o,minY:r-o,maxX:s+o,maxY:r+o},y=>{const{layer:c,feature:u}=y;this._styleRepository.getStyleLayerVisibility(c.id)&&c.isIntersectingFeature(s,r,o,u,l,n,a,y)&&t.push({layerId:c.id,layerIndex:c.uid,tilePoint:null,graphic:new e({attributes:u.values,origin:new i(this._parentLayer,y.layer.id,y.layer.uid)})})}),t}async _getTilePayload(e){return t(this._tileKeyToPBF,e.id,()=>this._tileHandler.fetchTilePBFs(e)).then(e=>e)}}const m=e=>({minX:e.bounds[0],minY:e.bounds[1],maxX:e.bounds[2],maxY:e.bounds[3]});export{h as default};
