/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Error as ErrorIcon, Spin } from "@blueprintjs/icons";
import classnames from "classnames";
import "pdfjs-dist/web/pdf_viewer.css";
import React, { useEffect, useRef } from "react";
import { EMPTY_ANNOTATION_ARRAY } from "../constants.js";
import { usePdfAnnotationsByPage } from "../hooks/usePdfAnnotationsByPage.js";
import { usePdfFormFields } from "../hooks/usePdfFormFields.js";
import { usePdfViewerCore } from "../hooks/usePdfViewerCore.js";
import { PdfAnnotationOverlay } from "./PdfAnnotationOverlay.js";
import styles from "../PdfViewer.module.css.js";
export function PdfViewerContent({
  src,
  annotations = EMPTY_ANNOTATION_ARRAY,
  onAnnotationClick,
  initialPage = 1,
  initialScale = 1.0,
  onPageChange: onPageChangeProp,
  onScaleChange: onScaleChangeProp,
  formData,
  onFormChange,
  className
}) {
  const viewer = usePdfViewerCore({
    src,
    initialPage,
    initialScale
  });
  const annotationsByPage = usePdfAnnotationsByPage(annotations);
  usePdfFormFields({
    pdfViewerRef: viewer.pdfViewerRef,
    eventBusRef: viewer.eventBusRef,
    document: viewer.document,
    formData,
    onFormChange
  });

  // Use refs for callbacks to avoid firing on initial mount
  const onPageChangeRef = useRef(onPageChangeProp);
  onPageChangeRef.current = onPageChangeProp;
  const onScaleChangeRef = useRef(onScaleChangeProp);
  onScaleChangeRef.current = onScaleChangeProp;
  const isInitialMountRef = useRef(true);
  useEffect(function () {
    if (isInitialMountRef.current) {
      return;
    }
    onPageChangeRef.current?.(viewer.currentPage);
  }, [viewer.currentPage]);
  useEffect(function () {
    if (isInitialMountRef.current) {
      return;
    }
    onScaleChangeRef.current?.(viewer.scale);
  }, [viewer.scale]);
  useEffect(function () {
    isInitialMountRef.current = false;
  }, []);
  const rootClassName = classnames(styles.pdfViewer, className);
  if (viewer.loading) {
    return /*#__PURE__*/React.createElement("div", {
      className: rootClassName
    }, /*#__PURE__*/React.createElement("div", {
      className: styles.loadingContainer
    }, /*#__PURE__*/React.createElement(Spin, {
      className: styles.spinnerIcon
    }), "Loading\u2026"));
  }
  if (viewer.error != null) {
    return /*#__PURE__*/React.createElement("div", {
      className: rootClassName
    }, /*#__PURE__*/React.createElement("div", {
      className: styles.errorContainer
    }, /*#__PURE__*/React.createElement(ErrorIcon, {
      className: styles.errorIcon
    }), "Failed to load PDF: ", viewer.error.message));
  }
  if (viewer.document == null) {
    return /*#__PURE__*/React.createElement("div", {
      className: rootClassName
    }, /*#__PURE__*/React.createElement("div", {
      className: styles.loadingContainer
    }, "No document"));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: rootClassName
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.scrollContainerWrapper
  }, /*#__PURE__*/React.createElement("div", {
    ref: viewer.containerRef,
    className: styles.scrollContainer
  }, /*#__PURE__*/React.createElement("div", {
    ref: viewer.viewerRef,
    className: "pdfViewer"
  }), viewer.portalTargets.map(target => {
    const pageAnnotations = annotationsByPage[target.pageNumber] ?? EMPTY_ANNOTATION_ARRAY;
    if (pageAnnotations.length === 0) {
      return null;
    }
    return /*#__PURE__*/React.createElement(PdfAnnotationOverlay, {
      key: target.pageNumber,
      target: target,
      annotations: pageAnnotations,
      onAnnotationClick: onAnnotationClick
    });
  }))));
}
//# sourceMappingURL=PdfViewerContent.js.map