{"version":3,"file":"astImports.cjs","names":[],"sources":["../../../../src/init/utils/astImports.ts"],"sourcesContent":["import * as recast from 'recast';\n\nconst { builders: b } = recast.types;\n\n/**\n * Index just past the leading directives (e.g. `'use client'`) and import\n * declarations — the safe insertion point for a new import.\n */\nexport const firstInsertIndex = (ast: any): number => {\n  const body = ast.program.body;\n  let index = 0;\n  while (\n    index < body.length &&\n    ((body[index].type === 'ExpressionStatement' &&\n      body[index].expression?.type === 'StringLiteral') ||\n      body[index].type === 'ImportDeclaration')\n  ) {\n    index++;\n  }\n  return index;\n};\n\n/**\n * Whether `name` is already bound at module scope — by any import (under that\n * local name, regardless of source) or by a top-level\n * `const`/`let`/`var`/`function`/`class` declaration.\n *\n * Used to avoid redeclaring an identifier the file already provides (e.g. a\n * `defaultLocale` re-exported from a local `i18n` module), which would otherwise\n * produce an `Identifier '…' has already been declared` parse error.\n */\nexport const isModuleScopeBinding = (ast: any, name: string): boolean =>\n  ast.program.body.some((statement: any) => {\n    // `export const x = …` / `export function x` / `export default class x`\n    const node =\n      (statement.type === 'ExportNamedDeclaration' ||\n        statement.type === 'ExportDefaultDeclaration') &&\n      statement.declaration\n        ? statement.declaration\n        : statement;\n\n    if (node.type === 'ImportDeclaration') {\n      return node.specifiers.some((spec: any) => spec.local?.name === name);\n    }\n    if (node.type === 'VariableDeclaration') {\n      return node.declarations.some(\n        (decl: any) => decl.id?.type === 'Identifier' && decl.id.name === name\n      );\n    }\n    if (\n      node.type === 'FunctionDeclaration' ||\n      node.type === 'ClassDeclaration'\n    ) {\n      return node.id?.name === name;\n    }\n    return false;\n  });\n\n/**\n * Ensures `import { importName } from source`, merging into an existing import\n * from the same source. No-ops when `importName` is already bound at module\n * scope (imported from another source or declared locally), reusing that binding\n * instead of redeclaring it.\n *\n * Shared across every framework adapter so injected identifiers never collide\n * with bindings the user's file already provides.\n */\nexport const ensureNamedImport = (\n  ast: any,\n  importName: string,\n  source: string\n): void => {\n  if (isModuleScopeBinding(ast, importName)) return;\n\n  for (const statement of ast.program.body) {\n    if (\n      statement.type === 'ImportDeclaration' &&\n      statement.source.value === source\n    ) {\n      const hasSpecifier = statement.specifiers.some(\n        (spec: any) =>\n          spec.type === 'ImportSpecifier' && spec.imported?.name === importName\n      );\n      if (!hasSpecifier) {\n        statement.specifiers.push(b.importSpecifier(b.identifier(importName)));\n      }\n      return;\n    }\n  }\n\n  ast.program.body.splice(\n    firstInsertIndex(ast),\n    0,\n    b.importDeclaration(\n      [b.importSpecifier(b.identifier(importName))],\n      b.stringLiteral(source)\n    )\n  );\n};\n"],"mappings":";;;;;;AAEA,MAAM,EAAE,UAAU,MAAM,OAAO;;;;;AAM/B,MAAa,oBAAoB,QAAqB;CACpD,MAAM,OAAO,IAAI,QAAQ;CACzB,IAAI,QAAQ;CACZ,OACE,QAAQ,KAAK,WACX,KAAK,MAAM,CAAC,SAAS,yBACrB,KAAK,MAAM,CAAC,YAAY,SAAS,mBACjC,KAAK,MAAM,CAAC,SAAS,sBAEvB;CAEF,OAAO;AACT;;;;;;;;;;AAWA,MAAa,wBAAwB,KAAU,SAC7C,IAAI,QAAQ,KAAK,MAAM,cAAmB;CAExC,MAAM,QACH,UAAU,SAAS,4BAClB,UAAU,SAAS,+BACrB,UAAU,cACN,UAAU,cACV;CAEN,IAAI,KAAK,SAAS,qBAChB,OAAO,KAAK,WAAW,MAAM,SAAc,KAAK,OAAO,SAAS,IAAI;CAEtE,IAAI,KAAK,SAAS,uBAChB,OAAO,KAAK,aAAa,MACtB,SAAc,KAAK,IAAI,SAAS,gBAAgB,KAAK,GAAG,SAAS,IACpE;CAEF,IACE,KAAK,SAAS,yBACd,KAAK,SAAS,oBAEd,OAAO,KAAK,IAAI,SAAS;CAE3B,OAAO;AACT,CAAC;;;;;;;;;;AAWH,MAAa,qBACX,KACA,YACA,WACS;CACT,IAAI,qBAAqB,KAAK,UAAU,GAAG;CAE3C,KAAK,MAAM,aAAa,IAAI,QAAQ,MAClC,IACE,UAAU,SAAS,uBACnB,UAAU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