{"version":3,"file":"index.cjs","sources":["../../src/svelte/context/pdf-engine-context.svelte.ts","../../src/svelte/components/PdfEngineProvider.svelte","../../src/svelte/hooks/use-engine-context.ts","../../src/svelte/hooks/use-pdfium-engine.svelte.ts"],"sourcesContent":["import { getContext, setContext } from 'svelte';\r\nimport type { PdfEngine } from '@elhalawany/models';\r\n\r\nexport interface PdfEngineContextState {\r\n  engine: PdfEngine | null;\r\n  isLoading: boolean;\r\n  error: Error | null;\r\n}\r\n\r\n\r\nconst PDF_ENGINE_CONTEXT_KEY = Symbol('pdfEngineContext');\r\n\r\n/**\r\n * Set the PDF engine context (used by PdfEngineProvider)\r\n */\r\nexport function setPdfEngineContext(value: PdfEngineContextState) {\r\n  setContext(PDF_ENGINE_CONTEXT_KEY, value);\r\n}\r\n\r\n/**\r\n * Get the PDF engine context\r\n * @throws Error if used outside of PdfEngineProvider\r\n */\r\nexport function getPdfEngineContext(): PdfEngineContextState {\r\n  const context = getContext<PdfEngineContextState | undefined>(PDF_ENGINE_CONTEXT_KEY);\r\n  \r\n  if (context === undefined) {\r\n    throw new Error('getPdfEngineContext must be used within a PdfEngineProvider');\r\n  }\r\n  \r\n  return context;\r\n}\r\n","<script lang=\"ts\">\r\n  import type { Snippet } from 'svelte';\r\n  import type { PdfEngine } from '@elhalawany/models';\r\n  import { setPdfEngineContext } from '../context';\r\n\r\n  interface Props {\r\n    children: Snippet;\r\n    engine: PdfEngine | null;\r\n    isLoading: boolean;\r\n    error: Error | null;\r\n  }\r\n\r\n  let { children, engine, isLoading, error }: Props = $props();\r\n\r\n  // Update context reactively whenever props change\r\n  $effect(() => {\r\n    setPdfEngineContext({\r\n      engine,\r\n      isLoading,\r\n      error,\r\n    });\r\n  });\r\n</script>\r\n\r\n{@render children()}\r\n","import { getPdfEngineContext, type PdfEngineContextState } from '../context/pdf-engine-context.svelte';\r\n\r\n/**\r\n * Hook to access the PDF engine from context.\r\n * @returns The PDF engine context state\r\n * @throws Error if used outside of PdfEngineProvider\r\n */\r\nexport function useEngineContext(): PdfEngineContextState {\r\n  const contextValue = getPdfEngineContext();\r\n\r\n  if (contextValue === undefined) {\r\n    throw new Error('useEngineContext must be used within a PdfEngineProvider');\r\n  }\r\n\r\n  return contextValue;\r\n}\r\n\r\n/**\r\n * Hook to access the PDF engine, with a more convenient API.\r\n * @returns The PDF engine or null if loading/error\r\n */\r\nexport function useEngine() {\r\n  const { engine, error } = getPdfEngineContext();\r\n\r\n  if (error) {\r\n    throw error;\r\n  }\r\n\r\n  return engine;\r\n}\r\n\r\n","import { ignore, Logger, PdfEngine } from '@elhalawany/models';\r\nimport { DEFAULT_PDFIUM_WASM_URL } from '@elhalawany/engines';\r\n\r\nexport interface UsePdfiumEngineProps {\r\n  wasmUrl?: string;\r\n  worker?: boolean;\r\n  logger?: Logger;\r\n}\r\n\r\nexport function usePdfiumEngine(config?: UsePdfiumEngineProps) {\r\n  const { wasmUrl = DEFAULT_PDFIUM_WASM_URL, worker = true, logger } = config ?? {};\r\n\r\n  // Create a reactive state object\r\n  const state = $state({\r\n    engine: null as PdfEngine | null,\r\n    isLoading: true,\r\n    error: null as Error | null,\r\n  });\r\n\r\n  let engineRef = $state<PdfEngine | null>(null);\r\n\r\n  const isBrowser = typeof window !== 'undefined';\r\n\r\n  if (isBrowser) {\r\n    $effect(() => {\r\n      let cancelled = false;\r\n\r\n      (async () => {\r\n        try {\r\n          const { createPdfiumEngine } = worker\r\n            ? await import('@elhalawany/engines/pdfium-worker-engine')\r\n            : await import('@elhalawany/engines/pdfium-direct-engine');\r\n\r\n          const pdfEngine = await createPdfiumEngine(wasmUrl, logger);\r\n          engineRef = pdfEngine;\r\n\r\n          pdfEngine.initialize().wait(\r\n            () => {\r\n              state.engine = pdfEngine;\r\n              state.isLoading = false;\r\n            },\r\n            (e) => {\r\n              state.error = new Error(e.reason.message);\r\n              state.isLoading = false;\r\n            },\r\n          );\r\n        } catch (e) {\r\n          if (!cancelled) {\r\n            state.error = e as Error;\r\n            state.isLoading = false;\r\n          }\r\n        }\r\n      })();\r\n\r\n      return () => {\r\n        cancelled = true;\r\n        engineRef?.closeAllDocuments?.().wait(() => {\r\n          engineRef?.destroy?.();\r\n          engineRef = null;\r\n        }, ignore);\r\n      };\r\n    });\r\n  }\r\n\r\n  // Return the reactive state object directly\r\n  return state;\r\n}\r\n"],"names":["PDF_ENGINE_CONTEXT_KEY","Symbol","getPdfEngineContext","context","getContext","Error","$","user_effect","value","engine","$$props","isLoading","error","svelte","setContext","contextValue","config","wasmUrl","DEFAULT_PDFIUM_WASM_URL","worker","logger","state","engineRef","window","cancelled","createPdfiumEngine","pdfEngine","$__namespace","set","initialize","wait","e","reason","message","closeAllDocuments","_b","call","_a","get","destroy","ignore"],"mappings":"wqBAUMA,EAAyBC,OAAO,oBAatB,SAAAC,IACR,MAAAC,EAAUC,aAA8CJ,eAE1DG,EACQ,MAAA,IAAAE,MAAM,+DAGX,OAAAF,CACT,sDChBEG,EAAAC,sBDAkCC,ICCb,CACjBC,OAAMC,EAAAD,OACNE,UAASD,EAAAC,UACTC,MAAKF,EAAAE,ODHTC,EAAAC,WAAWd,EAAwBQ,8FCMrC,oBCDO,WACL,MAAMC,OAAEA,EAAAG,MAAQA,GAAUV,IAE1B,GAAIU,EACI,MAAAA,EAGD,OAAAH,CACT,2BAtBO,WACL,MAAMM,EAAeb,IAErB,QAAqB,IAAjBa,EACI,MAAA,IAAIV,MAAM,4DAGX,OAAAU,CACT,mCCNgCC,iBACtBC,EAAUC,iCAAyBC,GAAS,EAAMC,OAAAA,GAAWJ,MAG/DK,WACJZ,OAAQ,KACRE,WAAW,EACXC,MAAO,OAGL,IAAAU,UAAqC,MA8ClC,MA5C6B,oBAAXC,QAGvBjB,EAAAC,kBACM,IAAAiB,GAAY,mBAGV,IACM,MAAAC,mBAAAA,GAAuBN,eACd,yDACA,4CAEXO,QAAkBD,EAAmBR,EAASG,GACxCO,EAAAC,IAAAN,EAAAI,GAAA,GAEZA,EAAUG,aAAaC,WAEnBT,EAAMZ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