'use strict';

var chunkKHMOMROL_cjs = require('./chunk-KHMOMROL.cjs');
var chunkRVZE7LPI_cjs = require('./chunk-RVZE7LPI.cjs');
var chunkGKDO6NU7_cjs = require('./chunk-GKDO6NU7.cjs');
var chunkVOROB7KJ_cjs = require('./chunk-VOROB7KJ.cjs');
var chunkP55Q2MCT_cjs = require('./chunk-P55Q2MCT.cjs');
var chunkXJDMSVW4_cjs = require('./chunk-XJDMSVW4.cjs');
var chunkLB3IHFAB_cjs = require('./chunk-LB3IHFAB.cjs');
var chunkUCTQICPR_cjs = require('./chunk-UCTQICPR.cjs');
var chunkOISZU73V_cjs = require('./chunk-OISZU73V.cjs');
var chunkOWBDRWL6_cjs = require('./chunk-OWBDRWL6.cjs');
var icons = require('@blueprintjs/icons');
var classnames = require('classnames');
var React2 = require('react');
var unstable = require('@osdk/api/unstable');
var react = require('@osdk/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 classnames__default = /*#__PURE__*/_interopDefault(classnames);
var React2__default = /*#__PURE__*/_interopDefault(React2);
var UTIF__namespace = /*#__PURE__*/_interopNamespace(UTIF);

// src/document-viewer/DocumentViewerApi.ts
var ViewerType = /* @__PURE__ */ (function(ViewerType2) {
  ViewerType2["Pdf"] = "pdf";
  ViewerType2["Tiff"] = "tiff";
  ViewerType2["Image"] = "image";
  ViewerType2["Video"] = "video";
  ViewerType2["Markdown"] = "markdown";
  ViewerType2["Excel"] = "excel";
  ViewerType2["Email"] = "email";
  ViewerType2["Xml"] = "xml";
  ViewerType2["Unsupported"] = "unsupported";
  return ViewerType2;
})({});
var DISABLED_RESULT = {
  viewerType: ViewerType.Tiff,
  pdfData: void 0,
  loading: false,
  error: void 0
};
function useTiffToPdf(media, enabled) {
  const client = react.useOsdkClient();
  const [result, setResult] = React2.useState(DISABLED_RESULT);
  const mediaRef = React2.useRef(media);
  mediaRef.current = media;
  React2.useEffect(() => {
    if (!enabled) {
      setResult(DISABLED_RESULT);
      return;
    }
    let cancelled = false;
    setResult({
      viewerType: ViewerType.Tiff,
      pdfData: void 0,
      loading: true,
      error: void 0
    });
    async function detectAndTransform() {
      const currentMedia = mediaRef.current;
      const response = await currentMedia.fetchContents();
      const buffer = await response.arrayBuffer();
      if (cancelled) return;
      const ifds = UTIF__namespace.decode(buffer);
      const pageCount = ifds.length;
      if (pageCount <= 1) {
        if (!cancelled) {
          setResult({
            viewerType: ViewerType.Tiff,
            pdfData: void 0,
            loading: false,
            error: void 0
          });
        }
        return;
      }
      const mediaReference = currentMedia.getMediaReference();
      const pdfResponse = await client(unstable.__EXPERIMENTAL__NOT_SUPPORTED_YET__transformAndWait).transformAndWait({
        mediaReference,
        transformation: {
          $imageToDocument: {
            $operation: {
              $createPdf: {}
            }
          }
        }
      });
      if (cancelled) return;
      const pdfBuffer = await pdfResponse.arrayBuffer();
      if (!cancelled) {
        setResult({
          viewerType: ViewerType.Pdf,
          pdfData: pdfBuffer,
          loading: false,
          error: void 0
        });
      }
    }
    detectAndTransform().catch((err) => {
      if (!cancelled) {
        console.warn("TIFF to PDF conversion failed, falling back to TIFF renderer:", err);
        setResult({
          viewerType: ViewerType.Tiff,
          pdfData: void 0,
          loading: false,
          error: err instanceof Error ? err : new Error(String(err))
        });
      }
    });
    return () => {
      cancelled = true;
    };
  }, [media, enabled, client]);
  return result;
}

// src/document-viewer/DocumentViewer.module.css
var DocumentViewer_default = {};

// src/document-viewer/TiffDocumentViewer.tsx
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);
}
function TiffDocumentViewer({
  media,
  className,
  enableTiffToPdf,
  tiffRendererProps,
  pdfViewerProps
}) {
  const {
    viewerType,
    pdfData,
    loading
  } = useTiffToPdf(media, enableTiffToPdf);
  if (loading) {
    return /* @__PURE__ */ React2__default.default.createElement("div", {
      className
    }, /* @__PURE__ */ React2__default.default.createElement("div", {
      className: DocumentViewer_default.loadingContainer
    }, /* @__PURE__ */ React2__default.default.createElement(icons.Spin, {
      className: DocumentViewer_default.spinnerIcon
    }), "Converting document\u2026"));
  }
  if (viewerType === ViewerType.Pdf && pdfData != null) {
    return /* @__PURE__ */ React2__default.default.createElement(chunkXJDMSVW4_cjs.BasePdfViewer, _extends({
      src: pdfData,
      className
    }, pdfViewerProps));
  }
  return /* @__PURE__ */ React2__default.default.createElement(chunkLB3IHFAB_cjs.TiffViewerMedia, _extends({
    media,
    className
  }, tiffRendererProps));
}

