{"version":3,"file":"createPathMatcher.mjs","names":[],"sources":["../../src/middleware-dir/createPathMatcher.ts"],"sourcesContent":["import { normalizePathname, stripTrailingSlashes } from './pathname';\n\nexport { normalizePathname };\n\nexport type PathConfig = {\n  [key: string]: string | { [key: string]: string };\n};\n\nexport type PathMatch = {\n  pathTemplate: string;\n  sharedPath: string;\n};\n\nexport type PathMatcherNode = {\n  staticSegments: Map<string, PathMatcherNode>;\n  dynamicSegment?: PathMatcherNode;\n  catchAllSegment?: PathMatcherNode;\n  optionalCatchAllSegment?: PathMatcherNode;\n  match?: PathMatch;\n};\n\nexport type PathMatcher = {\n  root: PathMatcherNode;\n  localizedRoot?: PathMatcherNode;\n  defaultLocaleRoot?: PathMatcherNode;\n};\n\nexport type DynamicSegmentType = 'dynamic' | 'catch-all' | 'optional-catch-all';\n\nconst DYNAMIC_SEGMENT_PATTERN = /^\\[[^.[\\]/][^\\]/]*\\]$/;\nconst CATCH_ALL_SEGMENT_PATTERN = /^\\[\\.\\.\\.[^\\]/]+\\]$/;\nconst OPTIONAL_CATCH_ALL_SEGMENT_PATTERN = /^\\[\\[\\.\\.\\.[^\\]/]+\\]\\]$/;\n\n/** Creates an empty node in the pathname matcher trie. */\nfunction createPathMatcherNode(): PathMatcherNode {\n  return { staticSegments: new Map() };\n}\n\n/** Identifies the supported Next.js dynamic segment syntax. */\nexport function getDynamicSegmentType(\n  segment: string\n): DynamicSegmentType | undefined {\n  if (OPTIONAL_CATCH_ALL_SEGMENT_PATTERN.test(segment)) {\n    return 'optional-catch-all';\n  }\n  if (CATCH_ALL_SEGMENT_PATTERN.test(segment)) return 'catch-all';\n  if (DYNAMIC_SEGMENT_PATTERN.test(segment)) return 'dynamic';\n  return undefined;\n}\n\n/** Splits a pathname without interpreting encoded literals as route syntax. */\nexport function getPathSegments(pathname: string): string[] {\n  if (pathname === '') return [];\n  const normalizedPathname = stripTrailingSlashes(pathname);\n  const pathnameWithoutLeadingSlash = normalizedPathname.startsWith('/')\n    ? normalizedPathname.slice(1)\n    : normalizedPathname;\n  return pathnameWithoutLeadingSlash.split('/');\n}\n\n/** Inserts a pathname template and its shared path into the matcher trie. */\nfunction insertPath(\n  matcher: PathMatcher,\n  pathname: string,\n  sharedPath: string\n) {\n  let node = matcher.root;\n\n  for (const segment of getPathSegments(pathname)) {\n    const segmentType = getDynamicSegmentType(segment);\n    if (segmentType === 'dynamic') {\n      node.dynamicSegment ||= createPathMatcherNode();\n      node = node.dynamicSegment;\n    } else if (segmentType === 'catch-all') {\n      node.catchAllSegment ||= createPathMatcherNode();\n      node = node.catchAllSegment;\n    } else if (segmentType === 'optional-catch-all') {\n      node.optionalCatchAllSegment ||= createPathMatcherNode();\n      node = node.optionalCatchAllSegment;\n    } else {\n      const staticSegment = normalizePathname(segment);\n      const staticNode =\n        node.staticSegments.get(staticSegment) || createPathMatcherNode();\n      node.staticSegments.set(staticSegment, staticNode);\n      node = staticNode;\n    }\n  }\n\n  // Match the previous map behavior when two parameter names describe the\n  // same path shape: the later entry wins.\n  node.match = { pathTemplate: pathname, sharedPath };\n}\n\n/** Builds a segment trie from pathname-to-shared-path entries. */\nexport function createPathMatcher(\n  pathEntries: ReadonlyArray<readonly [string, string]>\n): PathMatcher {\n  const matcher: PathMatcher = { root: createPathMatcherNode() };\n  for (const [pathname, sharedPath] of pathEntries) {\n    insertPath(matcher, pathname, sharedPath);\n  }\n  return matcher;\n}\n\n/** Creates matchers for localized paths and unprefixed default-locale paths. */\nexport function createPathToSharedPathMap(\n  pathConfig: PathConfig,\n  prefixDefaultLocale: boolean,\n  defaultLocale: string\n): {\n  pathToSharedPath: PathMatcher;\n  defaultLocalePaths: PathMatcher;\n} {\n  const pathEntries: Array<readonly [string, string]> = [];\n  const localizedEntries: Array<readonly [string, string]> = [];\n  const defaultLocaleEntries: Array<readonly [string, string]> = [];\n\n  for (const [sharedPath, localizedPaths] of Object.entries(pathConfig)) {\n    pathEntries.push([sharedPath, sharedPath]);\n\n    if (typeof localizedPaths === 'object') {\n      for (const [locale, localizedPath] of Object.entries(localizedPaths)) {\n        localizedEntries.push([`/${locale}${localizedPath}`, sharedPath]);\n        if (!prefixDefaultLocale && locale === defaultLocale) {\n          defaultLocaleEntries.push([localizedPath, sharedPath]);\n        }\n      }\n    }\n  }\n\n  const defaultLocalePaths = createPathMatcher(defaultLocaleEntries);\n  return {\n    pathToSharedPath: {\n      ...createPathMatcher(pathEntries),\n      localizedRoot: createPathMatcher(localizedEntries).root,\n      defaultLocaleRoot: defaultLocalePaths.root,\n    },\n    defaultLocalePaths,\n  };\n}\n"],"mappings":";;AA6BA,MAAM,0BAA0B;AAChC,MAAM,4BAA4B;AAClC,MAAM,qCAAqC;;AAG3C,SAAS,wBAAyC;AAChD,QAAO,EAAE,gCAAgB,IAAI,KAAK,EAAE;;;AAItC,SAAgB,sBACd,SACgC;AAChC,KAAI,mCAAmC,KAAK,QAAQ,CAClD,QAAO;AAET,KAAI,0BAA0B,KAAK,QAAQ,CAAE,QAAO;AACpD,KAAI,wBAAwB,KAAK,QAAQ,CAAE,QAAO;;;AAKpD,SAAgB,gBAAgB,UAA4B;AAC1D,KAAI,aAAa,GAAI,QAAO,EAAE;CAC9B,MAAM,qBAAqB,qBAAqB,SAAS;AAIzD,SAHoC,mBAAmB,WAAW,IAAI,GAClE,mBAAmB,MAAM,EAAE,GAC3B,oBAC+B,MAAM,IAAI;;;AAI/C,SAAS,WACP,SACA,UACA,YACA;CACA,IAAI,OAAO,QAAQ;AAEnB,MAAK,MAAM,WAAW,gBAAgB,SAAS,EAAE;EAC/C,MAAM,cAAc,sBAAsB,QAAQ;AAClD,MAAI,gBAAgB,WAAW;AAC7B,QAAK,mBAAmB,uBAAuB;AAC/C,UAAO,KAAK;aACH,gBAAgB,aAAa;AACtC,QAAK,oBAAoB,uBAAuB;AAChD,UAAO,KAAK;aACH,gBAAgB,sBAAsB;AAC/C,QAAK,4BAA4B,uBAAuB;AACxD,UAAO,KAAK;SACP;GACL,MAAM,gBAAgB,kBAAkB,QAAQ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