{"version":3,"sources":["../src/react/router/virtual.ts","../src/react/router/next.ts","../src/react/utils/path-inference/next.tsx","../src/react/utils/path-inference/utils.ts","../src/react/router/index.ts"],"sourcesContent":["'use client';\n\nimport type { ClerkHostRouter } from '@clerk/shared/router';\nimport { useSyncExternalStore } from 'react';\n\nconst DUMMY_ORIGIN = 'https://clerk.dummy';\n\n// TODO: introduce history stack?\nclass VirtualRouter implements ClerkHostRouter {\n  readonly name = 'VirtualRouter';\n  readonly mode = 'virtual';\n\n  #url: URL;\n  #listeners: Set<(url: URL) => void> = new Set();\n\n  constructor(path?: string) {\n    const origin = typeof window === 'undefined' ? DUMMY_ORIGIN : window.location.origin;\n\n    this.#url = new URL(path ?? '/', origin);\n  }\n\n  push(path: string) {\n    const newUrl = new URL(this.#url.toString());\n    newUrl.pathname = path;\n\n    this.#url = newUrl;\n    this.emit();\n  }\n\n  replace(path: string) {\n    this.push(path);\n  }\n\n  shallowPush(path: string) {\n    this.push(path);\n  }\n\n  pathname() {\n    return this.#url.pathname;\n  }\n\n  searchParams() {\n    return this.#url.searchParams;\n  }\n\n  subscribe(listener: () => void) {\n    this.#listeners.add(listener);\n\n    return () => this.#listeners.delete(listener);\n  }\n\n  emit() {\n    this.#listeners.forEach(listener => listener(this.#url));\n  }\n\n  getSnapshot() {\n    return this.#url;\n  }\n}\n\nconst virtualRouter = new VirtualRouter('/');\n\nexport const useVirtualRouter = (): ClerkHostRouter => {\n  const url = useSyncExternalStore(\n    virtualRouter.subscribe.bind(virtualRouter),\n    virtualRouter.getSnapshot.bind(virtualRouter),\n  );\n\n  return {\n    mode: virtualRouter.mode,\n    name: virtualRouter.name,\n    pathname: () => url.pathname,\n    push: virtualRouter.push.bind(virtualRouter),\n    replace: virtualRouter.replace.bind(virtualRouter),\n    searchParams: () => url.searchParams,\n    shallowPush: virtualRouter.shallowPush.bind(virtualRouter),\n  };\n};\n","import type { ClerkHostRouter } from '@clerk/types';\nimport { usePathname, useRouter, useSearchParams } from 'next/navigation';\n\nimport { NEXT_WINDOW_HISTORY_SUPPORT_VERSION } from '~/internals/constants';\n\nimport { usePathnameWithoutCatchAll } from '../utils/path-inference/next';\n\n/**\n * Clerk Elements router integration with Next.js's router.\n */\nexport const useNextRouter = (): ClerkHostRouter => {\n  const router = useRouter();\n  const pathname = usePathname();\n  const searchParams = useSearchParams();\n  const inferredBasePath = usePathnameWithoutCatchAll();\n\n  // The window.history APIs seem to prevent Next.js from triggering a full page re-render, allowing us to\n  // preserve internal state between steps.\n  const canUseWindowHistoryAPIs =\n    typeof window !== 'undefined' && window.next && window.next.version >= NEXT_WINDOW_HISTORY_SUPPORT_VERSION;\n\n  return {\n    mode: 'path',\n    name: 'NextRouter',\n    push: (path: string) => router.push(path),\n    replace: (path: string) =>\n      canUseWindowHistoryAPIs ? window.history.replaceState(null, '', path) : router.replace(path),\n    shallowPush: (path: string) =>\n      canUseWindowHistoryAPIs ? window.history.pushState(null, '', path) : router.push(path, {}),\n    pathname: () => pathname,\n    searchParams: () => searchParams,\n    inferredBasePath: () => inferredBasePath,\n  };\n};\n","import { useRouter } from 'next/compat/router';\nimport { useParams, usePathname } from 'next/navigation';\nimport React from 'react';\n\nimport { removeOptionalCatchAllSegment } from './utils';\n\n// Adapted from packages/nextjs/src/client-boundary/hooks/usePathnameWithoutCatchAll.tsx\n\n/**\n * This hook grabs the current pathname (both in pages and app router) and removes any (optional) catch all segments.\n * @example\n * 1. /user/[id]/profile/[[...rest]]/page.tsx\n * 2. /user/123/profile/security\n * 3. /user/123/profile\n * @returns The pathname without any catch all segments\n */\nexport const usePathnameWithoutCatchAll = () => {\n  const pathRef = React.useRef<string>();\n\n  /**\n   * The compat version of useRouter returns null instead of throwing an error when used inside App router.\n   * Use it to detect if the component is used inside pages or app router\n   */\n  const pagesRouter = useRouter();\n\n  if (pagesRouter) {\n    if (pathRef.current) {\n      return pathRef.current;\n    } else {\n      // The optional catch all route is included in the pathname in pages router. It starts with [[... and we can just remove it\n      pathRef.current = removeOptionalCatchAllSegment(pagesRouter.pathname);\n      return pathRef.current;\n    }\n  }\n\n  /**\n   * Get the pathname that includes any named or catch all params.\n   * @example\n   * /user/[id]/profile/[[...rest]]/page.tsx\n   *\n   * This filesystem route could give us the following pathname:\n   * /user/123/profile/security\n   * if the user navigates to the security section of the user profile\n   */\n  const pathname = usePathname() || '';\n  const pathParts = pathname.split('/').filter(Boolean);\n  /**\n   * For /user/[id]/profile/[[...rest]]/page.tsx useParams will return { id: '123', rest: ['security'] }.\n   * So catch all params are always arrays\n   */\n  const catchAllParams = Object.values(useParams() || {})\n    .filter(v => Array.isArray(v))\n    .flat(Infinity);\n  if (pathRef.current) {\n    return pathRef.current;\n  } else {\n    // /user/123/profile/security should be transformed to /user/123/profile\n    pathRef.current = `/${pathParts.slice(0, pathParts.length - catchAllParams.length).join('/')}`;\n    return pathRef.current;\n  }\n};\n","export function removeOptionalCatchAllSegment(pathname: string) {\n  return pathname.replace(/\\/\\[\\[\\.\\.\\..*/, '');\n}\n","export { Route, Router, useClerkRouter, ClerkHostRouterContext } from '@clerk/shared/router';\nexport { useVirtualRouter } from './virtual';\nexport { useNextRouter } from './next';\n"],"mappings":";;;;;;;;AAGA,SAAS,4BAA4B;AAErC,IAAM,eAAe;AALrB;AAQA,IAAM,gBAAN,MAA+C;AAAA,EAO7C,YAAY,MAAe;AAN3B,SAAS,OAAO;AAChB,SAAS,OAAO;AAEhB;AACA,mCAAsC,oBAAI,IAAI;AAG5C,UAAM,SAAS,OAAO,WAAW,cAAc,eAAe,OAAO,SAAS;AAE9E,uBAAK,MAAO,IAAI,IAAI,sBAAQ,KAAK,MAAM;AAAA,EACzC;AAAA,EAEA,KAAK,MAAc;AACjB,UAAM,SAAS,IAAI,IAAI,mBAAK,MAAK,SAAS,CAAC;AAC3C,WAAO,WAAW;AAElB,uBAAK,MAAO;AACZ,SAAK,KAAK;AAAA,EACZ;AAAA,EAEA,QAAQ,MAAc;AACpB,SAAK,KAAK,IAAI;AAAA,EAChB;AAAA,EAEA,Y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