// src/document-viewer/DocumentViewer.tsx
function _extends2() {
  return _extends2 = 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;
  }, _extends2.apply(null, arguments);
}
var IMAGE_MIME_TYPES = /* @__PURE__ */ new Set(["image/png", "image/jpeg", "image/gif", "image/svg+xml", "image/webp", "image/bmp"]);
var MARKDOWN_MIME_TYPES = /* @__PURE__ */ new Set(["text/markdown", "text/x-markdown"]);
var XML_MIME_TYPES = /* @__PURE__ */ new Set(["application/xml", "text/xml"]);
function isTiffFile(mimeType, fileName) {
  if (mimeType === "image/tiff") {
    return true;
  }
  const lowered = fileName?.toLowerCase();
  return lowered?.endsWith(".tif") === true || lowered?.endsWith(".tiff") === true;
}
function getViewerType(mimeType, fileName) {
  if (mimeType === "application/pdf") {
    return ViewerType.Pdf;
  }
  if (isTiffFile(mimeType, fileName)) {
    return ViewerType.Tiff;
  }
  if (IMAGE_MIME_TYPES.has(mimeType)) {
    return ViewerType.Image;
  }
  if (mimeType.startsWith("video/")) {
    return ViewerType.Video;
  }
  if (MARKDOWN_MIME_TYPES.has(mimeType)) {
    return ViewerType.Markdown;
  }
  if (mimeType === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
    return ViewerType.Excel;
  }
  if (mimeType === "message/rfc822") {
    return ViewerType.Email;
  }
  if (XML_MIME_TYPES.has(mimeType)) {
    return ViewerType.Xml;
  }
  return ViewerType.Unsupported;
}
function DocumentViewer({
  media,
  className,
  mimeTypeOverride,
  pdfViewerProps,
  imageViewerProps,
  videoViewerProps,
  tiffRendererProps,
  markdownRendererProps,
  excelViewerProps,
  emailViewerProps,
  xmlViewerProps,
  fileName,
  enableTiffToPdf = false
}) {
  const mimeType = mimeTypeOverride ?? media.getMediaReference().mimeType;
  const viewerType = React2.useMemo(() => getViewerType(mimeType, fileName), [mimeType, fileName]);
  const rootClassName = classnames__default.default(DocumentViewer_default.container, className);
  switch (viewerType) {
    case ViewerType.Pdf:
      return /* @__PURE__ */ React2__default.default.createElement(chunkXJDMSVW4_cjs.PdfViewer, _extends2({
        media,
        className: rootClassName
      }, pdfViewerProps));
    case ViewerType.Tiff:
      if (enableTiffToPdf) {
        return /* @__PURE__ */ React2__default.default.createElement(TiffDocumentViewer, {
          media,
          className: rootClassName,
          enableTiffToPdf,
          tiffRendererProps,
          pdfViewerProps
        });
      }
      return /* @__PURE__ */ React2__default.default.createElement(chunkLB3IHFAB_cjs.TiffViewerMedia, _extends2({
        media,
        className: rootClassName
      }, tiffRendererProps));
    case ViewerType.Image:
      return /* @__PURE__ */ React2__default.default.createElement(chunkVOROB7KJ_cjs.ImageViewer, _extends2({
        media,
        className: rootClassName
      }, imageViewerProps));
    case ViewerType.Video:
      return /* @__PURE__ */ React2__default.default.createElement(chunkKHMOMROL_cjs.VideoViewer, _extends2({
        media,
        className: rootClassName
      }, videoViewerProps));
    case ViewerType.Markdown:
      return /* @__PURE__ */ React2__default.default.createElement(chunkP55Q2MCT_cjs.MarkdownViewerMedia, _extends2({
        media,
        className: rootClassName
      }, markdownRendererProps));
    case ViewerType.Excel:
      return /* @__PURE__ */ React2__default.default.createElement(chunkGKDO6NU7_cjs.ExcelViewer, _extends2({
        media,
        className: rootClassName
      }, excelViewerProps));
    case ViewerType.Email:
      return /* @__PURE__ */ React2__default.default.createElement(chunkOISZU73V_cjs.EmailViewer, _extends2({
        media,
        className: rootClassName
      }, emailViewerProps));
    case ViewerType.Xml:
      return /* @__PURE__ */ React2__default.default.createElement(chunkRVZE7LPI_cjs.XmlViewer, _extends2({
        media,
        className: rootClassName
      }, xmlViewerProps));
    case ViewerType.Unsupported:
      return /* @__PURE__ */ React2__default.default.createElement("div", {
        className: rootClassName
      }, /* @__PURE__ */ React2__default.default.createElement("div", {
        className: DocumentViewer_default.unsupportedContainer
      }, /* @__PURE__ */ React2__default.default.createElement(icons.Document, {
        className: DocumentViewer_default.unsupportedIcon
      }), "Unsupported file type: ", mimeType));
    default:
      chunkUCTQICPR_cjs.assertUnreachable(viewerType);
  }
}

// src/public/experimental/document-viewer.ts
var DocumentViewer2 = chunkOWBDRWL6_cjs.withOsdkMetrics(DocumentViewer, "DocumentViewer");

exports.DocumentViewer = DocumentViewer2;
exports.ViewerType = ViewerType;
//# sourceMappingURL=chunk-GLBBDB6P.cjs.map
//# sourceMappingURL=chunk-GLBBDB6P.cjs.map