/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{throwIfAborted as e}from"../../../../../../core/promiseUtils.js";import{toExtent as t}from"../../../../../../geometry/support/aaBoundingRect.js";import{executeQueryPBFBuffer as r}from"../../../../../../rest/query/operations/query.js";import{Tile as o}from"../Tile.js";import{PBFPointFeatureSetView as s}from"../featureSet/PBFPointFeatureSetView.js";const i=8e3,a=4,n=4;class c{constructor(e,t,r,o,s){this.spatialReference=e,this.url=r,this.objectIdField=o,this.capabilities=s;const{supportsMaxRecordCountFactor:n,maxRecordCount:c}=this.capabilities.query,u=n?a:1,p=(c??i)*u;this._pageSize=Math.min(i,p);const l=t.clone();l.cacheHint=!0,l.resultType="tile",l.outSpatialReference=e,l.returnGeometry=!0,l.returnZ=!0,l.maxRecordCountFactor=u,l.num=this._pageSize,l.outFields=[o],this._baseQuery=l}async fetch(r,s){const{spatialReference:i,_pageSize:a}=this,c=t(r.extent,i),u=this._baseQuery.clone();u.geometry=c;const p=new Array;let l=0,h=!1,m=1;for(;!h;){const t=[];for(let e=0;e<m;++e)t.push(this._fetchPage(u,l++,s));const r=await Promise.all(t);e(s);for(const e of r){const t=0!==e.featureCount;h||=!e.exceededTransferLimit||!t,t&&p.push(e)}m=Math.min(m+1,n)}return new o(r,p,a)}async _fetchPage(t,o,i){const a=t.clone();a.start=o*this._pageSize;const n=await r(this.url,a,{signal:i});return e(i),new s(n)}}export{c as Tile3DFetcher};
