{"version":3,"file":"manifest.cjs","names":[],"sources":["../../../src/codegen/manifest.ts"],"sourcesContent":["import { AST_NODE_TYPES, type TSESTree } from \"@typescript-eslint/types\";\n\nimport type { GeneratedModel, LayoutFile, RouteEntry } from \"../types.js\";\nimport {\n  arrayExpression,\n  exportConst,\n  id,\n  importDefaultDeclaration,\n  objectExpression,\n  objectProperty,\n  str,\n} from \"./builders.js\";\nimport { asNode } from \"./ast.js\";\n\nexport type EmitManifestOptions = {\n  quotes?: \"single\" | \"double\" | undefined;\n  header?: readonly string[] | undefined;\n  rewriteImportPath?: ((spec: string) => string) | undefined;\n  taserImportPath?: string | undefined;\n};\n\nexport function joinManifestSections(\n  header?: readonly string[] | undefined,\n  body?: string | undefined,\n): string {\n  const sections = [...(header ?? []), body ?? \"\"].filter((section) => section.length > 0);\n  return `${sections.join(\"\\n\")}\\n`;\n}\n\nfunction applyRewrite(spec: string, rewrite?: (spec: string) => string): string {\n  return rewrite ? rewrite(spec) : spec;\n}\n\nexport function buildLayoutImports(\n  layouts: LayoutFile[],\n  rewriteImportPath?: (spec: string) => string,\n): TSESTree.ImportDeclaration[] {\n  return layouts.map((layout) =>\n    importDefaultDeclaration(layout.importName, applyRewrite(layout.importPath, rewriteImportPath)),\n  );\n}\n\nexport function buildRouteImports(\n  routes: RouteEntry[],\n  rewriteImportPath?: (spec: string) => string,\n): TSESTree.ImportDeclaration[] {\n  return routes.map((route) =>\n    importDefaultDeclaration(route.importName, applyRewrite(route.importPath, rewriteImportPath)),\n  );\n}\n\nexport function buildManifestLayoutsObject(model: GeneratedModel): TSESTree.ObjectExpression {\n  const layoutById = new Map(model.layouts.map((layout) => [layout.id, layout]));\n  const properties: TSESTree.PropertyNonComputedName[] = [];\n\n  for (const layoutId of model.layoutIds) {\n    const layout = layoutById.get(layoutId);\n    if (!layout) {\n      throw new Error(`Missing layout for ${layoutId}`);\n    }\n\n    properties.push(objectProperty(str(layoutId), id(layout.importName)));\n  }\n\n  return objectExpression(properties);\n}\n\nexport function buildManifestRoutesObject(model: GeneratedModel): TSESTree.ObjectExpression {\n  const properties: TSESTree.PropertyNonComputedName[] = [];\n  const routeByRel = new Map(model.routes.map((route) => [route.routeRel, route]));\n\n  for (const [urlPath, entries] of model.routesByPath) {\n    const methodProperties: TSESTree.PropertyNonComputedName[] = [];\n\n    for (const entry of entries) {\n      const fullRoute = routeByRel.get(entry.routeRel);\n      if (!fullRoute) continue;\n\n      methodProperties.push(\n        objectProperty(\n          id(entry.method),\n          objectExpression([\n            objectProperty(\n              id(\"layouts\"),\n              arrayExpression(\n                (entry.layouts ?? fullRoute.layouts).map((layoutId) => str(layoutId)),\n              ),\n            ),\n            objectProperty(id(\"route\"), id(fullRoute.importName)),\n          ]),\n        ),\n      );\n    }\n\n    properties.push(objectProperty(str(urlPath), objectExpression(methodProperties)));\n  }\n\n  return objectExpression(properties);\n}\n\nexport function buildVirtualManifestProgram(\n  model: GeneratedModel,\n  rewriteImportPath?: EmitManifestOptions[\"rewriteImportPath\"],\n): TSESTree.Program {\n  const body: TSESTree.Statement[] = [\n    ...buildLayoutImports(model.layouts, rewriteImportPath),\n    ...buildRouteImports(model.routes, rewriteImportPath),\n    exportConst(\n      \"routeManifest\",\n      objectExpression([\n        objectProperty(id(\"layouts\"), buildManifestLayoutsObject(model)),\n        objectProperty(id(\"routes\"), buildManifestRoutesObject(model)),\n      ]),\n    ),\n  ];\n\n  return asNode<TSESTree.Program>({\n    type: AST_NODE_TYPES.Program,\n    body,\n    sourceType: \"module\",\n    comments: undefined,\n    tokens: undefined,\n  });\n}\n"],"mappings":";;;;AAqBA,SAAgB,qBACd,QACA,MACQ;CAER,OAAO,GADU,CAAC,GAAI,UAAU,CAAC,GAAI,QAAQ,EAAE,CAAC,CAAC,QAAQ,YAAY,QAAQ,SAAS,CAC5E,CAAA,CAAS,KAAK,IAAI,EAAE;AAChC;AAEA,SAAS,aAAa,MAAc,SAA4C;CAC9E,OAAO,UAAU,QAAQ,IAAI,IAAI;AACnC;AAEA,S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