/*
 * 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 { getDocument, GlobalWorkerOptions } from "pdfjs-dist";
const pdfWorkerUrl = new URL("pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url);
import { useEffect, useState } from "react";
/**
 * Resolve a {@link PdfSource} to pdfjs `getDocument` parameters.
 *
 * A Blob is read into an ArrayBuffer first; URLs and in-memory bytes
 * (ArrayBuffer/Uint8Array) resolve immediately. Always returns a promise so
 * callers have a single code path regardless of source type.
 */
function toDocumentParams(src) {
  if (src instanceof Blob) {
    return src.arrayBuffer().then(data => ({
      data
    }));
  }
  return Promise.resolve(typeof src === "string" ? {
    url: src
  } : {
    data: src
  });
}
const pdfWorker = {
  workerInitialized: false,
  ensureWorker() {
    GlobalWorkerOptions.workerSrc = pdfWorkerUrl.href;
    this.workerInitialized = true;
  }
};
export function usePdfDocument(src) {
  const [document, setDocument] = useState(undefined);
  const [numPages, setNumPages] = useState(0);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(undefined);
  useEffect(function () {
    pdfWorker.ensureWorker();
    setLoading(true);
    setError(undefined);
    let cancelled = false;
    let loadingTask;
    const startLoad = params => {
      if (cancelled) {
        return;
      }
      loadingTask = getDocument(params);
      loadingTask.promise.then(pdf => {
        if (!cancelled) {
          setDocument(pdf);
          setNumPages(pdf.numPages);
          setLoading(false);
        }
      }, err => {
        if (!cancelled) {
          setError(err instanceof Error ? err : new Error(String(err)));
          setLoading(false);
        }
      });
    };
    toDocumentParams(src).then(startLoad, err => {
      if (!cancelled) {
        setError(err instanceof Error ? err : new Error(String(err)));
        setLoading(false);
      }
    });
    return () => {
      cancelled = true;
      void loadingTask?.destroy();
    };
  }, [src]);
  return {
    document,
    numPages,
    loading,
    error
  };
}
//# sourceMappingURL=usePdfDocument.js.map