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