import * as thumbhash from "https://esm.sh/thumbhash@0.1.1";

export function generateThumbhash(buffer: Uint8Array, w: number, h: number) {
  const hash = thumbhash.rgbaToThumbHash(w, h, buffer);
  return [hash, thumbhash.thumbHashToAverageRGBA(hash)] as const;
}

export function generateDataURL(hash: string) {
  const decodedString = atob(hash);

  // Create Uint8Array from decoded string
  const uint8Array = new Uint8Array(decodedString.length);
  for (let i = 0; i < decodedString.length; i++) {
    uint8Array[i] = decodedString.charCodeAt(i);
  }

  return thumbhash.thumbHashToDataURL(uint8Array);
}
