"use strict";var t="undefined"!=typeof document?document.currentScript:null;module.exports=class{constructor(t={}){this.wasmModule=null,this.isInitialized=!1,this.wasmUrl=t.wasmUrl,this.preloadedWasmModule=t.wasmModule,this.initPromise=this.initializeWasm()}async initializeWasm(){if(!this.isInitialized){if(this.preloadedWasmModule)return this.wasmModule=this.preloadedWasmModule,await this.wasmModule.default(this.wasmUrl),void(this.isInitialized=!0);try{const e=this.wasmUrl||new URL("./wasm/libimagequant_wasm.js","undefined"==typeof document?require("url").pathToFileURL(__filename).href:t&&"SCRIPT"===t.tagName.toUpperCase()&&t.src||new URL("index.cjs",document.baseURI).href).href;this.wasmModule=await import(e),await this.wasmModule.default(this.wasmUrl),this.isInitialized=!0}catch(e){throw new Error(`Failed to load WASM module. For Next.js applications, please provide the WASM module directly: \nimport wasmModule from '@fe-daily/libimagequant-wasm/wasm/libimagequant_wasm.js';\nconst quantizer = new LibImageQuant({ wasmModule });\nOriginal error: ${e instanceof Error?e.message:String(e)}`)}}}async quantizePng(t,e={}){let a;if(await this.initPromise,t instanceof Blob){const e=await t.arrayBuffer();a=new Uint8Array(e)}else a=t instanceof ArrayBuffer?new Uint8Array(t):t;const i=this.wasmModule.decode_png_to_rgba(a),s=i[0],n=i[1],o=i[2];return await this.quantizeRgbaData(s,n,o,e)}async quantizeImageData(t,e={}){await this.initPromise;const a=new Uint8ClampedArray(t.data);return await this.quantizeRgbaData(a,t.width,t.height,e)}async quantizeRgbaData(t,e,a,i){const s=new this.wasmModule.ImageQuantizer;if(void 0!==i.speed&&s.setSpeed(i.speed),void 0!==i.quality){const{min:t=0,target:e=100}=i.quality;s.setQuality(t,e)}void 0!==i.maxColors&&s.setMaxColors(i.maxColors),void 0!==i.posterization&&s.setPosterization(i.posterization);const n=s.quantizeImage(t,e,a),o=n.getPalette(),r=n.getQuantizationQuality(),l=n.getPaletteLength();void 0!==i.dithering&&n.setDithering(i.dithering);const d=n.remapImage(t,e,a),m=n.getPaletteIndices(t,e,a),u=this.wasmModule.encode_palette_to_png(m,o,e,a);let h;try{h=new ImageData(d,e,a)}catch(w){h={data:d,width:e,height:a,colorSpace:"srgb"}}return{palette:o,pngBytes:u,imageData:h,quality:r,paletteLength:l,width:e,height:a}}dispose(){}};
//# sourceMappingURL=index.cjs.map
