/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import r from"../Error.js";import has from"../has.js";import{throwIfAborted as t}from"../promiseUtils.js";import o from"./Connection.js";import{register as e}from"./connectionRegistry.js";import n from"./RemoteClient.js";import i from"./WorkerOwner.js";const s=has("host-browser")?Math.min(navigator.hardwareConcurrency-1,has("workers-pool-size")??8):0;let a=has("esri-mobile")?Math.min(s,3):s;a||(a=has("safari")&&has("mac")?7:2);let c=0;const l=[];function m(){g()}function u(r,t={}){return f(r,t)}async function f(r,t){const n=new o,{registryTarget:i,...s}=t;return await n.open(r,s),i&&e(i,n),n}async function p(o,e={}){if("string"!=typeof o)throw new r("workers:undefined-module","modulePath is missing");let i=e.strategy||"distributed";if(has("host-webworker")&&!has("esri-workers")&&(i="local"),"local"===i){let r=await n.loadWorker(o);r||(r=await import(/* @vite-ignore */ /* webpackIgnore: true */o)),t(e.signal);const i=e.client||r;return f([n.connect(r,e.schedule)],{...e,client:i})}if(await g(),t(e.signal),"dedicated"===i){const r=c++%a;return f([await l[r].open(o,e)],e)}if(e.maxNumWorkers&&e.maxNumWorkers>0){const r=Math.min(e.maxNumWorkers,a);if(r<a){const t=new Array(r);for(let n=0;n<r;++n){const r=c++%a;t[n]=l[r].open(o,e)}return f(t,e)}}return f(l.map(r=>r.open(o,e)),e)}function w(){d&&(h.abort(),d=null);for(let r=0;r<l.length;r++)l[r]&&l[r].terminate();l.length=0}let h,d=null;async function g(){if(d)return d;h=new AbortController;const r=[];for(let t=0;t<a;t++){const o=i.create(t).then(r=>(l[t]=r,r));r.push(o)}return d=Promise.all(r),d}export{o as Connection,n as RemoteClient,m as initialize,p as open,u as openWithPorts,w as terminate};
