function scanRouteFiles(vfs, routesDir) {
  const files = [];
  function walk(dir, prefix) {
    let entries;
    try {
      entries = vfs.readdirSync(dir);
    } catch {
      return;
    }
    for (const entry of entries) {
      const fullPath = dir + "/" + entry;
      let stat;
      try {
        stat = vfs.statSync(fullPath);
      } catch {
        continue;
      }
      if (stat.isDirectory()) {
        walk(fullPath, prefix ? prefix + "/" + entry : entry);
      } else if (/\.(tsx?|jsx?)$/.test(entry)) {
        files.push(prefix ? prefix + "/" + entry : entry);
      }
    }
  }
  walk(routesDir, "");
  return files.sort();
}
function fileToRoutePath(filePath) {
  const withoutExt = filePath.replace(/\.(tsx?|jsx?)$/, "");
  if (withoutExt === "__root") {
    return { routeId: "__root__", routePath: "", isRoot: true, isIndex: false, isLayout: false };
  }
  const segments = withoutExt.split("/");
  const pathSegments = [];
  let isIndex = false;
  let isLayout = false;
  for (let i = 0; i < segments.length; i++) {
    const seg = segments[i];
    if (seg.startsWith("(") && seg.endsWith(")")) {
      continue;
    }
    if (seg.startsWith("_")) {
      if (i === segments.length - 1) {
        isLayout = true;
      }
      continue;
    }
    if (i === segments.length - 1 && seg === "index") {
      isIndex = true;
      continue;
    }
    pathSegments.push(seg);
  }
  const routePath = "/" + pathSegments.join("/");
  const routeId = isIndex && pathSegments.length === 0 ? "/" : isIndex ? routePath + "/" : routePath;
  return { routeId, routePath: routePath === "" ? "/" : routePath, isRoot: false, isIndex, isLayout };
}
function toVariableName(filePath) {
  const withoutExt = filePath.replace(/\.(tsx?|jsx?)$/, "");
  const parts = withoutExt.split("/");
  const name = parts.map((part) => {
    let p = part;
    if (p.startsWith("(") && p.endsWith(")")) {
      p = p.slice(1, -1);
    }
    if (p.startsWith("_")) {
      p = p.slice(1) || "Layout";
    }
    if (p.startsWith("$")) {
      if (p === "$") return "Splat";
      p = p.slice(1);
    }
    return p.charAt(0).toUpperCase() + p.slice(1);
  }).join("");
  return name + "Route";
}
function buildRouteNodes(files) {
  const nodes = [];
  for (const filePath of files) {
    const { routeId, routePath, isRoot, isIndex, isLayout } = fileToRoutePath(filePath);
    const variableName = toVariableName(filePath);
    nodes.push({
      filePath,
      routeId: isRoot ? "__root__" : routeId,
      routePath: isRoot ? "" : routePath,
      importName: variableName + "Import",
      variableName,
      isRoot,
      isIndex,
      isLayout,
      parentId: "__root__"
      // simplified: all file routes are children of root
    });
  }
  return nodes;
}
function generateRouteTree(vfs, root) {
  const routesDir = root === "/" ? "/src/routes" : root + "/src/routes";
  try {
    vfs.statSync(routesDir);
  } catch {
    return "";
  }
  const files = scanRouteFiles(vfs, routesDir);
  if (files.length === 0) return "";
  const nodes = buildRouteNodes(files);
  const rootNode = nodes.find((n) => n.isRoot);
  const routeNodes = nodes.filter((n) => !n.isRoot);
  if (!rootNode) return "";
  const lines = [];
  lines.push("/* eslint-disable */");
  lines.push("");
  lines.push("// @ts-nocheck");
  lines.push("");
  lines.push("// This file was automatically generated by TanStack Router.");
  lines.push("// You should NOT make any changes in this file as it will be overwritten.");
  lines.push("");
  lines.push(`import { Route as ${rootNode.importName} } from './routes/__root'`);
  for (const node of routeNodes) {
    const importPath = "./routes/" + node.filePath.replace(/\.(tsx?|jsx?)$/, "");
    lines.push(`import { Route as ${node.importName} } from '${importPath}'`);
  }
  lines.push("");
  for (const node of routeNodes) {
    lines.push(`const ${node.variableName} = ${node.importName}.update({`);
    lines.push(`  id: '${node.routeId}',`);
    lines.push(`  path: '${node.routePath}',`);
    lines.push(`  getParentRoute: () => ${rootNode.importName},`);
    lines.push("} as any)");
  }
  if (routeNodes.length > 0) lines.push("");
  const nonLayoutRoutes = routeNodes.filter((n) => !n.isLayout);
  lines.push("export interface FileRoutesByFullPath {");
  for (const node of nonLayoutRoutes) {
    lines.push(`  '${node.routePath}': typeof ${node.variableName}`);
  }
  lines.push("}");
  lines.push("export interface FileRoutesByTo {");
  for (const node of nonLayoutRoutes) {
    lines.push(`  '${node.routePath}': typeof ${node.variableName}`);
  }
  lines.push("}");
  lines.push("export interface FileRoutesById {");
  lines.push(`  __root__: typeof ${rootNode.importName}`);
  for (const node of nonLayoutRoutes) {
    lines.push(`  '${node.routeId}': typeof ${node.variableName}`);
  }
  lines.push("}");
  lines.push("export interface FileRouteTypes {");
  lines.push("  fileRoutesByFullPath: FileRoutesByFullPath");
  const fullPaths = nonLayoutRoutes.map((n) => `'${n.routePath}'`).join(" | ");
  lines.push(`  fullPaths: ${fullPaths || "never"}`);
  lines.push("  fileRoutesByTo: FileRoutesByTo");
  const toPaths = nonLayoutRoutes.map((n) => `'${n.routePath}'`).join(" | ");
  lines.push(`  to: ${toPaths || "never"}`);
  const ids = [`'__root__'`, ...nonLayoutRoutes.map((n) => `'${n.routeId}'`)].join(" | ");
  lines.push(`  id: ${ids}`);
  lines.push("  fileRoutesById: FileRoutesById");
  lines.push("}");
  lines.push("export interface RootRouteChildren {");
  for (const node of routeNodes) {
    lines.push(`  ${node.variableName}: typeof ${node.variableName}`);
  }
  lines.push("}");
  lines.push("");
  lines.push("const rootRouteChildren: RootRouteChildren = {");
  for (const node of routeNodes) {
    lines.push(`  ${node.variableName}: ${node.variableName},`);
  }
  lines.push("}");
  lines.push("export const routeTree = " + rootNode.importName);
  lines.push("  ._addFileChildren(rootRouteChildren)");
  lines.push("  ._addFileTypes<FileRouteTypes>()");
  lines.push("");
  return lines.join("\n");
}
function generateAndWriteRouteTree(vfs, root) {
  const content = generateRouteTree(vfs, root);
  if (!content) return false;
  const outputPath = root === "/" ? "/src/routeTree.gen.ts" : root + "/src/routeTree.gen.ts";
  try {
    const existing = vfs.readFileSync(outputPath, "utf8");
    if (existing === content) return false;
  } catch {
  }
  vfs.writeFileSync(outputPath, content);
  return true;
}
export {
  generateAndWriteRouteTree,
  generateRouteTree
};
//# sourceMappingURL=tanstack-route-tree-Cb1t32So.js.map
