{"version":3,"file":"injectRouteHelpers.js","names":[],"sources":["esm/typed-routes/injectRouteHelpers.js"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { readFile, writeFile } from \"node:fs/promises\";\nasync function injectRouteHelpers(filePath, routePath, mode) {\n  if (!existsSync(filePath)) {\n    return false;\n  }\n  try {\n    let content = await readFile(filePath, \"utf-8\");\n    let modified = false;\n    const hasTypeRoute = /^type\\s+Route\\s*=/m.test(content);\n    const hasConstRoute = /^const\\s+route\\s*=/m.test(content);\n    if (mode === \"runtime\" && !hasConstRoute) {\n      const { updatedContent } = addCreateRouteImport(content);\n      content = updatedContent;\n      const routeDeclaration = `const route = createRoute<'${routePath}'>()`;\n      content = insertAfterImports(content, routeDeclaration);\n      modified = true;\n    }\n    if (mode === \"type\" && !hasTypeRoute) {\n      const { updatedContent } = addRouteTypeImport(content);\n      content = updatedContent;\n      const typeDeclaration = `type Route = RouteType<'${routePath}'>`;\n      content = insertAfterImports(content, typeDeclaration);\n      modified = true;\n    }\n    if (modified) {\n      await writeFile(filePath, content, \"utf-8\");\n      return true;\n    }\n    return false;\n  } catch (error) {\n    console.error(`Failed to inject route helpers into ${filePath}:`, error);\n    return false;\n  }\n}\nfunction addCreateRouteImport(content) {\n  if (/import\\s+[^'\"]*createRoute[^'\"]*from\\s+['\"]one['\"]/m.test(content)) {\n    return { updatedContent: content, importAdded: false };\n  }\n  const oneImportRegex = /import\\s+{([^}]*)}\\s+from\\s+['\"]one['\"]/m;\n  const match = content.match(oneImportRegex);\n  if (match) {\n    const existingImports = match[1].trim();\n    const newImports = existingImports ? `${existingImports}, createRoute` : \"createRoute\";\n    const updatedContent = content.replace(\n      oneImportRegex,\n      `import { ${newImports} } from 'one'`\n    );\n    return { updatedContent, importAdded: true };\n  }\n  const lastImportIndex = findLastImportIndex(content);\n  if (lastImportIndex >= 0) {\n    const lines = content.split(\"\\n\");\n    lines.splice(lastImportIndex + 1, 0, `import { createRoute } from 'one'`);\n    return { updatedContent: lines.join(\"\\n\"), importAdded: true };\n  }\n  const newImport = `import { createRoute } from 'one'\n`;\n  return { updatedContent: newImport + content, importAdded: true };\n}\nfunction addRouteTypeImport(content) {\n  if (/import\\s+type\\s+[^'\"]*RouteType[^'\"]*from\\s+['\"]one['\"]/m.test(content)) {\n    return { updatedContent: content, importAdded: false };\n  }\n  const oneTypeImportRegex = /import\\s+type\\s+{([^}]*)}\\s+from\\s+['\"]one['\"]/m;\n  const match = content.match(oneTypeImportRegex);\n  if (match) {\n    const existingImports = match[1].trim();\n    const newImports = existingImports ? `${existingImports}, RouteType` : \"RouteType\";\n    const updatedContent = content.replace(\n      oneTypeImportRegex,\n      `import type { ${newImports} } from 'one'`\n    );\n    return { updatedContent, importAdded: true };\n  }\n  const lastImportIndex = findLastImportIndex(content);\n  if (lastImportIndex >= 0) {\n    const lines = content.split(\"\\n\");\n    lines.splice(lastImportIndex + 1, 0, `import type { RouteType } from 'one'`);\n    return { updatedContent: lines.join(\"\\n\"), importAdded: true };\n  }\n  const newImport = `import type { RouteType } from 'one'\n`;\n  return { updatedContent: newImport + content, importAdded: true };\n}\nfunction findLastImportIndex(content) {\n  const lines = content.split(\"\\n\");\n  let lastImportIndex = -1;\n  for (let i = 0; i < lines.length; i++) {\n    const line = lines[i].trim();\n    if (line.startsWith(\"import \") || lastImportIndex >= 0 && (line.startsWith(\"from \") || line === \"}\")) {\n      lastImportIndex = i;\n    } else if (lastImportIndex >= 0 && line && !line.startsWith(\"//\")) {\n      break;\n    }\n  }\n  return lastImportIndex;\n}\nfunction insertAfterImports(content, codeToInsert) {\n  const lines = content.split(\"\\n\");\n  const lastImportIndex = findLastImportIndex(content);\n  if (lastImportIndex >= 0) {\n    const nextLine = lines[lastImportIndex + 1];\n    const hasBlankLine = nextLine === \"\";\n    if (hasBlankLine) {\n      lines.splice(lastImportIndex + 2, 0, codeToInsert, \"\");\n    } else {\n      lines.splice(lastImportIndex + 1, 0, \"\", codeToInsert, \"\");\n    }\n    return lines.join(\"\\n\");\n  }\n  return codeToInsert + \"\\n\\n\" + content;\n}\nexport {\n  injectRouteHelpers\n};\n//# sourceMappingURL=injectRouteHelpers.js.map\n"],"mappings":";;;;AAEA,eAAe,mBAAmB,UAAU,WAAW,MAAM;CAC3D,IAAI,CAAC,WAAW,QAAQ,GAAG;EACzB,OAAO;CACT;CACA,IAAI;EACF,IAAI,UAAU,MAAM,SAAS,UAAU,OAAO;EAC9C,IAAI,WAAW;EACf,MAAM,eAAe,qBAAqB,KAAK,OAAO;EACtD,MAAM,gBAAgB,sBAAsB,KAAK,OAAO;EACxD,IAAI,SAAS,aAAa,CAAC,eAAe;GACxC,MAAM,EAAE,mBAAmB,qBAAqB,OAAO;GACvD,UAAU;GACV,MAAM,mBAAmB,8BAA8B,UAAU;GACjE,UAAU,mBAAmB,SAAS,gBAAgB;GACtD,WAAW;EACb;EACA,IAAI,SAAS,UAAU,CAAC,cAAc;GACpC,MAAM,EAAE,mBAAmB,mBAAmB,OAAO;GACrD,UAAU;GACV,MAAM,kBAAkB,2BAA2B,UAAU;GAC7D,UAAU,mBAAmB,SAAS,eAAe;GACrD,WAAW;EACb;EACA,IAAI,UAAU;GACZ,MAAM,UAAU,UAAU,SAAS,OAAO;GAC1C,OAAO;EACT;EACA,OAAO;CACT,SAAS,OAAO;EACd,QAAQ,MAAM,uCAAuC,SAAS,IAAI,KAAK;EACvE,OAAO;CACT;AACF;AACA,SAAS,qBAAqB,SAAS;CACrC,IAAI,sDAAsD,KAAK,OAAO,GAAG;EACvE,OAAO;GAAE,gBAAgB;GAAS,aAAa;EAAM;CACvD;CACA,MAAM,iBAAiB;CACvB,MAAM,QAAQ,QAAQ,MAAM,cAAc;CAC1C,IAAI,OAAO;EACT,MAAM,kBAAkB,MAAM,EAAE,CAAC,KAAK;EACtC,MAAM,aAAa,kBAAkB,GAAG,gBAAgB,iBAAiB;EACzE,MAAM,iBAAiB,QAAQ,QAC7B,gBACA,YAAY,WAAW,cACzB;EACA,OAAO;GAAE;GAAgB,aAAa;EA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