import * as pathLib from 'path'

const getExtensionOfFilename = (filename) => pathLib.extname(filename).toLowerCase()

async function loadAndRenderTiffImage(imgUrl) {
  if (getExtensionOfFilename(imgUrl) !== '.tiff') {
    return;
  }

  const response = await fetch(imgUrl);
  const arrayBuffer = await response.arrayBuffer();

  const ifds = UTIF.decode(arrayBuffer);
  UTIF.decodeImage(arrayBuffer, ifds[0]);
  const rgba = UTIF.toRGBA8(ifds[0]);

  const cnv = document.createElement('canvas');
  cnv.width = ifds[0]?.width || 100;
  cnv.height = ifds[0]?.height || 100;

  const ctx = cnv.getContext('2d');
  const imageData = ctx.createImageData(cnv.width, cnv.height);

  const imageSize = { editorWidth: cnv.width, naturalWidth: cnv.width };
  dispatch(initializeRoi({ imageSize, annotId: annotationInfo.id }));

  for (let i = 0; i < rgba.length; i++) {
    imageData.data[i] = rgba[i];
  }

  ctx.putImageData(imageData, 0, 0);

  setImgObject(cnv);
  setImageLoaded(true);
  handleImageSizeOnload(cnv);
}

// 사용 예제
loadAndRenderTiffImage(imgUrl);