{"version":3,"file":"merge.mjs","names":[],"sources":["../../../src/codegen/parsers/merge.ts"],"sourcesContent":["import type { CommandMeta, FieldMeta } from '../types.ts';\n\n/**\n * Deep-merge multiple CommandMeta from different sources.\n * Deduplicates fields, resolves conflicts, and combines subcommands.\n *\n * Later sources take precedence for descriptions and types,\n * unless the earlier source was more specific (non-ambiguous).\n */\nexport function mergeCommandMeta(...sources: CommandMeta[]): CommandMeta {\n  if (sources.length === 0) {\n    return { name: '' };\n  }\n\n  if (sources.length === 1) {\n    return sources[0]!;\n  }\n\n  const result: CommandMeta = { name: '' };\n\n  for (const source of sources) {\n    // Name: first non-empty wins\n    if (source.name && !result.name) {\n      result.name = source.name;\n    }\n\n    // Description: last non-empty wins\n    if (source.description) {\n      result.description = source.description;\n    }\n\n    // Aliases: merge and deduplicate\n    if (source.aliases) {\n      result.aliases = [...new Set([...(result.aliases || []), ...source.aliases])];\n    }\n\n    // Examples: merge and deduplicate\n    if (source.examples) {\n      result.examples = [...new Set([...(result.examples || []), ...source.examples])];\n    }\n\n    // Deprecated: last truthy wins\n    if (source.deprecated !== undefined) {\n      result.deprecated = source.deprecated;\n    }\n\n    // Arguments: merge by name\n    if (source.arguments) {\n      result.arguments = mergeFields(result.arguments || [], source.arguments);\n    }\n\n    // Positionals: merge by name\n    if (source.positionals) {\n      result.positionals = mergeFields(result.positionals || [], source.positionals);\n    }\n\n    // Subcommands: merge recursively by name\n    if (source.subcommands) {\n      result.subcommands = mergeSubcommands(result.subcommands || [], source.subcommands);\n    }\n  }\n\n  // Clean up empty arrays\n  if (result.aliases?.length === 0) delete result.aliases;\n  if (result.examples?.length === 0) delete result.examples;\n  if (result.arguments?.length === 0) delete result.arguments;\n  if (result.positionals?.length === 0) delete result.positionals;\n  if (result.subcommands?.length === 0) delete result.subcommands;\n\n  return result;\n}\n\n/**\n * Merge two arrays of FieldMeta by name.\n * Later fields take precedence unless earlier was non-ambiguous.\n */\nfunction mergeFields(existing: FieldMeta[], incoming: FieldMeta[]): FieldMeta[] {\n  const map = new Map<string, FieldMeta>();\n\n  for (const field of existing) {\n    map.set(field.name, { ...field });\n  }\n\n  for (const field of incoming) {\n    const prev = map.get(field.name);\n    if (!prev) {\n      map.set(field.name, { ...field });\n      continue;\n    }\n\n    // Merge the fields\n    const merged: FieldMeta = { ...prev };\n\n    // Type: prefer non-ambiguous source\n    if (field.type !== 'unknown') {\n      if (prev.ambiguous || !field.ambiguous) {\n        merged.type = field.type;\n        merged.ambiguous = field.ambiguous;\n      }\n    }\n\n    // Description: last non-empty wins\n    if (field.description) {\n      merged.description = field.description;\n    }\n\n    // Default: last non-undefined wins\n    if (field.default !== undefined) {\n      merged.default = field.default;\n    }\n\n    // Required: last defined wins\n    if (field.required !== undefined) {\n      merged.required = field.required;\n    }\n\n    // Aliases: merge and deduplicate\n    if (field.aliases) {\n      merged.aliases = [...new Set([...(prev.aliases || []), ...field.aliases])];\n    }\n\n    // Enum values: merge and deduplicate\n    if (field.enumValues) {\n      merged.enumValues = [...new Set([...(prev.enumValues || []), ...field.enumValues])];\n    }\n\n    // Items: last non-empty wins\n    if (field.items) {\n      merged.items = field.items;\n    }\n\n    map.set(field.name, merged);\n  }\n\n  return [...map.values()];\n}\n\n/**\n * Merge two arrays of CommandMeta by name, recursively.\n */\nfunction mergeSubcommands(existing: CommandMeta[], incoming: CommandMeta[]): CommandMeta[] {\n  const map = new Map<string, CommandMeta>();\n\n  for (const cmd of existing) {\n    map.set(cmd.name, cmd);\n  }\n\n  for (const cmd of incoming) {\n    const prev = map.get(cmd.name);\n    if (!prev) {\n      map.set(cmd.name, cmd);\n    } else {\n      map.set(cmd.name, mergeCommandMeta(prev, cmd));\n    }\n  }\n\n  return [...map.values()];\n}\n"],"mappings":";;;;;;;;AASA,SAAgB,iBAAiB,GAAG,SAAqC;CACvE,IAAI,QAAQ,WAAW,GACrB,OAAO,EAAE,MAAM,GAAG;CAGpB,IAAI,QAAQ,WAAW,GACrB,OAAO,QAAQ;CAGjB,MAAM,SAAsB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