{"version":3,"sources":["../../../../src/build/segment-config/app/app-segments.ts"],"sourcesContent":["import type { LoadComponentsReturnType } from '../../../server/load-components'\nimport type { Params } from '../../../server/request/params'\nimport type {\n  AppPageRouteModule,\n  AppPageModule,\n} from '../../../server/route-modules/app-page/module.compiled'\nimport type {\n  AppRouteRouteModule,\n  AppRouteModule,\n} from '../../../server/route-modules/app-route/module.compiled'\nimport {\n  type AppSegmentConfig,\n  parseAppSegmentConfig,\n} from './app-segment-config'\n\nimport { InvariantError } from '../../../shared/lib/invariant-error'\nimport {\n  isAppRouteRouteModule,\n  isAppPageRouteModule,\n} from '../../../server/route-modules/checks'\nimport { isClientReference } from '../../../lib/client-and-server-references'\nimport { getSegmentParam } from '../../../server/app-render/get-segment-param'\nimport {\n  getLayoutOrPageModule,\n  type LoaderTree,\n} from '../../../server/lib/app-dir-module'\nimport { PAGE_SEGMENT_KEY } from '../../../shared/lib/segment'\n\ntype GenerateStaticParams = (options: { params?: Params }) => Promise<Params[]>\n\n/**\n * Parses the app config and attaches it to the segment.\n */\nfunction attach(segment: AppSegment, userland: unknown, route: string) {\n  // If the userland is not an object, then we can't do anything with it.\n  if (typeof userland !== 'object' || userland === null) {\n    return\n  }\n\n  // Try to parse the application configuration.\n  const config = parseAppSegmentConfig(userland, route)\n\n  // If there was any keys on the config, then attach it to the segment.\n  if (Object.keys(config).length > 0) {\n    segment.config = config\n  }\n\n  if (\n    'generateStaticParams' in userland &&\n    typeof userland.generateStaticParams === 'function'\n  ) {\n    segment.generateStaticParams =\n      userland.generateStaticParams as GenerateStaticParams\n\n    // Validate that `generateStaticParams` makes sense in this context.\n    if (segment.config?.runtime === 'edge') {\n      throw new Error(\n        'Edge runtime is not supported with `generateStaticParams`.'\n      )\n    }\n  }\n}\n\nexport type AppSegment = {\n  name: string\n  param: string | undefined\n  filePath: string | undefined\n  config: AppSegmentConfig | undefined\n  isDynamicSegment: boolean\n  generateStaticParams: GenerateStaticParams | undefined\n}\n\n/**\n * Walks the loader tree and collects the generate parameters for each segment.\n *\n * @param routeModule the app page route module\n * @returns the segments for the app page route module\n */\nasync function collectAppPageSegments(routeModule: AppPageRouteModule) {\n  // We keep track of unique segments, since with parallel routes, it's possible\n  // to see the same segment multiple times.\n  const uniqueSegments = new Map<string, AppSegment>()\n\n  // Queue will store tuples of [loaderTree, currentSegments]\n  type QueueItem = [LoaderTree, AppSegment[]]\n  const queue: QueueItem[] = [[routeModule.userland.loaderTree, []]]\n\n  while (queue.length > 0) {\n    const [loaderTree, currentSegments] = queue.shift()!\n    const [name, parallelRoutes] = loaderTree\n\n    // Process current node\n    const { mod: userland, filePath } = await getLayoutOrPageModule(loaderTree)\n    const isClientComponent = userland && isClientReference(userland)\n\n    const param = getSegmentParam(name)?.param\n\n    const segment: AppSegment = {\n      name,\n      param,\n      filePath,\n      config: undefined,\n      isDynamicSegment: !!param,\n      generateStaticParams: undefined,\n    }\n\n    // Only server components can have app segment configurations\n    if (!isClientComponent) {\n      attach(segment, userland, routeModule.definition.pathname)\n    }\n\n    // Create a unique key for the segment\n    const segmentKey = getSegmentKey(segment)\n    if (!uniqueSegments.has(segmentKey)) {\n      uniqueSegments.set(segmentKey, segment)\n    }\n\n    const updatedSegments = [...currentSegments, segment]\n\n    // If this is a page segment, we've reached a leaf node\n    if (name === PAGE_SEGMENT_KEY) {\n      // Add all segments in the current path\n      updatedSegments.forEach((seg) => {\n        const key = getSegmentKey(seg)\n        uniqueSegments.set(key, seg)\n      })\n    }\n\n    // Add all parallel routes to the queue\n    for (const parallelRouteKey in parallelRoutes) {\n      const parallelRoute = parallelRoutes[parallelRouteKey]\n      queue.push([parallelRoute, updatedSegments])\n    }\n  }\n\n  return Array.from(uniqueSegments.values())\n}\n\nfunction getSegmentKey(segment: AppSegment) {\n  return `${segment.name}-${segment.filePath ?? ''}-${segment.param ?? ''}`\n}\n\n/**\n * Collects the segments for a given app route module.\n *\n * @param routeModule the app route module\n * @returns the segments for the app route module\n */\nfunction collectAppRouteSegments(\n  routeModule: AppRouteRouteModule\n): AppSegment[] {\n  // Get the pathname parts, slice off the first element (which is empty).\n  const parts = routeModule.definition.pathname.split('/').slice(1)\n  if (parts.length === 0) {\n    throw new InvariantError('Expected at least one segment')\n  }\n\n  // Generate all the segments.\n  const segments: AppSegment[] = parts.map((name) => {\n    const param = getSegmentParam(name)?.param\n\n    return {\n      name,\n      param,\n      filePath: undefined,\n      isDynamicSegment: !!param,\n      config: undefined,\n      generateStaticParams: undefined,\n    }\n  })\n\n  // We know we have at least one, we verified this above. We should get the\n  // last segment which represents the root route module.\n  const segment = segments[segments.length - 1]\n\n  segment.filePath = routeModule.definition.filename\n\n  // Extract the segment config from the userland module.\n  attach(segment, routeModule.userland, routeModule.definition.pathname)\n\n  return segments\n}\n\n/**\n * Collects the segments for a given route module.\n *\n * @param components the loaded components\n * @returns the segments for the route module\n */\nexport function collectSegments({\n  routeModule,\n}: LoadComponentsReturnType<AppPageModule | AppRouteModule>):\n  | Promise<AppSegment[]>\n  | AppSegment[] {\n  if (isAppRouteRouteModule(routeModule)) {\n    return collectAppRouteSegments(routeModule)\n  }\n\n  if (isAppPageRouteModule(routeModule)) {\n    return collectAppPageSegments(routeModule)\n  }\n\n  throw new InvariantError(\n    'Expected a route module to be one of app route or page'\n  )\n}\n"],"names":["collectSegments","attach","segment","userland","route","config","parseAppSegmentConfig","Object","keys","length","generateStaticParams","runtime","Error","collectAppPageSegments","routeModule","uniqueSegments","Map","queue","loaderTree","getSegmentParam","currentSegments","shift","name","parallelRoutes","mod","filePath","getLayoutOrPageModule","isClientComponent","isClientReference","param","undefined","isDynamicSegment","definition","pathname","segmentKey","getSegmentKey","has","set","updatedSegments","PAGE_SEGMENT_KEY","forEach","seg","key","parallelRouteKey","parallelRoute","push","Array","from","values","collectAppRouteSegments","parts","split","slice","InvariantError","segments","map","filename","isAppRouteRouteModule","isAppPageRouteModule"],"mappings":";;;;+BA6LgBA;;;eAAAA;;;kCAhLT;gCAEwB;wBAIxB;2CAC2B;iCACF;8BAIz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