'use strict';

var chunkXJDMSVW4_cjs = require('./chunk-XJDMSVW4.cjs');
var chunkOWBDRWL6_cjs = require('./chunk-OWBDRWL6.cjs');
var icons = require('@blueprintjs/icons');
var classnames = require('classnames');
require('pdfjs-dist/web/pdf_viewer.css');
var React = require('react');

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

var classnames__default = /*#__PURE__*/_interopDefault(classnames);
var React__default = /*#__PURE__*/_interopDefault(React);

function PdfViewerContent({
  src,
  annotations = chunkXJDMSVW4_cjs.EMPTY_ANNOTATION_ARRAY,
  onAnnotationClick,
  initialPage = 1,
  initialScale = 1,
  onPageChange: onPageChangeProp,
  onScaleChange: onScaleChangeProp,
  formData,
  onFormChange,
  className
}) {
  const viewer = chunkXJDMSVW4_cjs.usePdfViewerCore({
    src,
    initialPage,
    initialScale
  });
  const annotationsByPage = chunkXJDMSVW4_cjs.usePdfAnnotationsByPage(annotations);
  chunkXJDMSVW4_cjs.usePdfFormFields({
    pdfViewerRef: viewer.pdfViewerRef,
    eventBusRef: viewer.eventBusRef,
    document: viewer.document,
    formData,
    onFormChange
  });
  const onPageChangeRef = React.useRef(onPageChangeProp);
  onPageChangeRef.current = onPageChangeProp;
  const onScaleChangeRef = React.useRef(onScaleChangeProp);
  onScaleChangeRef.current = onScaleChangeProp;
  const isInitialMountRef = React.useRef(true);
  React.useEffect(function notifyPageChange() {
    if (isInitialMountRef.current) {
      return;
    }
    onPageChangeRef.current?.(viewer.currentPage);
  }, [viewer.currentPage]);
  React.useEffect(function notifyScaleChange() {
    if (isInitialMountRef.current) {
      return;
    }
    onScaleChangeRef.current?.(viewer.scale);
  }, [viewer.scale]);
  React.useEffect(function clearInitialMount() {
    isInitialMountRef.current = false;
  }, []);
  const rootClassName = classnames__default.default(chunkXJDMSVW4_cjs.PdfViewer_default.pdfViewer, className);
  if (viewer.loading) {
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: rootClassName
    }, /* @__PURE__ */ React__default.default.createElement("div", {
      className: chunkXJDMSVW4_cjs.PdfViewer_default.loadingContainer
    }, /* @__PURE__ */ React__default.default.createElement(icons.Spin, {
      className: chunkXJDMSVW4_cjs.PdfViewer_default.spinnerIcon
    }), "Loading\u2026"));
  }
  if (viewer.error != null) {
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: rootClassName
    }, /* @__PURE__ */ React__default.default.createElement("div", {
      className: chunkXJDMSVW4_cjs.PdfViewer_default.errorContainer
    }, /* @__PURE__ */ React__default.default.createElement(icons.Error, {
      className: chunkXJDMSVW4_cjs.PdfViewer_default.errorIcon
    }), "Failed to load PDF: ", viewer.error.message));
  }
  if (viewer.document == null) {
    return /* @__PURE__ */ React__default.default.createElement("div", {
      className: rootClassName
    }, /* @__PURE__ */ React__default.default.createElement("div", {
      className: chunkXJDMSVW4_cjs.PdfViewer_default.loadingContainer
    }, "No document"));
  }
  return /* @__PURE__ */ React__default.default.createElement("div", {
    className: rootClassName
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    className: chunkXJDMSVW4_cjs.PdfViewer_default.scrollContainerWrapper
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    ref: viewer.containerRef,
    className: chunkXJDMSVW4_cjs.PdfViewer_default.scrollContainer
  }, /* @__PURE__ */ React__default.default.createElement("div", {
    ref: viewer.viewerRef,
    className: "pdfViewer"
  }), viewer.portalTargets.map((target) => {
    const pageAnnotations = annotationsByPage[target.pageNumber] ?? chunkXJDMSVW4_cjs.EMPTY_ANNOTATION_ARRAY;
    if (pageAnnotations.length === 0) {
      return null;
    }
    return /* @__PURE__ */ React__default.default.createElement(chunkXJDMSVW4_cjs.PdfAnnotationOverlay, {
      key: target.pageNumber,
      target,
      annotations: pageAnnotations,
      onAnnotationClick
    });
  }))));
}
var PdfViewerContext = /* @__PURE__ */ React.createContext(null);
function PdfViewerProvider({
  value,
  children
}) {
  return /* @__PURE__ */ React__default.default.createElement(PdfViewerContext.Provider, {
    value
  }, children);
}
function usePdfViewerContext() {
  const ctx = React.useContext(PdfViewerContext);
  if (ctx == null) {
    throw new Error("usePdfViewerContext must be used within a PdfViewerProvider");
  }
  return ctx;
}
function usePdfViewerInstance(options) {
  const viewer = chunkXJDMSVW4_cjs.usePdfViewerState({
    src: options.src,
    initialPage: options.initialPage,
    initialScale: options.initialScale,
    initialAutoSize: options.initialAutoSize,
    initialSidebarOpen: options.initialSidebarOpen,
    sidebarMode: options.sidebarMode,
    onDownload: options.onDownload
  });
  const {
    highlightModeActive,
    toggleHighlightMode
  } = chunkXJDMSVW4_cjs.usePdfHighlightMode({
    pdfViewerRef: viewer.pdfViewerRef,
    document: viewer.document,
    enabled: options.highlightEnabled ?? false,
    onTextHighlight: options.onTextHighlight,
    onHighlightDelete: options.onHighlightDelete
  });
  const {
    hasFormFields,
    submitFormData
  } = chunkXJDMSVW4_cjs.usePdfFormFields({
    pdfViewerRef: viewer.pdfViewerRef,
    eventBusRef: viewer.eventBusRef,
    document: viewer.document,
    formData: options.formData,
    onFormSubmit: options.onFormSubmit,
    onFormChange: options.onFormChange
  });
  const annotations = options.annotations ?? chunkXJDMSVW4_cjs.EMPTY_ANNOTATION_ARRAY;
  const annotationsByPage = chunkXJDMSVW4_cjs.usePdfAnnotationsByPage(annotations);
  const highlightEnabled = options.highlightEnabled ?? false;
  const enableDownload = options.enableDownload ?? false;
  const enableFormSave = options.onFormSubmit != null && hasFormFields;
  const {
    onAnnotationClick,
    outlineIcons
  } = options;
  return React.useMemo(() => ({
    ...viewer,
    highlightEnabled,
    highlightModeActive,
    toggleHighlightMode,
    hasFormFields,
    submitFormData,
    annotations,
    annotationsByPage,
    onAnnotationClick,
    enableDownload,
    enableFormSave,
    outlineIcons
  }), [viewer, highlightEnabled, highlightModeActive, toggleHighlightMode, hasFormFields, submitFormData, annotations, annotationsByPage, onAnnotationClick, enableDownload, enableFormSave, outlineIcons]);
}

// src/public/experimental/pdf-viewer.ts
var PdfViewer2 = chunkOWBDRWL6_cjs.withOsdkMetrics(chunkXJDMSVW4_cjs.PdfViewer, "PdfViewer");

exports.PdfViewer = PdfViewer2;
exports.PdfViewerContent = PdfViewerContent;
exports.PdfViewerProvider = PdfViewerProvider;
exports.usePdfViewerContext = usePdfViewerContext;
exports.usePdfViewerInstance = usePdfViewerInstance;
//# sourceMappingURL=chunk-3OWSHPWL.cjs.map
//# sourceMappingURL=chunk-3OWSHPWL.cjs.map