import {
  Skia,
  AlphaType,
  ColorType,
  type SkImage,
} from '@shopify/react-native-skia';

/** pickMap pixel value regionId+1 → RGBA texture (R channel stores lookup code) */
export function pickBufferToSkImage(
  pickBuffer: Uint8Array,
  cols: number,
  rows: number,
): SkImage | null {
  if (pickBuffer.length !== cols * rows) {
    return null;
  }
  const rgba = new Uint8Array(cols * rows * 4);
  for (let i = 0; i < pickBuffer.length; i++) {
    const o = i * 4;
    const code = pickBuffer[i];
    rgba[o] = code;
    rgba[o + 1] = code;
    rgba[o + 2] = code;
    rgba[o + 3] = 255;
  }
  const data = Skia.Data.fromBytes(rgba);
  return Skia.Image.MakeImage(
    {
      width: cols,
      height: rows,
      alphaType: AlphaType.Opaque,
      colorType: ColorType.RGBA_8888,
    },
    data,
    cols * 4,
  );
}
