/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{clone as e}from"../../../../../core/lang.js";import{floatEqualAbsolute as t}from"../../../../../core/mathUtils.js";import{sqlAnd as r}from"../../../../../core/sql.js";import{isHostedAgolService as a}from"../../../../../layers/support/arcgisLayerUrl.js";import i from"../../../../../layers/support/FeatureFilter.js";import{createFeatureIdInfo as s}from"./featureServiceUtils.js";import{addFloorFilter as l,hasFloorFilter as o}from"./floorFilterUtils.js";import{getServiceGeometryType as n}from"./geometryUtils.js";import{getLayerDeconflictionEnabled as u}from"./labelingUtils.js";import{ExceedsLimitCache as c}from"../schema/ExceedsLimitCache.js";import{createSnapshotInfo as p,createFeatureSourceSchema as y}from"../schema/SourceSchema.js";import{createSubtypeProcessorSchema as m}from"../schema/processor/SubtypeProcessorSchema.js";class d{constructor(e){this.layer=e,this._cache=new c}getLabelingDeconflictionInfo(e){const t=this.layer;return[{vvEvaluators:{},deconflictionEnabled:t.sublayers.some(e=>u(e)),labelingInfo:t.sublayers.toArray().filter(e=>!!e.labelingInfo).flatMap(e=>e.labelingInfo)}]}async createSourceSchema(e,t){const{definitionExpression:r,customParameters:a,gdbVersion:i,historicMoment:s,timeExtent:l,apiKey:o,displayFilterInfo:n}=this.layer,u=this._createServiceInfo(e),c=null!=this.layer.editingInfo?.lastEditDate,m=this.layer.refreshInterval>0,d=!c&&m,f=p(u.isSourceHosted,d,this.layer.capabilities,u.metadata.geometryType,e.extent,this.layer.fullExtent);return y(u,f,{definitionExpression:r,queryScaleRanges:this.layer.sublayers.items.map(e=>({subtypeCode:e.subtypeCode,minScale:e.minScale,maxScale:e.maxScale})),displayFilterEnabled:e.displayFilterEnabled,displayFilterInfo:n,customParameters:a,gdbVersion:i,historicMoment:s,timeExtent:l,apiKey:o,sourceRefreshVersion:t,availableFields:e.availableFields,cache:this._cache})}createProcessorSchema(e,t,r){const a={labelingInfoSource:this.layer.id,subtypeField:this.layer.subtypeField,sublayers:Array.from(this.layer.sublayers,(e,t)=>({featureReduction:null,geometryType:this.layer.geometryType,labelingInfoSource:this.layer.id+`-${t}`,labelingInfo:e.labelingInfo,labelsVisible:e.labelsVisible,renderer:e.renderer,subtypeCode:e.subtypeCode,orderBy:null}))};return m(e,t,a,r)}addFilters(e,t){e=l(this.layer,e,t);const a=this.layer.sublayers.filter(e=>!f(e,t)).map(e=>e.subtypeCode);if(!a.length)return e;e??=new i;const s=`NOT ${this.layer.subtypeField} IN (${a.join(",")})`;return e.where=r(e.where,s),e}get hasRequiredSupport(){return!0}get timeOptions(){return this.layer}getUpdateHashProperties(e){return[()=>this.layer.apiKey,()=>this.layer.customParameters,()=>this.layer.definitionExpression,()=>o(this.layer,e)?e.floors:null,()=>this.layer.outFields,()=>this.layer.gdbVersion,()=>this.layer.historicMoment,()=>this.layer.sublayers.map(({renderer:e,labelsVisible:t,labelingInfo:r,visible:a,minScale:i,maxScale:s})=>({renderer:e,labelsVisible:t,labelingInfo:r,visible:a,minScale:i,maxScale:s}))]}setGraphicOrigin(e){const t=this.layer.findSublayerForFeature(e);e.layer=e.sourceLayer=t,e.origin=t?.graphicOrigin}_createServiceInfo(t){const{capabilities:r,datesInUnknownTimezone:i,dateFieldsTimeZone:l,editingInfo:o,globalIdField:u,objectIdField:c,subtypeField:p}=this.layer,y=this.layer.fieldsIndex.toJSON(),m=n(this.layer),d=this.layer.timeInfo?.toJSON(),f=this.layer.spatialReference.toJSON(),h=e(this.layer.parsedUrl),b=c,g=a(h.path),S=t.spatialReference.toJSON();return{type:"feature-service",source:h,isSourceHosted:g,orderByFields:b,outSpatialReference:S,metadata:{timeReferenceUnknownClient:i,dateFieldsTimeZone:l,subtypeField:p,globalIdField:u,fieldsIndex:y,geometryType:m,featureIdInfo:s(this.layer),timeInfo:d,spatialReference:f,outSpatialReference:S,subtypes:this.layer.subtypes?.map(e=>e.toJSON()),typeIdField:null,types:null},queryMetadata:{maxRecordCount:r.query.maxRecordCount,supportsCompactGeometry:r.query.supportsCompactGeometry,supportsDefaultSpatialReference:r.query.supportsDefaultSpatialReference,supportsFormatPBF:r.query.supportsFormatPBF,supportsMaxRecordCountFactor:r.query.supportsMaxRecordCountFactor,supportsQuantization:r.query.supportsQuantization,supportsCentroidOnDegeneratedQuantizedGeometry:r.query.supportsCentroidOnDegeneratedQuantizedGeometry,supportsDegeneratedQuantizedGeometry:r.query.supportsDegeneratedQuantizedGeometry,lastEditDate:o?.lastEditDate?.getTime()}}}}function f(e,r){return e.visible&&(0===e.minScale||t(r.scale,e.minScale)||r.scale<e.minScale)&&(0===e.maxScale||t(r.scale,e.maxScale)||r.scale>e.maxScale)}export{d as SubtypeGroupLayerAdapter};
