/*
 * 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 React, { createContext, useContext, useMemo } from "react";
import { EMPTY_ANNOTATION_ARRAY } from "./constants.js";
import { usePdfAnnotationsByPage } from "./hooks/usePdfAnnotationsByPage.js";
import { usePdfFormFields } from "./hooks/usePdfFormFields.js";
import { usePdfHighlightMode } from "./hooks/usePdfHighlightMode.js";
import { usePdfViewerState } from "./hooks/usePdfViewerState.js";

/** The shape of the value provided by {@link PdfViewerProvider}. */

const PdfViewerContext = /*#__PURE__*/createContext(null);
/** Provides {@link PdfViewerContextValue} to descendant components. */
export function PdfViewerProvider({
  value,
  children
}) {
  return /*#__PURE__*/React.createElement(PdfViewerContext.Provider, {
    value: value
  }, children);
}

/**
 * Returns the nearest {@link PdfViewerContextValue} from a {@link PdfViewerProvider}.
 * Throws if called outside a provider.
 */
export function usePdfViewerContext() {
  const ctx = useContext(PdfViewerContext);
  if (ctx == null) {
    throw new Error("usePdfViewerContext must be used within a PdfViewerProvider");
  }
  return ctx;
}

/**
 * Consumer-facing setup hook analogous to react-hook-form's `useForm()`.
 * Accepts viewer options and returns a {@link PdfViewerContextValue} suitable
 * for passing to {@link PdfViewerProvider}.
 */
export function usePdfViewerInstance(options) {
  const viewer = 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
  } = usePdfHighlightMode({
    pdfViewerRef: viewer.pdfViewerRef,
    document: viewer.document,
    enabled: options.highlightEnabled ?? false,
    onTextHighlight: options.onTextHighlight,
    onHighlightDelete: options.onHighlightDelete
  });
  const {
    hasFormFields,
    submitFormData
  } = usePdfFormFields({
    pdfViewerRef: viewer.pdfViewerRef,
    eventBusRef: viewer.eventBusRef,
    document: viewer.document,
    formData: options.formData,
    onFormSubmit: options.onFormSubmit,
    onFormChange: options.onFormChange
  });
  const annotations = options.annotations ?? EMPTY_ANNOTATION_ARRAY;
  const annotationsByPage = usePdfAnnotationsByPage(annotations);
  const highlightEnabled = options.highlightEnabled ?? false;
  const enableDownload = options.enableDownload ?? false;
  const enableFormSave = options.onFormSubmit != null && hasFormFields;
  const {
    onAnnotationClick,
    outlineIcons
  } = options;
  return 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]);
}
//# sourceMappingURL=PdfViewerContext.js.map