{"version":3,"file":"types.cjs","names":[],"sources":["../../../src/codegen/types.ts"],"sourcesContent":["import { AST_NODE_TYPES, type TSESTree } from \"@typescript-eslint/types\";\n\nimport type { GeneratedModel, LayoutFile } from \"../types.js\";\nimport {\n  exportConst,\n  exportTypeAlias,\n  id,\n  importDefaultDeclaration,\n  objectExpression,\n  objectProperty,\n  str,\n  tsAsConst,\n  tsConditionalType,\n  tsInferType,\n  tsLiteralType,\n  tsNullKeyword,\n  tsPropertySignature,\n  tsTupleType,\n  tsTypeAlias,\n  tsTypeLiteral,\n  tsTypeQuery,\n  tsTypeReference,\n  tsUnionType,\n} from \"./builders.js\";\nimport {\n  buildLayoutImports,\n  buildManifestLayoutsObject,\n  buildManifestRoutesObject,\n  buildRouteImports,\n  type EmitManifestOptions,\n} from \"./manifest.js\";\nimport { asNode } from \"./ast.js\";\n\nexport function buildLayoutTreeType(\n  layoutIds: string[],\n  layoutParents: Map<string, string | null>,\n): TSESTree.TSTypeAliasDeclaration {\n  const properties: TSESTree.TypeElement[] = layoutIds.map((layoutId) => {\n    const parent = layoutParents.get(layoutId) ?? null;\n    const parentType = parent === null ? tsNullKeyword() : tsLiteralType(parent);\n\n    return tsPropertySignature(\n      str(layoutId),\n      tsTypeLiteral([tsPropertySignature(id(\"parent\"), parentType)]),\n    );\n  });\n\n  return tsTypeAlias(\"LayoutTreeGen\", tsTypeLiteral(properties));\n}\n\nexport function buildLayoutMiddlewaresType(\n  layoutIds: string[],\n  layouts: LayoutFile[],\n): TSESTree.TSTypeAliasDeclaration {\n  const layoutById = new Map(layouts.map((layout) => [layout.id, layout]));\n\n  const properties: TSESTree.TypeElement[] = layoutIds.map((layoutId) => {\n    const layout = layoutById.get(layoutId);\n    if (!layout) {\n      throw new Error(`Missing layout import for ${layoutId}`);\n    }\n\n    return tsPropertySignature(str(layoutId), tsTypeQuery(layout.importName));\n  });\n\n  return tsTypeAlias(\"LayoutMiddlewaresGen\", tsTypeLiteral(properties));\n}\n\nexport function buildRouteByPathMethodType(\n  routesByPath: GeneratedModel[\"routesByPath\"],\n): TSESTree.TSTypeAliasDeclaration {\n  const properties: TSESTree.TypeElement[] = [];\n\n  for (const [urlPath, entries] of routesByPath) {\n    const methodProperties: TSESTree.TypeElement[] = [];\n\n    for (const entry of entries) {\n      const layoutsType = tsTupleType(entry.layouts.map((layoutId) => tsLiteralType(layoutId)));\n\n      methodProperties.push(\n        tsPropertySignature(\n          id(entry.method),\n          tsTypeLiteral([\n            tsPropertySignature(id(\"layouts\"), layoutsType),\n            tsPropertySignature(id(\"route\"), tsTypeQuery(entry.importName)),\n          ]),\n        ),\n      );\n    }\n\n    properties.push(tsPropertySignature(str(urlPath), tsTypeLiteral(methodProperties)));\n  }\n\n  return tsTypeAlias(\"RouteByPathMethodGen\", tsTypeLiteral(properties));\n}\n\nexport function buildAppContextType(): TSESTree.TSTypeAliasDeclaration {\n  return tsTypeAlias(\n    \"AppContextGen\",\n    tsConditionalType(\n      tsTypeQuery(\"taser\"),\n      tsTypeLiteral([\n        tsPropertySignature(\n          id(\"$Infer\"),\n          tsTypeLiteral([tsPropertySignature(id(\"Context\"), tsInferType(\"C\"))]),\n        ),\n      ]),\n      tsTypeReference(\"C\"),\n      tsTypeLiteral([]),\n    ),\n  );\n}\n\nexport function buildRouterRegisterAugmentation(\n  hasAppContext: boolean = false,\n): TSESTree.TSModuleDeclarationModuleWithStringIdDeclared {\n  const registerMembers: TSESTree.TypeElement[] = [];\n\n  if (hasAppContext) {\n    registerMembers.push(tsPropertySignature(id(\"AppContext\"), tsTypeReference(\"AppContextGen\")));\n  }\n\n  registerMembers.push(\n    tsPropertySignature(id(\"RoutePath\"), tsTypeReference(\"RoutePathGen\")),\n    tsPropertySignature(id(\"LayoutId\"), tsTypeReference(\"LayoutIdGen\")),\n    tsPropertySignature(id(\"LayoutTree\"), tsTypeReference(\"LayoutTreeGen\")),\n  );\n\n  return asNode<TSESTree.TSModuleDeclarationModuleWithStringIdDeclared>({\n    type: AST_NODE_TYPES.TSModuleDeclaration,\n    declare: true,\n    kind: \"module\",\n    global: false,\n    id: str(\"@taserjs/router\"),\n    body: asNode<TSESTree.TSModuleBlock>({\n      type: AST_NODE_TYPES.TSModuleBlock,\n      body: [\n        asNode<TSESTree.TSInterfaceDeclaration>({\n          type: AST_NODE_TYPES.TSInterfaceDeclaration,\n          declare: false,\n          id: id(\"RouterRegister\"),\n          typeParameters: undefined,\n          extends: [],\n          body: asNode<TSESTree.TSInterfaceBody>({\n            type: AST_NODE_TYPES.TSInterfaceBody,\n            body: registerMembers,\n          }),\n        }),\n        asNode<TSESTree.TSInterfaceDeclaration>({\n          type: AST_NODE_TYPES.TSInterfaceDeclaration,\n          declare: false,\n          id: id(\"RouterMiddlewaresRegister\"),\n          typeParameters: undefined,\n          extends: [],\n          body: asNode<TSESTree.TSInterfaceBody>({\n            type: AST_NODE_TYPES.TSInterfaceBody,\n            body: [\n              tsPropertySignature(id(\"LayoutMiddlewares\"), tsTypeReference(\"LayoutMiddlewaresGen\")),\n            ],\n          }),\n        }),\n        asNode<TSESTree.TSInterfaceDeclaration>({\n          type: AST_NODE_TYPES.TSInterfaceDeclaration,\n          declare: false,\n          id: id(\"RouterRoutesRegister\"),\n          typeParameters: undefined,\n          extends: [],\n          body: asNode<TSESTree.TSInterfaceBody>({\n            type: AST_NODE_TYPES.TSInterfaceBody,\n            body: [\n              tsPropertySignature(id(\"RouteByPathMethod\"), tsTypeReference(\"RouteByPathMethodGen\")),\n            ],\n          }),\n        }),\n      ],\n    }),\n  });\n}\n\nexport function buildFullProgram(\n  model: GeneratedModel,\n  options?: EmitManifestOptions,\n): TSESTree.Program {\n  const rewriteImportPath = options?.rewriteImportPath;\n  const taserImportPath = options?.taserImportPath;\n\n  const routePathType = tsTypeAlias(\n    \"RoutePathGen\",\n    tsUnionType(model.routePaths.map((path) => tsLiteralType(path))),\n  );\n\n  const layoutIdType = tsTypeAlias(\n    \"LayoutIdGen\",\n    tsUnionType(model.layoutIds.map((idValue) => tsLiteralType(idValue))),\n  );\n\n  const layoutTreeType = buildLayoutTreeType(model.layoutIds, model.layoutParents);\n  const layoutMiddlewaresType = buildLayoutMiddlewaresType(model.layoutIds, model.layouts);\n  const routeByPathMethodType = buildRouteByPathMethodType(model.routesByPath);\n\n  const imports: TSESTree.Statement[] = [];\n\n  if (taserImportPath) {\n    imports.push(importDefaultDeclaration(\"taser\", taserImportPath, \"type\"));\n  }\n\n  imports.push(\n    ...buildLayoutImports(model.layouts, rewriteImportPath),\n    ...buildRouteImports(model.routes, rewriteImportPath),\n  );\n\n  const body: TSESTree.Statement[] = [\n    ...imports,\n    exportConst(\n      \"routeManifest\",\n      tsAsConst(\n        objectExpression([\n          objectProperty(id(\"layouts\"), buildManifestLayoutsObject(model)),\n          objectProperty(id(\"routes\"), buildManifestRoutesObject(model)),\n        ]),\n      ),\n    ),\n    ...(taserImportPath ? [buildAppContextType()] : []),\n    routePathType,\n    layoutIdType,\n    layoutTreeType,\n    layoutMiddlewaresType,\n    routeByPathMethodType,\n    buildRouterRegisterAugmentation(Boolean(taserImportPath)),\n    exportTypeAlias(\"RouteManifest\", tsTypeQuery(\"routeManifest\")),\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":";;;;;AAiCA,SAAgB,oBACd,WACA,eACiC;CAWjC,OAAO,iBAAA,YAAY,iBAAiB,iBAAA,cAVO,UAAU,KAAK,aAAa;EACrE,MAAM,SAAS,cAAc,IAAI,QAAQ,KAAK;EAC9C,MAAM,aAAa,WAAW,OAAO,iBAAA,cAAc,IAAI,iBAAA,cAAc,MAAM;EAE3E,OAAO,iBAAA,oBACL,iBAAA,IAAI,QAAQ,GACZ,iBAAA,cAAc,CAAC,iBAAA,oBAAoB,iBAAA,GAAG,QAAQ,GAAG,UAAU,CAAC,CAAC,CAC/D;CACF,CAEkD,CAAU,CAAC;AAC/D;AAEA,SAAgB,2BACd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