/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{create as t}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{transformMat4 as e,set as i}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{empty as s,expandWithVec3 as a,create as h,isPoint as r,center as l,offset as o}from"../../../../geometry/support/aaBoundingBox.js";import{evaluateElevationInfoAtPoint as g}from"./elevationAlignmentUtils.js";import{demResolutionForBoundingBox as c}from"./graphicUtils.js";import{Object3DHighlightStateID as d}from"../../webgl-engine/lib/Object3DStateID.js";class m{constructor(t,e,i,n,s){this.graphics3DSymbolLayer=t,this.instanceIndex=e,this.elevationAligner=i,this.elevationContext=n,this._highlightOrderMap=s,this.type="lod-instance",this._highlights=new Set,this.alignedSampledElevation=0,this.isElevationSource=!1,this.needsElevationUpdates=!1,this._highlightName=null}initialize(){}setVisibility(t){const{instanceData:e}=this;t!==e.getVisible(this.instanceIndex)&&e.setVisible(this.instanceIndex,t)}destroy(){null!=this.instanceIndex&&(this.instanceData.removeInstance(this.instanceIndex),this.graphics3DSymbolLayer.notifyDestroyGraphicLayer(this))}get usedMemory(){return this.graphics3DSymbolLayer.usedMemory}alignWithElevation(t,e){if(this.elevationAligner){const i=(i,n)=>g(i,t,this.elevationContext,e,n),n=this.elevationAligner(this,this.elevationContext,t.spatialReference,i,e);null!=n&&(this.alignedSampledElevation=n)}}getCenterObjectSpace(t=n()){return this.instanceData.getCombinedLocalTransform(this.instanceIndex,v),e(t,this._lodRenderer.baseBoundingSphere.center,v)}getBoundingBoxObjectSpace(t=h()){this.instanceData.getCombinedLocalTransform(this.instanceIndex,v);const n=this._lodRenderer.baseBoundingBox;s(t);for(let s=0;s<8;++s)i(u,1&s?n[3]:n[0],2&s?n[4]:n[1],4&s?n[5]:n[2]),e(u,u,v),a(t,u);return t}computeAttachmentOrigin(t){this.instanceData.getGlobalTransform(this.instanceIndex,v),t.render.origin[0]+=v[12],t.render.origin[1]+=v[13],t.render.origin[2]+=v[14],t.render.num++}async getProjectedBoundingBox(t,i,n,a,h){const g=this.getBoundingBoxObjectSpace(h),d=f,m=r(g)?1:d.length;this.instanceData.getGlobalTransform(this.instanceIndex,v);for(let s=0;s<m;s++){const t=d[s];u[0]=g[t[0]],u[1]=g[t[1]],u[2]=g[t[2]],e(u,u,v),p[3*s]=u[0],p[3*s+1]=u[1],p[3*s+2]=u[2]}if(!t(p,0,m))return null;s(g);let x=null;this.calculateRelativeScreenBounds&&(x=this.calculateRelativeScreenBounds());for(let e=0;e<3*m;e+=3){for(let t=0;t<3;t++)g[t]=Math.min(g[t],p[e+t]),g[t+3]=Math.max(g[t+3],p[e+t]);x&&n.push({location:p.slice(e,e+3),screenSpaceBoundingRect:x})}if(i&&(l(g,b),"absolute-height"!==this.elevationContext.mode)){let t;const e=c(g,i.service.spatialReference,i);try{t=await i.service.queryElevation(b[0],b[1],a,e,"ground")}catch(_){}null!=t&&o(g,0,0,-this.alignedSampledElevation+t)}return g}addObjectState(t){0===t.stateType&&this.addObjectHighlightState(t)}addObjectHighlightState(t){const e=new d(t.highlightName);this._addHighlightId(e),t.addExternal(t=>{this._removeHighlightId(t)},e)}removeObjectState(t){this._highlights.forEach(e=>t.remove(e))}updateHighlights(t){this._highlightOrderMap=t,this._updateHighlightOptions()}_calculateHighlightOptions(){let t=-1,e=null;return this._highlights.forEach(({highlightName:i})=>{const n=this._highlightOrderMap.get(i);void 0!==n&&n>t&&(t=n,e=i)}),e}_addHighlightId(t){this._highlights.add(t),this._updateHighlightOptions()}_removeHighlightId(t){this._highlights.delete(t),this._updateHighlightOptions()}_updateHighlightOptions(){const t=this._calculateHighlightOptions();t!==this._highlightName&&(this._highlightName=t,this.instanceData.setHighlight(this.instanceIndex,t))}get _lodRenderer(){return this.graphics3DSymbolLayer.lodRenderer}get instanceData(){return this._lodRenderer.instanceData}}const p=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],u=n(),b=n(),f=[[0,1,2],[3,1,2],[0,4,2],[3,4,2],[0,1,5],[3,1,5],[0,4,5],[3,4,5]],v=t();export{m as Graphics3DLodInstanceGraphicLayer};
