/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{__decorate as e}from"tslib";import{throwIfAborted as r}from"../../../../../../core/promiseUtils.js";import{subclass as s}from"../../../../../../core/accessorSupport/decorators.js";import{LodRenderer as n}from"../../../../webgl-engine/lib/lodRendering/LodRenderer.js";import{LodComponentRenderGeometry as t,LodComponentResources as o,LodLevelResources as i,LodResources as a}from"../../../../webgl-engine/lib/lodRendering/LodResources.js";let d=class{constructor(e){this._instanceGroupToIndices=new Map,this._instanceIndexToFeatureId=new Map,this._disposeResourceHandles=new Array,this._lodRendererResources=null,this._numFeatures=0,this.layerViewUid=e.layerViewUid,this.view=e.view,this.sharedResources=this.view.sharedSymbolResources,this.scheduler=this.view.resourceController.scheduler}get numFeatures(){return this._numFeatures}get usedMemory(){const e=this._lodRendererResources?.lodRenderer,r=e?.symbol;return(r?.computeUsedMemory()??0)+16*this._instanceIndexToFeatureId.size}destroy(){this._disposeResourceHandles.forEach(e=>e())}async doLoad(e,s,n){const t=l(e=>s(e),e),o=this.view.stage,i=t.getMaterials(),a=t.getTextures();o.addTextures(a),this._addDisposeResource(()=>{a.forEach(e=>e.unload()),o.removeTextures(a)}),await Promise.all(a.map(e=>this.view.stage.schedule(()=>e.load(o.renderView.renderingContext),n))),r(n);const d=await this._createLodRenderer(t,n);this._lodRendererResources={lodRenderer:d,materials:i,textures:a}}addInstances(e,r){const s=this._lodRendererResources;if(null==s)return;const n=s.lodRenderer;if(null==n)return;const{featureIds:t,localTransforms:o,globalTransforms:i,visibility:a}=r,d=new Array,l=n.instanceData,c=t.length,u=this._instanceIndexToFeatureId;for(let h=0;h<c;++h){const e=t[h],r=l.addInstance(),s=l.view,n=16*h;s.localTransform.copyFromTypedBuffer(r,o,n),s.globalTransform.copyFromTypedBuffer(r,i,n),l.updateModelTransform(r),l.setVisible(r,Boolean(a[h])),d.push(r),u.set(r,e)}this._instanceGroupToIndices.set(e,d),this._numFeatures+=c}removeInstances(e){const r=this._instanceGroupToIndices.get(e);if(null==r)return;const s=this._lodRendererResources;if(null==s)return;const n=s.lodRenderer.instanceData,t=this._instanceIndexToFeatureId;for(const o of r)n.removeInstance(o),t.delete(o);this._numFeatures-=r.length,this._instanceGroupToIndices.delete(e)}updateVisibility(e,r){const s=this._instanceGroupToIndices.get(e);if(null==s)return;const n=this._lodRendererResources;if(null==n)return;const t=s.length;if(t!==r.length)throw new Error("Unexpected mismatch instance count and visibility flag buffer length.");const o=n.lodRenderer.instanceData;for(let i=0;i<t;++i)o.setVisible(s[i],Boolean(r[i]))}updateGlobalTransforms(e,r){const s=this._instanceGroupToIndices.get(e);if(null==s)return;const n=this._lodRendererResources;if(null==n)return;const t=s.length;if(16*t!==r.length)throw new Error("Unexpected mismatch instance count and visibility flag buffer length.");const o=n.lodRenderer.instanceData,i=o.view;for(let a=0;a<t;++a){const e=s[a],n=16*a;i.globalTransform.copyFromTypedBuffer(e,r,n),o.updateModelTransform(e)}}_addDisposeResource(e){this._disposeResourceHandles.push(e)}async _createLodRenderer(e,r){const s=this.view.stage,t={layerViewUid:this.layerViewUid,graphicUid:e=>this._instanceIndexToFeatureId.get(e)??-1,notifyGraphicGeometryChanged:e=>1,notifyGraphicVisibilityChanged:e=>1},o=new n({symbol:e,metadata:t,shaderTransformation:null},this.scheduler);return o.slicePlaneEnabled=!1,this._addDisposeResource(()=>{s.removeRenderPlugin(o),o.destroy()}),await s.addRenderPlugin(o,r),o}};function l(e,r){const s=r.levels.map(r=>{const s=r.components.map(r=>{const s=e(r.materialId);if(!c(s))throw new Error("LodRenderer only supports DefaultMaterial");const n=new t(s,r.renderGeometryBuffer.data,r.renderGeometryBuffer.elementCount,r.boundingInfo);return new o(n)});return new i(s,r.minScreenSpaceRadius)});return new a(s)}function c(e){return null!=e&&"materialType"in e&&"default"===e.materialType}d=e([s("esri.views.3d.layers.graphics.pipeline.rendering.LodRenderer")],d);export{d as LodRenderer,c as isDefaultMaterial};
