/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import s from"../../../core/Logger.js";import t from"../../../core/PooledArray.js";import{create as e}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{WorkerHandle as o}from"../../../core/workers/WorkerHandle.js";import{canProjectWithoutEngine as i}from"../../../geometry/projectionUtils.js";import{projectVectorToVector as n}from"../../../geometry/projection/projectVectorToVector.js";class r{constructor(s,t,e,o,i,n){this.layout=s,this.interleavedVertexData=t,this.indices=e,this.hasColors=o,this.hasModifications=i,this.positionData=n}}class h{constructor(s,t,e,o,i,n,r){this.componentOffsets=s,this.featureIds=t,this.anchorIds=e,this.anchors=o,this.transformedGeometry=i,this.globalTrafo=n,this.obb=r}}class a extends o{constructor(s){super("SceneLayerWorker","process",{process:s=>[s.geometryBuffer],project:s=>[s.positions.buffer],transformNormals:s=>[s.normals.buffer]},s,{hasInitialize:!0}),this._schemasByContext=new Map,this._modificationsByContext=new Map}_onClientConnected(s){for(const[t,e]of this._schemasByContext)s.invoke("setLegacySchema",{context:t,jsonSchema:e});for(const t of this._modificationsByContext.values())s.invoke("setModifications",t)}setModifications(s,t,e){const o={context:s,modifications:t,isGeodetic:e};return this._modificationsByContext.set(s,o),this.broadcast(o,"setModifications")}setLegacySchema(s,t){const e=JSON.stringify(t);return this._schemasByContext.set(s,e),this.broadcast({context:s,jsonSchema:e},"setLegacySchema")}destroyContext(s){return this._schemasByContext.delete(s),this._modificationsByContext.delete(s),this.broadcast(s,"destroyContext")}async destroyContextAndSelf(s){await this.destroyContext(s),this.destroy()}project(s,t){return this.invokeMethod("project",s,t)}transformNormals(s,t){return this.invokeMethod("transformNormals",s,t)}}const c=new t({deallocator:null}),p=e();function u(t,e,o){c.clear();let r=1/0,h=1/0,a=-1/0,u=-1/0,l=!1;for(const m of e){const t="clip"===m.type?2:"mask"===m.type?1:0,e=m.geometry;let f=s=>s;if(e.spatialReference){if(!i(e.spatialReference,o)){s.getLogger("esri.views.3d.layers.SceneLayerWorkerHandle").warn("im-modification-projection-failed","Can't project modification polygon into layer spatial reference, ignoring modification",{polygon:e});continue}f=s=>(n(s,e.spatialReference,p,o),p)}l=l||1===t;const g=e.rings.length,d=e.rings.some(s=>s.length<3);if(0===g||d)s.getLogger("esri.views.3d.layers.SceneLayerWorkerHandle").warn("im-modification-invalid-polygon","Ignoring invalid modification polygon (no rings or rings with less than 3 vertices).",{polygon:e});else{c.push(t),c.push(g);for(const s of e.rings){c.push(s.length);for(const t of s){const s=f(t);c.push(s[0]),c.push(s[1]),c.push(s[2]??0),r=Math.min(r,s[0]),h=Math.min(h,s[1]),a=Math.max(a,s[0]),u=Math.max(u,s[1])}}}}if(null!=t)if(l){const s=1e-4;c.push(2),c.push(2),c.push(4),c.push(r-s),c.push(h-s),c.push(0),c.push(a+s),c.push(h-s),c.push(0),c.push(a+s),c.push(u+s),c.push(0),c.push(r-s),c.push(u+s),c.push(0),c.push(4),c.push(t[0]),c.push(t[1]),c.push(0),c.push(t[2]),c.push(t[1]),c.push(0),c.push(t[2]),c.push(t[3]),c.push(0),c.push(t[0]),c.push(t[3]),c.push(0)}else c.push(1),c.push(1),c.push(4),c.push(t[0]),c.push(t[1]),c.push(0),c.push(t[2]),c.push(t[1]),c.push(0),c.push(t[2]),c.push(t[3]),c.push(0),c.push(t[0]),c.push(t[3]),c.push(0);c.push(3);const f=new Float64Array(c.length);for(let s=0;s<c.length;++s)f[s]=c.at(s);return f}function l(s,t){let e=0;for(const o of t){const t=o.geometry,i=t.rings.length,n=t.rings.some(s=>s.length<3);if(0!==i&&!n){e+=1,e+=1;for(let o=0;o<t.rings.length;++o){const i=t.rings[o];e+=1;for(let n=0;n<i.length;++n){const i=[s[e+0],s[e+1],s[e+2]];e+=3,t.setPoint(o,n,i)}}}}}export{a as SceneLayerWorkerHandle,h as TransformedData,r as TransformedGeometry,l as fromWasmModification,u as toWasmModification};
