'use strict';

var chunkMVQKV5BJ_cjs = require('./chunk-MVQKV5BJ.cjs');
var icons = require('@blueprintjs/icons');
var React = require('react');
var UTIF = require('utif');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__default = /*#__PURE__*/_interopDefault(React);
var UTIF__namespace = /*#__PURE__*/_interopNamespace(UTIF);

// src/images/tiff-renderer/TiffRenderer.module.css
var TiffRenderer_default = {};

// src/images/tiff-renderer/TiffRenderer.tsx
var MAX_IMAGE_SIZE = 25e6;
var MAX_IMAGE_SIZE_MB_STRING = (MAX_IMAGE_SIZE / 1e6).toFixed(1);
function decodeTiff(content) {
  if (content.byteLength > MAX_IMAGE_SIZE) {
    return {
      status: "error",
      message: `TIFF file exceeds maximum size of ${MAX_IMAGE_SIZE_MB_STRING}MB`
    };
  }
  try {
    const buffer = content.buffer;
    const ifds = UTIF__namespace.decode(buffer);
    const image = ifds[0];
    if (image == null) {
      return {
        status: "error",
        message: "Could not render TIFF file (it may be corrupted)"
      };
    }
    UTIF__namespace.decodeImage(buffer, image);
    const rgba = UTIF__namespace.toRGBA8(image);
    if (image.width == null || image.height == null) {
      return {
        status: "error",
        message: "Could not render TIFF file (it may be corrupted)"
      };
    }
    return {
      status: "ok",
      data: {
        width: image.width,
        height: image.height,
        content: rgba
      }
    };
  } catch {
    return {
      status: "error",
      message: "Could not render TIFF file (it may be corrupted)"
    };
  }
}
var ErrorMessage = /* @__PURE__ */ React__default.default.memo(({
  message
}) => /* @__PURE__ */ React__default.default.createElement("div", {
  className: TiffRenderer_default.errorContainer
}, /* @__PURE__ */ React__default.default.createElement(icons.Error, {
  className: TiffRenderer_default.errorIcon
}), message));
var TiffCanvas = /* @__PURE__ */ React__default.default.memo(({
  imageData
}) => {
  const canvasRef = React.useCallback((canvas) => {
    if (canvas == null) {
      return;
    }
    const ctx = canvas.getContext("2d");
    if (ctx == null) {
      return;
    }
    const renderedImageData = ctx.createImageData(imageData.width, imageData.height);
    renderedImageData.data.set(imageData.content);
    ctx.putImageData(renderedImageData, 0, 0);
  }, [imageData]);
  return /* @__PURE__ */ React__default.default.createElement("canvas", {
    className: TiffRenderer_default.canvas,
    width: imageData.width,
    height: imageData.height,
    ref: canvasRef
  });
});
var TiffRenderer = /* @__PURE__ */ React__default.default.memo(({
  content,
  onError
}) => {
  const [result, setResult] = React.useState(void 0);
  const onErrorRef = React.useRef(onError);
  onErrorRef.current = onError;
  React.useEffect(() => {
    const decodeResult = decodeTiff(content);
    setResult(decodeResult);
    if (decodeResult.status === "error") {
      onErrorRef.current?.();
    }
  }, [content]);
  if (result == null) {
    return null;
  }
  if (result.status === "error") {
    return /* @__PURE__ */ React__default.default.createElement(ErrorMessage, {
      message: result.message
    });
  }
  return /* @__PURE__ */ React__default.default.createElement(TiffCanvas, {
    imageData: result.data
  });
});
function _extends() {
  return _extends = Object.assign ? Object.assign.bind() : function(n) {
    for (var e = 1; e < arguments.length; e++) {
      var t = arguments[e];
      for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
    }
    return n;
  }, _extends.apply(null, arguments);
}
var transformToUint8Array = async (response) => {
  const buffer = await response.arrayBuffer();
  return new Uint8Array(buffer);
};
function TiffViewerMedia({
  media,
  className,
  ...tiffRendererProps
}) {
  const {
    data: content,
    loading,
    error
  } = chunkMVQKV5BJ_cjs.useMediaContents(media, transformToUint8Array);
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className
  }, loading && /* @__PURE__ */ React__default.default.createElement("div", {
    className: TiffRenderer_default.loadingContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Spin, {
    className: TiffRenderer_default.spinnerIcon
  }), "Loading\u2026"), error != null && /* @__PURE__ */ React__default.default.createElement("div", {
    className: TiffRenderer_default.errorContainer
  }, /* @__PURE__ */ React__default.default.createElement(icons.Error, {
    className: TiffRenderer_default.errorIcon
  }), "Failed to load TIFF: ", error.message), content != null && /* @__PURE__ */ React__default.default.createElement(TiffRenderer, _extends({
    content
  }, tiffRendererProps)));
}

exports.TiffRenderer = TiffRenderer;
exports.TiffViewerMedia = TiffViewerMedia;
//# sourceMappingURL=chunk-LB3IHFAB.cjs.map
//# sourceMappingURL=chunk-LB3IHFAB.cjs.map