// Generated by pdfx init.
// See: https://pdfx.akashpise.dev/installation#theming
//
// Provides runtime theme overrides via React context.
// Wrap a subtree in <PdfxThemeProvider theme={myTheme}> to override defaults.

/* eslint-disable react-refresh/only-export-components */
// Intentional: this module exports both a component and hooks/context.
// Keeping a single import surface preserves the generated public API.

import { type DependencyList, type ReactNode, createContext, useContext } from 'react';
import { theme as defaultTheme } from './pdfx-theme';

type PdfxTheme = typeof defaultTheme;

export const PdfxThemeContext = createContext<PdfxTheme>(defaultTheme);

export interface PdfxThemeProviderProps {
  theme?: PdfxTheme;
  children: ReactNode;
}

export function PdfxThemeProvider({ theme, children }: PdfxThemeProviderProps) {
  const resolvedTheme = theme ?? defaultTheme;
  return <PdfxThemeContext.Provider value={resolvedTheme}>{children}</PdfxThemeContext.Provider>;
}

/**
 * Returns the active theme from context.
 *
 * When called outside a React render tree (for example in plain-function tests),
 * React may throw an invalid hook error. In that case we return defaultTheme.
 * Unexpected errors are re-thrown.
 */
export function usePdfxTheme(): PdfxTheme {
  try {
    return useContext(PdfxThemeContext);
  } catch (error) {
    // Suppress only known React dispatcher errors.
    if (
      error instanceof Error &&
      /invalid hook call|useContext|cannot read properties of null/i.test(error.message)
    ) {
      return defaultTheme;
    }
    throw error;
  }
}

/**
 * Calls factory() and returns the result.
 * The deps parameter is accepted for API compatibility with existing callers.
 */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function useSafeMemo<T>(factory: () => T, _deps: DependencyList): T {
  return factory();
}
