{"version":3,"file":"index.cjs","sources":["../../src/svelte/hooks/use-rotate.svelte.ts","../../src/svelte/components/Rotate.svelte"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/svelte';\nimport { Rotation } from '@embedpdf/models';\nimport { RotatePlugin, initialDocumentState, RotateScope } from '@embedpdf/plugin-rotate';\n\n/**\n * Hook to get the raw rotate plugin instance.\n */\nexport const useRotatePlugin = () => usePlugin<RotatePlugin>(RotatePlugin.id);\n\n/**\n * Hook to get the rotate plugin's capability API.\n * This provides methods for rotating the document.\n */\nexport const useRotateCapability = () => useCapability<RotatePlugin>(RotatePlugin.id);\n\n// Define the return type explicitly to maintain type safety\ninterface UseRotateReturn {\n  provides: RotateScope | null;\n  rotation: Rotation;\n}\n\n/**\n * Hook that provides reactive rotation state and methods for a specific document.\n * @param getDocumentId Function that returns the document ID\n */\nexport const useRotate = (getDocumentId: () => string | null): UseRotateReturn => {\n  const capability = useRotateCapability();\n\n  let rotation = $state<Rotation>(initialDocumentState.rotation);\n\n  // Reactive documentId\n  const documentId = $derived(getDocumentId());\n\n  // Scoped capability for current docId\n  const scopedProvides = $derived(\n    capability.provides && documentId ? capability.provides.forDocument(documentId) : null,\n  );\n\n  $effect(() => {\n    const provides = capability.provides;\n    const docId = documentId;\n\n    if (!provides || !docId) {\n      rotation = initialDocumentState.rotation;\n      return;\n    }\n\n    const scope = provides.forDocument(docId);\n\n    // Get initial state\n    rotation = scope.getRotation();\n\n    // Subscribe to rotation changes for this document\n    return scope.onRotateChange((newRotation) => {\n      rotation = newRotation;\n    });\n  });\n\n  return {\n    get provides() {\n      return scopedProvides;\n    },\n    get rotation() {\n      return rotation;\n    },\n  };\n};\n","<script lang=\"ts\">\n  import type { Rotation } from '@embedpdf/models';\n  import type { Snippet } from 'svelte';\n  import type { HTMLAttributes } from 'svelte/elements';\n  import { useDocumentState } from '@embedpdf/core/svelte';\n  import { useRotatePlugin } from '../hooks';\n\n  type RotateProps = HTMLAttributes<HTMLDivElement> & {\n    documentId: string;\n    pageIndex: number;\n    rotation?: Rotation;\n    scale?: number;\n    children?: Snippet;\n    class?: string;\n    style?: string;\n  };\n\n  let {\n    documentId,\n    pageIndex,\n    rotation: rotationOverride,\n    scale: scaleOverride,\n    children,\n    class: propsClass,\n    style: propsStyle,\n    ...restProps\n  }: RotateProps = $props();\n\n  const rotatePlugin = useRotatePlugin();\n  const documentState = useDocumentState(() => documentId);\n\n  const page = $derived(documentState.current?.document?.pages?.[pageIndex]);\n  const width = $derived(page?.size?.width ?? 0);\n  const height = $derived(page?.size?.height ?? 0);\n\n  // If override is provided, use it directly (consistent with other layer components)\n  // Otherwise, combine page intrinsic rotation with document rotation\n  const pageRotation = $derived(page?.rotation ?? 0);\n  const docRotation = $derived(documentState.current?.rotation ?? 0);\n  const rotation = $derived(\n    rotationOverride !== undefined ? rotationOverride : (pageRotation + docRotation) % 4,\n  );\n\n  const scale = $derived(\n    scaleOverride !== undefined ? scaleOverride : (documentState.current?.scale ?? 1),\n  );\n\n  const matrix = $derived(\n    rotatePlugin.plugin\n      ? rotatePlugin.plugin.getMatrixAsString({\n          width: width * scale,\n          height: height * scale,\n          rotation: rotation,\n        })\n      : 'matrix(1, 0, 0, 1, 0, 0)',\n  );\n</script>\n\n{#if page}\n  <div\n    class={propsClass}\n    style:position=\"absolute\"\n    style:transform-origin=\"0 0\"\n    style:transform={matrix}\n    style={propsStyle}\n    {...restProps}\n  >\n    {@render children?.()}\n  </div>\n{/if}\n"],"names":["useRotatePlugin","usePlugin","RotatePlugin","id","useRotateCapability","useCapability","restProps","$","rest_props","$$props","rotatePlugin","documentState","useDocumentState","documentId","page","_c","_b","_a","current","document","pages","pageIndex","width","size","height","pageRotation","derived","get","rotation","docRotation","scale","matrix","plugin","getMatrixAsString","div","root_1","consequent","getDocumentId","capability","state","proxy","initialDocumentState","scopedProvides","provides","forDocument","user_effect","docId","scope","getRotation","onRotateChange","newRotation","set"],"mappings":"sgBAOaA,EAAA,IAAwBC,YAAwBC,EAAAA,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