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); }
/*
 * 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 React, { useEffect, useState } from "react";
import { BasePdfViewer } from "./PdfViewer.js";
import styles from "./PdfViewer.module.css";
async function fetchMediaContents(media) {
  const result = await media.fetchContents();
  return result.arrayBuffer();
}
export function PdfViewer({
  media,
  className,
  ...pdfViewerProps
}) {
  const [src, setSrc] = useState(undefined);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(undefined);

  // This is required until we either support React 19+ or a data fetching hook
  useEffect(function () {
    let cancelled = false;
    setLoading(true);
    setError(undefined);
    setSrc(undefined);
    fetchMediaContents(media).then(buffer => {
      if (!cancelled) {
        setSrc(buffer);
        setLoading(false);
      }
    }).catch(err => {
      if (!cancelled) {
        setError(err instanceof Error ? err : new Error(String(err)));
        setLoading(false);
      }
    });
    return () => {
      cancelled = true;
    };
  }, [media]);
  const rootClassName = classnames(styles.pdfViewer, className);
  if (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 (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: ", error.message));
  }
  if (src == null) {
    return /*#__PURE__*/React.createElement("div", {
      className: rootClassName
    }, /*#__PURE__*/React.createElement("div", {
      className: styles.loadingContainer
    }, "No document"));
  }
  return /*#__PURE__*/React.createElement(BasePdfViewer, _extends({
    src: src,
    className: className
  }, pdfViewerProps));
}
//# sourceMappingURL=PdfRenderer.js.map