(function(t,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(t=typeof globalThis<"u"?globalThis:t||self,n(t.index={}))})(this,function(t){"use strict";var c=Object.defineProperty;var d=(t,n,a)=>n in t?c(t,n,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[n]=a;var k=(t,n,a)=>(d(t,typeof n!="symbol"?n+"":n,a),a);class n{constructor(){k(this,"_taskQueue",[]);k(this,"_resultHandlers",new Map)}executeTask(s){const e={id:crypto.randomUUID(),data:s},r=new Promise((i,o)=>{this._taskQueue.push(e),this._resultHandlers.set(e.id,l=>"error"in l?o(l.error):i(l.data))});return this.scheduleTask(),{id:e.id,promise:r}}cancelTask(s,e){const r=this._taskQueue.findIndex(i=>i.id===s);r!==-1&&this._taskQueue.splice(r,1),this._handleTaskResult({id:s,error:e??new Error("Task cancelled by user")}),this._resultHandlers.delete(s)}scheduleTask(){const s=this._taskQueue.shift();s&&(this._runTask(s)||this._taskQueue.unshift(s))}_handleTaskResult(s){const e=this._resultHandlers.get(s.id);e&&(e(s),this._resultHandlers.delete(s.id)),this.scheduleTask()}}class a extends n{constructor(e){super();k(this,"_workers",[]);k(this,"_workerTask",new Map);if(typeof Worker>"u"){console.warn("Worker() is not supported in this environment. Tasks will not be executed.");return}this._workers=new Array((e==null?void 0:e.maxWorkers)??(navigator==null?void 0:navigator.hardwareConcurrency)??4).fill(null).map(()=>this._newBindedWorker())}_getTransferableData(e){return typeof e=="object"&&e!==null?Object.values(e).flatMap(r=>this._getTransferableData(r)):e instanceof ArrayBuffer?[e]:e instanceof MessagePort?[e]:e instanceof ImageBitmap?[e]:e instanceof OffscreenCanvas?[e]:[]}_runTask(e){const r=this._nextFreeWorker();if(r){this._workerTask.set(r,e.id);const i=this._getTransferableData(e.data);return r.postMessage(e,i),!0}return!1}cancelTask(e,r){super.cancelTask(e,r),this._workers.find(o=>this._workerTask.get(o)===e)&&this._replaceWorker(this._workers.find(o=>this._workerTask.get(o)===e))}terminate(){this._workers.forEach(e=>e.terminate()),this._workers.splice(0,this._workers.length)}_newBindedWorker(){const e=this._createWorker();return e.addEventListener("message",this._handleWorkerMessage.bind(this)),e.addEventListener("error",this._handleWorkerError.bind(this)),e}_handleWorkerMessage(e){const{data:r,target:i}=e,o=this._workerTask.get(i);if((r==null?void 0:r.id)==null)throw new Error(`Invalid task result missing task ID was received from worker processing task with ID "${o}"`);this._workerTask.delete(i),this._handleTaskResult(r)}_handleWorkerError(e){const{target:r,error:i}=e,o=this._workerTask.get(r);o?this.cancelTask(o,i):this._replaceWorker(r)}_replaceWorker(e){const r=this._workers.indexOf(e);r<0||(this._workers.splice(r,1),this._workers.push(this._newBindedWorker()),this._workerTask.has(e)&&this._workerTask.delete(e))}_nextFreeWorker(){return this._workers.find(e=>!this._workerTask.has(e))}}function u(h,s){class e extends a{_createWorker(){return h()}}return new e(s)}t.TaskScheduler=n,t.WebWorkerPool=a,t.createWorkerPool=u,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})});