{"version":3,"file":"reactNativeCodegen.js","names":[],"sources":["esm/reactNativeCodegen.js"],"sourcesContent":["import MagicString from \"magic-string\";\nimport {\n  generateViewConfig,\n  parseSpec,\n  renderViewConfigModule\n} from \"./reactNativeViewConfig\";\nfunction normalizePath(id) {\n  return id.replace(/\\\\/g, \"/\");\n}\nconst CODEGEN_FILE_RE = /(?:NativeComponent\\.[cm]?[jt]sx?$|[/\\\\]specs?[/\\\\])/;\nconst CODEGEN_CALL_RE = /codegenNativeComponent\\s*[<(]/;\nconst startOf = (node) => node.start ?? node.range[0];\nconst endOf = (node) => node.end ?? node.range[1];\nfunction unwrapExpression(node) {\n  while (node) {\n    if (node.type === \"TSAsExpression\" || node.type === \"TSSatisfiesExpression\" || node.type === \"TSTypeAssertion\" || node.type === \"TypeCastExpression\" || node.type === \"AsExpression\" || node.type === \"ParenthesizedExpression\") {\n      node = node.expression;\n    } else if (node.type === \"SequenceExpression\" && Array.isArray(node.expressions) && node.expressions.length > 0) {\n      node = node.expressions[node.expressions.length - 1];\n    } else {\n      break;\n    }\n  }\n  return node;\n}\nfunction isCodegenDeclaration(declaration) {\n  if (!declaration) return false;\n  if (declaration.left && declaration.left.left && declaration.left.left.name === \"codegenNativeComponent\") {\n    return true;\n  }\n  const unwrapped = unwrapExpression(declaration);\n  return unwrapped?.type === \"CallExpression\" && unwrapped.callee?.type === \"Identifier\" && unwrapped.callee.name === \"codegenNativeComponent\";\n}\nfunction isCodegenNativeCommandsDeclaration(declaration) {\n  if (!declaration) return false;\n  const unwrapped = unwrapExpression(declaration);\n  return unwrapped?.type === \"CallExpression\" && unwrapped.callee?.type === \"Identifier\" && unwrapped.callee.name === \"codegenNativeCommands\";\n}\nfunction isReactNativeCodegenSpec(id, code) {\n  return CODEGEN_FILE_RE.test(normalizePath(id.split(\"?\")[0])) && CODEGEN_CALL_RE.test(code);\n}\nfunction transformReactNativeCodegen(code, id, projectRoot) {\n  const cleanId = normalizePath(id.split(\"?\")[0]);\n  if (!isReactNativeCodegenSpec(id, code)) {\n    return;\n  }\n  const program = parseSpec(code, cleanId, projectRoot);\n  const body = program?.body;\n  if (!body) {\n    return;\n  }\n  let defaultExport = null;\n  let commandsExport = null;\n  for (const node of body) {\n    if (node.type === \"ExportDefaultDeclaration\") {\n      if (isCodegenDeclaration(node.declaration)) {\n        defaultExport = node;\n      }\n    } else if (node.type === \"ExportNamedDeclaration\") {\n      if (node.declaration && node.declaration.declarations) {\n        const firstDeclaration = node.declaration.declarations[0];\n        if (firstDeclaration?.type === \"VariableDeclarator\") {\n          const isValidCommandsExport = isCodegenNativeCommandsDeclaration(\n            firstDeclaration.init\n          );\n          if (isValidCommandsExport) {\n            if (firstDeclaration.id?.type === \"Identifier\" && firstDeclaration.id.name !== \"Commands\") {\n              throw new Error(\"Native commands must be exported with the name 'Commands'\");\n            }\n            commandsExport = node;\n          } else {\n            if (firstDeclaration.id?.type === \"Identifier\" && firstDeclaration.id.name === \"Commands\") {\n              throw new Error(\n                \"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.\"\n              );\n            }\n          }\n        }\n      } else if (node.specifiers && node.specifiers.length > 0) {\n        for (const specifier of node.specifiers) {\n          if (specifier.type === \"ExportSpecifier\" && (specifier.local?.type === \"Identifier\" && specifier.local.name === \"Commands\" || specifier.exported?.type === \"Identifier\" && specifier.exported.name === \"Commands\")) {\n            throw new Error(\n              \"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands.\"\n            );\n          }\n        }\n      }\n    }\n  }\n  if (!defaultExport) {\n    return;\n  }\n  const generated = generateViewConfig(program);\n  if (!generated) {\n    return;\n  }\n  const s = new MagicString(code);\n  if (commandsExport) {\n    let removeEnd = endOf(commandsExport);\n    if (code[removeEnd] === \"\\r\") removeEnd++;\n    if (code[removeEnd] === \"\\n\") removeEnd++;\n    s.remove(startOf(commandsExport), removeEnd);\n  }\n  s.overwrite(\n    startOf(defaultExport),\n    endOf(defaultExport),\n    renderViewConfigModule(generated)\n  );\n  return {\n    code: s.toString(),\n    map: s.generateMap({ hires: true, source: cleanId })\n  };\n}\nexport {\n  isReactNativeCodegenSpec,\n  transformReactNativeCodegen\n};\n//# sourceMappingURL=reactNativeCodegen.js.map\n"],"mappings":";;;;AAMA,SAAS,cAAc,IAAI;CACzB,OAAO,GAAG,QAAQ,OAAO,GAAG;AAC9B;AACA,MAAM,kBAAkB;AACxB,MAAM,kBAAkB;AACxB,MAAM,WAAW,SAAS,KAAK,SAAS,KAAK,MAAM;AACnD,MAAM,SAAS,SAAS,KAAK,OAAO,KAAK,MAAM;AAC/C,SAAS,iBAAiB,MAAM;CAC9B,OAAO,MAAM;EACX,IAAI,KAAK,SAAS,oBAAoB,KAAK,SAAS,2BAA2B,KAAK,SAAS,qBAAqB,KAAK,SAAS,wBAAwB,KAAK,SAAS,kBAAkB,KAAK,SAAS,2BAA2B;GAC/N,OAAO,KAAK;EACd,OAAO,IAAI,KAAK,SAAS,wBAAwB,MAAM,QAAQ,KAAK,WAAW,KAAK,KAAK,YAAY,SAAS,GAAG;GAC/G,OAAO,KAAK,YAAY,KAAK,YAAY,SAAS;EACpD,OAAO;GACL;EACF;CACF;CACA,OAAO;AACT;AACA,SAAS,qBAAqB,aAAa;CACzC,IAAI,CAAC,aAAa,OAAO;CACzB,IAAI,YAAY,QAAQ,YAAY,KAAK,QAAQ,YAAY,KAAK,KAAK,SAAS,0BAA0B;EACxG,OAAO;CACT;CACA,MAAM,YAAY,iBAAiB,WAAW;CAC9C,OAAO,WAAW,SAAS,oBAAoB,UAAU,QAAQ,SAAS,gBAAgB,UAAU,OAAO,SAAS;AACtH;AACA,SAAS,mCAAmC,aAAa;CACvD,IAAI,CAAC,aAAa,OAAO;CACzB,MAAM,YAAY,iBAAiB,WAAW;CAC9C,OAAO,WAAW,SAAS,oBAAoB,UAAU,QAAQ,SAAS,gBAAgB,UAAU,OAAO,SAAS;AACtH;AACA,SAAS,yBAAyB,IAAI,MAAM;CAC1C,OAAO,gBAAgB,KAAK,cAAc,GAAG,MAAM,G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