{"version":3,"file":"useRouterState.cjs","names":[],"sources":["../../src/useRouterState.tsx"],"sourcesContent":["'use client'\n\nimport { useStore } from '@tanstack/react-store'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { useRouter } from './useRouter'\nimport { useStructuralSharing } from './useMatch'\nimport type {\n  AnyRouter,\n  RegisteredRouter,\n  RouterState,\n} from '@tanstack/router-core'\nimport type {\n  StructuralSharingOption,\n  ValidateSelected,\n} from './structuralSharing'\n\nexport type UseRouterStateOptions<\n  TRouter extends AnyRouter,\n  TSelected,\n  TStructuralSharing,\n> = {\n  router?: TRouter\n  select?: (\n    state: RouterState<TRouter['routeTree']>,\n  ) => ValidateSelected<TRouter, TSelected, TStructuralSharing>\n} & StructuralSharingOption<TRouter, TSelected, TStructuralSharing>\n\nexport type UseRouterStateResult<\n  TRouter extends AnyRouter,\n  TSelected,\n> = unknown extends TSelected ? RouterState<TRouter['routeTree']> : TSelected\n\n/**\n * Subscribe to the router's state store with optional selection and\n * structural sharing for render optimization.\n *\n * Options:\n * - `select`: Project the full router state to a derived slice\n * - `structuralSharing`: Replace-equal semantics for stable references\n * - `router`: Read state from a specific router instance instead of context\n *\n * @returns The selected router state (or the full state by default).\n * @link https://tanstack.com/router/latest/docs/framework/react/api/router/useRouterStateHook\n */\nexport function useRouterState<\n  TRouter extends AnyRouter = RegisteredRouter,\n  TSelected = unknown,\n  TStructuralSharing extends boolean = boolean,\n>(\n  opts?: UseRouterStateOptions<TRouter, TSelected, TStructuralSharing>,\n): UseRouterStateResult<TRouter, TSelected> {\n  const contextRouter = useRouter<TRouter>({\n    warn: opts?.router === undefined,\n  })\n  const router = opts?.router || contextRouter\n\n  // During SSR we render exactly once and do not need reactivity.\n  // Avoid subscribing to the store (and any structural sharing work) on the server.\n  const _isServer = isServer ?? router.isServer\n  if (_isServer) {\n    const state = router.stores.__store.get() as RouterState<\n      TRouter['routeTree']\n    >\n    return (opts?.select ? opts.select(state) : state) as UseRouterStateResult<\n      TRouter,\n      TSelected\n    >\n  }\n\n  // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n  return useStore(\n    router.stores.__store,\n    // eslint-disable-next-line react-hooks/rules-of-hooks -- condition is static\n    useStructuralSharing(opts, router),\n  ) as UseRouterStateResult<TRouter, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA4CA,SAAgB,eAKd,MAC0C;CAC1C,MAAM,gBAAgB,kBAAA,UAAmB,EACvC,MAAM,MAAM,WAAW,KAAA,EACzB,CAAC;CACD,MAAM,SAAS,MAAM,UAAU;CAK/B,IADkB,+BAAA,YAAY,OAAO,UACtB;EACb,MAAM,QAAQ,OAAO,OAAO,QAAQ,IAAI;EAGxC,OAAQ,MAAM,SAAS,KAAK,OAAO,KAAK,IAAI;CAI9C;CAGA,QAAA,GAAA,sBAAA,UACE,OAAO,OAAO,SAEd,iBAAA,qBAAqB,MAAM,MAAM,CACnC;AACF"}