{"version":3,"file":"index.cjs","sources":["../../src/svelte/hooks/use-search.svelte.ts","../../src/svelte/components/SearchLayer.svelte"],"sourcesContent":["import { useCapability, usePlugin } from '@embedpdf/core/svelte';\nimport {\n  SearchPlugin,\n  SearchDocumentState,\n  SearchScope,\n  initialSearchDocumentState,\n} from '@embedpdf/plugin-search';\n\nexport const useSearchPlugin = () => usePlugin<SearchPlugin>(SearchPlugin.id);\nexport const useSearchCapability = () => useCapability<SearchPlugin>(SearchPlugin.id);\n\n// Define the return type explicitly to maintain type safety\ninterface UseSearchReturn {\n  provides: SearchScope | null;\n  state: SearchDocumentState;\n}\n\n/**\n * Hook for search state for a specific document\n * @param getDocumentId Function that returns the document ID\n */\nexport const useSearch = (getDocumentId: () => string | null): UseSearchReturn => {\n  const capability = useSearchCapability();\n\n  let searchState = $state<SearchDocumentState>(initialSearchDocumentState);\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      searchState = initialSearchDocumentState;\n      return;\n    }\n\n    const scope = provides.forDocument(docId);\n\n    // Set initial state\n    searchState = scope.getState();\n\n    // Subscribe to changes\n    return scope.onStateChange((state) => {\n      searchState = state;\n    });\n  });\n\n  return {\n    get provides() {\n      return scopedProvides;\n    },\n    get state() {\n      return searchState;\n    },\n  };\n};\n","<script lang=\"ts\">\n  import type { SearchResultState } from '@embedpdf/plugin-search';\n  import { useDocumentState } from '@embedpdf/core/svelte';\n  import { useSearchCapability } from '../hooks';\n  import type { HTMLAttributes } from 'svelte/elements';\n\n  interface SearchLayerProps extends HTMLAttributes<HTMLDivElement> {\n    documentId: string;\n    pageIndex: number;\n    scale?: number;\n    highlightColor?: string;\n    activeHighlightColor?: string;\n  }\n\n  let {\n    documentId,\n    pageIndex,\n    scale: scaleOverride,\n    highlightColor = '#FFFF00',\n    activeHighlightColor = '#FFBF00',\n    ...divProps\n  }: SearchLayerProps = $props();\n\n  const searchCapability = useSearchCapability();\n  const documentState = useDocumentState(() => documentId);\n\n  let searchResultState = $state<SearchResultState | null>(null);\n\n  const scope = $derived(searchCapability.provides?.forDocument(documentId) ?? null);\n\n  const actualScale = $derived(\n    scaleOverride !== undefined ? scaleOverride : (documentState.current?.scale ?? 1),\n  );\n\n  $effect(() => {\n    if (!scope) {\n      searchResultState = null;\n      return;\n    }\n\n    // Set initial state\n    const currentState = scope.getState();\n    searchResultState = {\n      results: currentState.results,\n      activeResultIndex: currentState.activeResultIndex,\n      showAllResults: currentState.showAllResults,\n      active: currentState.active,\n    };\n\n    // Subscribe to changes\n    return scope.onSearchResultStateChange((state) => {\n      searchResultState = state;\n    });\n  });\n\n  // Filter results for current page while preserving original indices\n  const pageResults = $derived(\n    searchResultState\n      ? searchResultState.results\n          .map((result, originalIndex) => ({ result, originalIndex }))\n          .filter(({ result }) => result.pageIndex === pageIndex)\n      : [],\n  );\n\n  // Decide which results to show\n  const resultsToShow = $derived(\n    searchResultState\n      ? pageResults.filter(\n          ({ originalIndex }) =>\n            searchResultState!.showAllResults ||\n            originalIndex === searchResultState!.activeResultIndex,\n        )\n      : [],\n  );\n</script>\n\n{#if searchResultState && searchResultState.active}\n  <div style:pointer-events=\"none\" {...divProps}>\n    {#each resultsToShow as { result, originalIndex }, idx (`result-${idx}`)}\n      {#each result.rects as rect, rectIdx (`rect-${idx}-${rectIdx}`)}\n        <div\n          style:position=\"absolute\"\n          style:top=\"{rect.origin.y * actualScale}px\"\n          style:left=\"{rect.origin.x * actualScale}px\"\n          style:width=\"{rect.size.width * actualScale}px\"\n          style:height=\"{rect.size.height * actualScale}px\"\n          style:background-color={originalIndex === searchResultState.activeResultIndex\n            ? activeHighlightColor\n            : highlightColor}\n          style:mix-blend-mode=\"multiply\"\n          style:transform=\"scale(1.02)\"\n          style:transform-origin=\"center\"\n          style:transition=\"opacity .3s ease-in-out\"\n          style:opacity=\"1\"\n        ></div>\n      {/each}\n    {/each}\n  </div>\n{/if}\n"],"names":["useSearchCapability","useCapability","SearchPlugin","id","highlightColor","activeHighlightColor","divProps","$","rest_props","$$props","searchCapability","documentState","useDocumentState","documentId","searchResultState","state","scope","_a","provides","forDocument","actualScale","derived","scale","current","user_effect","get","set","currentState","getState","results","activeResultIndex","showAllResults","active","onSearchResultStateChange","pageResults","map","result","originalIndex","filter","pageIndex","resultsToShow","div","root_1","attribute_effect","STYLE","each","idx","$$anchor","$$item","rects","rect","rectIdx","div_1","root_3","styles","top","origin","y","left","x","width","size","height","$$render","consequent","getDocumentId","capability","searchState","initialSearchDocumentState","scopedProvides","docId","onStateChange","usePlugin"],"mappings":"sgBASaA,EAAA,IAA4BC,gBAA4BC,EAAAA,aAAaC,iHCS9E,IAAAC,8BAAiB,WACjBC,oCAAuB,WACpBC,EAAOC,EAAAC,WAAAC,EAAA,4GAGN,MAAAC,EAAmBV,IACnBW,EAAgBC,EAAAA,iBAAgB,IAAAH,EAAAI,gBAElCC,EAAoBP,EAAAQ,MAAiC,MAEnD,MAAAC,uBAAiB,OAAA,OAAAC,EAAAP,EAAiBQ,eAAjB,EAAAD,EAA2BE,4BAA2B,OAEvEC,EAAWb,EAAAc,QAAA,WAAA,YACG,IADHZ,EAAAa,MACYb,EAAAa,OAAoB,OAAAL,EAAAN,EAAcY,kBAASD,QAAS,IAGjFf,EAAAiB,YAAO,KACA,IAAAjB,EAAAkB,IAAAT,eACHT,EAAAmB,IAAAZ,EAAoB,YAKhBa,EAAYpB,EAAAkB,IAAGT,GAAMY,WASpB,aARPd,GACEe,QAASF,EAAaE,QACtBC,kBAAmBH,EAAaG,kBAChCC,eAAgBJ,EAAaI,eAC7BC,OAAQL,EAAaK,YAIhBzB,EAAAkB,IAAAT,GAAMiB,0BAA2BlB,IACtCR,EAAAmB,IAAAZ,EAAoBC,GAAK,OAKvB,MAAAmB,sBACJpB,GACIP,EAAAkB,IAAAX,G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