/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import"./EXBEKVP5.js";var o=`(function(){"use strict";const h=(a,r)=>{if(a.length!==r.length)throw new Error("Vectors must be the same length");let c=0,t=0,e=0;for(let n=0;n<a.length;++n){const s=a[n],l=r[n];c+=s*l,t+=s*s,e+=l*l}const i=Math.sqrt(t*e);if(i===0)return 0;const m=c/i;return Math.max(-1,Math.min(1,m))},f=(a,r,c=.6)=>{const t=[],e=[...a];for(;t.length<r&&e.length>0;){let i=-1,m=-1/0;for(let n=0;n<e.length;n++){const s=e[n].score,l=t.length===0?0:Math.max(...t.map(o=>h(e[n].vector,o.vector))),d=c*s-(1-c)*l;d>m&&(m=d,i=n)}t.push(e[i]),e.splice(i,1)}return t.map(({name:i,score:m})=>({name:i,score:m}))};let p=[];const u=new Map,v=12,y=10,M=a=>{p=a.layers.map(r=>({id:r.id,vector:r.vector}))},E=a=>{u.clear();for(const r of a.layers){const c=r.fields.map(t=>({name:t.name,vector:t.vector}));u.set(r.id,c)}};globalThis.addEventListener("message",a=>{(async()=>{const{type:r,webmapEmbeddings:c,layerIdForFieldsSearch:t=[],requestId:e,precomputedEmbedding:i,minScore:m=0,topResults:n=v}=a.data;switch(r){case"generateEmbeddings":{c&&(M(c),E(c)),globalThis.postMessage("completed");break}case"layerSearch":{if(p.length>0)try{const s=p.map(o=>({id:o.id,vector:o.vector,score:h(i,o.vector)})),d=f(s.map(o=>({name:o.id,vector:o.vector,score:o.score})),y).map(({name:o,score:b})=>({id:o,score:b}));globalThis.postMessage({requestId:e,results:d})}catch(s){globalThis.postMessage({requestId:e,results:[],error:s instanceof Error?s.message:"Calculation error"})}else globalThis.postMessage({requestId:e,results:[],error:"Layer embeddings not initialized"});break}case"fieldSearch":{if(t.length>0)try{const s=t.map(l=>{const d=u.get(l);if(!d)return{layerId:l,results:[]};const o=d.map(g=>({name:g.name,vector:g.vector,score:h(i,g.vector)})).filter(({score:g})=>g>=m),b=f(o,n);return{layerId:l,results:b}});globalThis.postMessage({requestId:e,results:s})}catch(s){globalThis.postMessage({requestId:e,results:[],error:s instanceof Error?s.message:"Field search calculation error"})}break}default:console.warn("Unknown message type:",r)}})()})})();
`,r=typeof self<"u"&&self.Blob&&new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);",o],{type:"text/javascript;charset=utf-8"});function a(t){let e;try{if(e=r&&(self.URL||self.webkitURL).createObjectURL(r),!e)throw"";let s=new Worker(e,{name:t?.name});return s.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(e)}),s}catch{return new Worker("data:text/javascript;charset=utf-8,"+encodeURIComponent(o),{name:t?.name})}}export{a as default};
