{"version":3,"file":"matchPath.mjs","names":[],"sources":["../../src/middleware-dir/matchPath.ts"],"sourcesContent":["import {\n  getDynamicSegmentType,\n  getPathSegments,\n  normalizePathname,\n  type PathMatch,\n  type PathMatcher,\n  type PathMatcherNode,\n} from './createPathMatcher';\n\nexport type SharedPathMatch = PathMatch & {\n  matchedPathname: string;\n};\n\n/** Recursively matches segments in static, dynamic, then catch-all order. */\nfunction matchPathSegments(\n  node: PathMatcherNode,\n  pathSegments: string[],\n  segmentIndex: number\n): PathMatch | undefined {\n  // Base case: we're at the end of the segments\n  if (segmentIndex === pathSegments.length) {\n    if (node.match !== undefined) return node.match;\n    // Recursive case 4: edge case - see below\n    if (node.optionalCatchAllSegment) {\n      return matchPathSegments(\n        node.optionalCatchAllSegment,\n        pathSegments,\n        segmentIndex\n      );\n    }\n    return undefined;\n  }\n\n  // Get the segment at the current index\n  const segment = pathSegments[segmentIndex];\n\n  // Recursive case 1: match a static segment\n  const staticNode = node.staticSegments.get(segment);\n  if (staticNode) {\n    const match = matchPathSegments(staticNode, pathSegments, segmentIndex + 1);\n    if (match !== undefined) return match;\n  }\n\n  // Recursive case 2: match a dynamic segement\n  if (segment && node.dynamicSegment) {\n    const match = matchPathSegments(\n      node.dynamicSegment,\n      pathSegments,\n      segmentIndex + 1\n    );\n    if (match !== undefined) return match;\n  }\n\n  // Recursive case 3: match a catch-all segment\n  if (node.catchAllSegment) {\n    /**\n     * Technically, this loop isn't necessary. This would handle cases\n     * where we have static segments after the catch-all segment. Which\n     * does not occur in Next.js routing. (`/docs/[...slug]/authors`)\n     *\n     * That means this loop only iterates once for a catch-all segment,\n     * so costs only scale linearlly which is acceptable.\n     */\n    for (\n      let nextSegmentIndex = pathSegments.length;\n      nextSegmentIndex > segmentIndex;\n      nextSegmentIndex--\n    ) {\n      if (\n        !pathSegments\n          .slice(segmentIndex, nextSegmentIndex)\n          .some((segment) => segment.length > 0)\n      ) {\n        continue;\n      }\n      const match = matchPathSegments(\n        node.catchAllSegment,\n        pathSegments,\n        nextSegmentIndex\n      );\n      if (match !== undefined) return match;\n    }\n  }\n\n  // Recursive case 4: match an optional catch-all segment\n  if (node.optionalCatchAllSegment) {\n    /**\n     * See note in recursive case 3.\n     */\n    for (\n      let nextSegmentIndex = pathSegments.length;\n      nextSegmentIndex >= segmentIndex;\n      nextSegmentIndex--\n    ) {\n      const match = matchPathSegments(\n        node.optionalCatchAllSegment,\n        pathSegments,\n        nextSegmentIndex\n      );\n      if (match !== undefined) return match;\n    }\n  }\n\n  return undefined;\n}\n\n/** Looks up the route entry associated with a concrete pathname. */\nfunction matchPath(pathname: string, matcher: PathMatcher) {\n  return matchPathSegments(\n    matcher.root,\n    getPathSegments(pathname).map(normalizePathname),\n    0\n  );\n}\n\n/** Ranks route segments in the same order as trie traversal. */\nfunction segmentPriority(segment: string | undefined): number {\n  if (segment === undefined) return -1;\n  switch (getDynamicSegmentType(segment)) {\n    case 'dynamic':\n      return 1;\n    case 'catch-all':\n      return 2;\n    case 'optional-catch-all':\n      return 3;\n    default:\n      return 0;\n  }\n}\n\n/** Compares shared and localized candidates without counting the locale prefix. */\nfunction isSharedMoreSpecific(\n  shared: PathMatch,\n  localized: PathMatch,\n  pathnameLocale: string | undefined\n): boolean {\n  const sharedSegments = getPathSegments(shared.pathTemplate);\n  const localizedSegments = getPathSegments(localized.pathTemplate);\n  if (pathnameLocale) localizedSegments.shift();\n  for (\n    let index = 0;\n    index < Math.max(sharedSegments.length, localizedSegments.length);\n    index++\n  ) {\n    const difference =\n      segmentPriority(sharedSegments[index]) -\n      segmentPriority(localizedSegments[index]);\n    if (difference !== 0) return difference < 0;\n  }\n  return false;\n}\n\n/** Gets the shared route and source template matching a concrete pathname. */\nexport function getSharedPath(\n  standardizedPathname: string,\n  pathToSharedPath: PathMatcher,\n  pathnameLocale: string | undefined\n): SharedPathMatch | undefined {\n  if (pathToSharedPath.localizedRoot) {\n    // Is this a \"shared path\" (e.g. `/home`)?\n    const pathnameWithoutLocale = pathnameLocale\n      ? standardizedPathname.replace(/^\\/[^/]+/, '') || '/'\n      : standardizedPathname;\n    const sharedMatch = matchPath(pathnameWithoutLocale, pathToSharedPath);\n\n    // Is this a \"localized path\" (e.g. `/inicio`)?\n    const localizedRoot = pathnameLocale\n      ? pathToSharedPath.localizedRoot\n      : pathToSharedPath.defaultLocaleRoot;\n    const localizedMatch = localizedRoot\n      ? matchPath(standardizedPathname, { root: localizedRoot })\n      : undefined;\n\n    if (\n      sharedMatch &&\n      (!localizedMatch ||\n        isSharedMoreSpecific(sharedMatch, localizedMatch, pathnameLocale))\n    ) {\n      return { ...sharedMatch, matchedPathname: pathnameWithoutLocale };\n    }\n    if (localizedMatch) {\n      return { ...localizedMatch, matchedPathname: standardizedPathname };\n    }\n    return undefined;\n  }\n\n  const directMatch = matchPath(standardizedPathname, pathToSharedPath);\n  if (directMatch !== undefined) {\n    return { ...directMatch, matchedPathname: standardizedPathname };\n  }\n\n  if (pathnameLocale) {\n    const pathnameWithoutLocale =\n      standardizedPathname.replace(/^\\/[^/]+/, '') || '/';\n    const unprefixedMatch = matchPath(pathnameWithoutLocale, pathToSharedPath);\n    if (unprefixedMatch !== undefined) {\n      return { ...unprefixedMatch, matchedPathname: pathnameWithoutLocale };\n    }\n  }\n  return undefined;\n}\n"],"mappings":";;;;AAcA,SAAS,kBACP,MACA,cACA,cACuB;AAEvB,KAAI,iBAAiB,aAAa,QAAQ;AACxC,MAAI,KAAK,UAAU,KAAA,EAAW,QAAO,KAAK;AAE1C,MAAI,KAAK,wBACP,QAAO,kBACL,KAAK,yBACL,cACA,aACD;AAEH;;CAIF,MAAM,UAAU,aAAa;CAG7B,MAAM,aAAa,KAAK,eAAe,IAAI,QAAQ;AACnD,KAAI,YAAY;EACd,MAAM,QAAQ,kBAAkB,YAAY,cAAc,eAAe,EAAE;AAC3E,MAAI,UAAU,KAAA,EAAW,QAAO;;AAIlC,KAAI,WAAW,KAAK,gBAAgB;EAClC,MAAM,QAAQ,kBACZ,KAAK,gBACL,c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