/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{GeometryCursor as e}from"../../../../../../../geometry/GeometryCursor.js";import{loadGeometryOperatorsForEffect as t,loadGeometryOperatorsForPlacement as a}from"../../../../../../../symbols/cim/CIMEffects.js";import{getEffectsInflateSize as r}from"../../../../../../../symbols/cim/utils.js";import{EffectHelper as s}from"../../../../../../../symbols/cim/effects/EffectHelper.js";import{MeshWriterVertexPack as i}from"./MeshWriterVertexPack.js";const c=96/72;class n{constructor(e,t,a,r){this._instanceId=e,this._evaluator=t,this._enabledOptionalAttributes=a,this._viewParams=r,this._evaluator.evaluator=e=>this.vertexSpec.createComputedParams(e)}get _vertexPack(){if(!this._cachedVertexPack){const e=i.fromVertexSpec(this.vertexSpec,this._enabledOptionalAttributes);this._evaluator.hasDynamicProperties||e.pack(this._evaluator.evaluatedMeshParams,this._viewParams),this._cachedVertexPack=e}return this._cachedVertexPack}get evaluatedMeshParams(){return this._evaluator.evaluatedMeshParams}get hasEffects(){return!!this.evaluatedMeshParams.effects}get effectInfos(){return this._evaluator.inputMeshParams.effects?.effectInfos}get placementInfo(){return this._evaluator.inputMeshParams.placement}get instanceId(){return this._instanceId}get attributeLayout(){return this._vertexPack.attributeLayout}get _preventEffectClipping(){return!1}setReferences(e){this._references=e}getBoundsInfo(){return null}getTileInfo(){return this._viewParams.tileInfo}async loadDependencies(){for(const{effect:e}of this.effectInfos||[])await t(e);this.placementInfo&&await a(this.placementInfo.placement)}enqueueRequest(e,t,a){this._evaluator.hasDynamicProperties&&this._evaluator.enqueueRequest(e,t,a)}write(e,t,a,r,s,i,c,n){this.ensurePacked(t,a,r);const o=e.id||"",f=this.evaluatedMeshParams.effects,u=this._createEffectExecutionContext(a,r,o,c,n),h={target:a,options:r,level:s,viewExtent:i};if(!f||0===f.length)return void this._write(e,h);const l=this.getEffectCursor(a,f,u);if(!l)return;let m;for(;m=l.next();)m.invertY(),this._write(e,{...h,effectGeometry:m,scaleFactor:c})}ensurePacked(e,t,a){if(!this._evaluator.hasDynamicProperties)return;const r=this._evaluator.evaluateMeshParams(e,t,a);this._vertexPack.pack(r,this._viewParams)}hasArcadeDependency(e){return this._evaluator.hasArcadeDependency(e)}_writeVertex(e,t,a,r,s){const i=this.evaluatedMeshParams;this._vertexPack.writeVertex(e,t,a,r,i,s)}_createEffectExecutionContext(e,t,a,s,i){return{geomUnitsPerPoint:c,maxInflateSize:r(this.evaluatedMeshParams.effects),scale:t.$view.scale,spatialReference:e.metadata.outSpatialReference??e.metadata.spatialReference,preventClipping:this._preventEffectClipping,tileKey:a,scaleFactor:s,matcher:i,target:e}}getEffectCursor(t,a,r){null==r.scaleFactor&&(r={...r,scaleFactor:1});const i=t.readGeometryForDisplay()?.clone();if(!i)return;const c=e.fromOptimizedCIM(i,t.geometryType);return c.invertY(),s.executeEffects(a,c,r)}}export{n as MeshWriter};
