// ...existing code...
const worker = new Worker(new URL('./image.worker.ts', import.meta.url), { type: 'module' });

type WorkerReq =
  | { id: number; type: 'blurhashToBase64Image'; payload: { blurhash: string; width: number; height: number } }
  | { id: number; type: 'base64ImageToBlurhash'; payload: { base64Image: string } };

type WorkerRes =
  | { id: number; ok: true; data: string }
  | { id: number; ok: false; error: string };

let seq = 0;
const pending = new Map<number, (res: WorkerRes) => void>();

worker.onmessage = (e: MessageEvent<WorkerRes>) => {
  const res = e.data;
  const cb = pending.get(res.id);
  if (cb) {
    pending.delete(res.id);
    cb(res);
  }
};

function callWorker(req: Omit<WorkerReq, 'id'>): Promise<string> {
  return new Promise((resolve, reject) => {
    const id = ++seq;
    pending.set(id, (res) => (res.ok ? resolve(res.data) : reject(res.error)));
    worker.postMessage({ ...req, id });
  });
}

export function blurhashToBase64Image(blurhash: string, width: number, height: number): Promise<string> {
  return callWorker({ type: 'blurhashToBase64Image', payload: { blurhash, width, height } });
}

export function base64ImageToBlurhash(base64Image: string): Promise<string> {
  return callWorker({ type: 'base64ImageToBlurhash', payload: { base64Image } });
}
// ...existing code...