var k=Object.defineProperty;var m=(n,e,r)=>e in n?k(n,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[e]=r;var i=(n,e,r)=>(m(n,typeof e!="symbol"?e+"":e,r),r);import{cf as w,cg as _,ch as d,ci as c,cj as f}from"./index-a76b28be.js";import{T as g}from"./TaskQueue-05c94a4d.js";import{r as p,w as y}from"./ktx-parse.modern-6c9f771f.js";class T{constructor(e=4,r){i(this,"_taskQueue",[]);i(this,"_workerStatus",0);i(this,"_workerItems");this.limitedCount=e,this._workerCreator=r,this._workerItems=new Array(e)}prepareWorker(){const e=this.limitedCount,r=new Array(e);for(let t=0;t<e;t++)r.push(this._initWorker(t));return Promise.all(r)}postMessage(e){return new Promise((r,t)=>{const s=this._getIdleWorkerId();if(this._workerStatus|=1<<s,s!==-1){const a=this._workerItems;Promise.resolve(a[s]??this._initWorker(s)).then(()=>{const o=a[s];o.resolve=r,o.reject=t,o.worker.postMessage(e)}).catch(o=>{this._workerStatus^=1<<s,t(o)})}else this._taskQueue.push({resolve:r,reject:t,message:e})})}destroy(){const e=this._workerItems;for(let r=0,t=e.length;r<t;r++){const s=e[r];s.worker.terminate(),s.reject=null,s.resolve=null}e.length=0,this._taskQueue.length=0,this._workerStatus=0}_initWorker(e){return Promise.resolve(this._workerCreator()).then(r=>{const t=s=>{const a=this._workerItems[e];a!=null&&a.reject&&(a.reject(s.error||new Error("Worker error")),this._nextTask(e))};return r.addEventListener("message",this._onMessage.bind(this,e)),r.addEventListener("error",t),r.onerror=t,this._workerItems[e]={worker:r,resolve:null,reject:null},r})}_getIdleWorkerId(){for(let e=0,r=this.limitedCount;e<r;e++)if(!(this._workerStatus&1<<e))return e;return-1}_onMessage(e,r){r.data.error?this._workerItems[e].reject(r.data.error):this._workerItems[e].resolve(r),this._nextTask(e)}_nextTask(e){if(this._taskQueue.length){const r=this._taskQueue.shift(),t=this._workerItems[e];t.resolve=r.resolve,t.reject=r.reject,t.worker.postMessage(r.message)}else this._workerStatus^=1<<e}}function x(n,e){const r=p(n);return r.keyValue.GalaceanTextureParams=new Uint8Array([e.wrapModeU,e.wrapModeV,e.filterMode,e.anisoLevel]),y(r)}const P=new g,l=w("ktx2","upload-cache");class S{get(e){return P.enqueue(()=>_(e,l))}set(e,r){return d(e,r,l)}}const u=new S,W=f("ktx2");class I{constructor(){i(this,"encoderWorkerPool");i(this,"initPromise");this.encoderWorkerPool=new T(4,()=>new Worker(new URL("/_static/ktx2-worker-a04f41ea.js",self.location),{type:"module"})),this.initPromise=this.encoderWorkerPool.prepareWorker()}async encode(e,r={isUASTC:!1,enableDebug:!1,qualityLevel:255,generateMipmap:!0,isSetKTX2SRGBTransferFunc:!1}){let t;const s=c.create();if(Array.isArray(e)&&Array.isArray(e[0])){for(const o of e)for(const h of o)s.update(h);t=s.hex()}else if(Array.isArray(e)){for(const o of e)s.update(o);t=s.hex()}else t=c(e);const a=t+JSON.stringify(r);try{const o=await u.get(a);if(o)return o}catch(o){console.error(o)}return this._encode(a,e,r)}setTextureParams(e,r){return x(e,r)}async _encode(e,r,t={isUASTC:!1,enableDebug:!1,qualityLevel:255,generateMipmap:!0,isSetKTX2SRGBTransferFunc:!1}){W.info(`cannot find cache, use worker encoder for ${e}`),await this.initPromise;const{data:s}=await this.encoderWorkerPool.postMessage({buffer:r,options:t});return u.set(e,s),s}}const C=new I;export{C as k};
