{"version":3,"file":"flags.mjs","names":[],"sources":["../../src/cli/flags.ts"],"sourcesContent":["import { globalRegistry } from \"zod\";\nimport { commandOptionSchemas } from \"../contract\";\nimport type { CommandDescriptor } from \"./catalog\";\n\ntype SchemaLike = {\n  def?: {\n    type?: string;\n    innerType?: SchemaLike;\n    shape?: Record<string, SchemaLike>;\n    values?: Record<string, string> | string[];\n    defaultValue?: unknown;\n  };\n};\n\n/** The flag description is whatever `.describe()` attached along the\n * wrapper chain — check each layer's registry entry. */\nexport function describeOf(schema: SchemaLike): string | undefined {\n  let current = schema;\n  while (true) {\n    const registered = globalRegistry.get(current as never)?.description;\n    if (registered) return registered;\n    const inner = current.def?.innerType;\n    if (!inner) break;\n    current = inner;\n  }\n  return undefined;\n}\n\nfunction unwrap(schema: SchemaLike): SchemaLike {\n  let current = schema;\n  while (true) {\n    const type = current.def?.type;\n    if (type === \"default\" || type === \"optional\" || type === \"nullable\" || type === \"nullish\") {\n      const inner = current.def?.innerType;\n      if (!inner) break;\n      current = inner;\n      continue;\n    }\n    return current;\n  }\n  return current;\n}\n\nfunction toFlagName(field: string): string {\n  return `--${field.replace(/([a-z0-9])([A-Z])/g, \"$1-$2\").toLowerCase()}`;\n}\n\nfunction defaultValueOf(schema: SchemaLike): unknown {\n  let current = schema;\n  while (true) {\n    const value = current.def?.defaultValue;\n    if (value !== undefined) return value;\n    const inner = current.def?.innerType;\n    if (!inner) break;\n    current = inner;\n  }\n  return undefined;\n}\n\n/** Flag help derived from the contract schemas (ticket #426) — the schema is\n * the single flag source; there is no parallel description table. */\nexport function flagHelpLines(descriptor: CommandDescriptor): string[] {\n  const schema = commandOptionSchemas[descriptor.key as keyof typeof commandOptionSchemas];\n  const shape = unwrap(schema as SchemaLike).def?.shape;\n  if (!shape) return [];\n\n  const lines: string[] = [];\n  for (const [fieldName, fieldSchema] of Object.entries(shape)) {\n    const flag = toFlagName(fieldName);\n    const parts: string[] = [flag];\n    const description = describeOf(fieldSchema as SchemaLike);\n    if (unwrap(fieldSchema as SchemaLike).def?.type === \"boolean\") {\n      parts.push(\"(boolean)\");\n    }\n    if (description) parts.push(description);\n    const defaultValue = defaultValueOf(fieldSchema as SchemaLike);\n    if (defaultValue !== undefined && typeof defaultValue !== \"function\") {\n      parts.push(`default: ${String(defaultValue)}`);\n    }\n    lines.push(`    ${parts.join(\"  \")}`);\n  }\n  return lines;\n}\n"],"mappings":";;;;;;AAgBA,SAAgB,WAAW,QAAwC;CACjE,IAAI,UAAU;CACd,OAAO,MAAM;EACX,MAAM,aAAa,eAAe,IAAI,OAAgB,CAAC,EAAE;EACzD,IAAI,YAAY,OAAO;EACvB,MAAM,QAAQ,QAAQ,KAAK;EAC3B,IAAI,CAAC,OAAO;EACZ,UAAU;CACZ;AAEF;AAEA,SAAS,OAAO,QAAgC;CAC9C,IAAI,UAAU;CACd,OAAO,MAAM;EACX,MAAM,OAAO,QAAQ,KAAK;EAC1B,IAAI,SAAS,aAAa,SAAS,cAAc,SAAS,cAAc,SAAS,WAAW;GAC1F,MAAM,QAAQ,QAAQ,KAAK;GAC3B,IAAI,CAAC,OAAO;GACZ,UAAU;GACV;EACF;EACA,OAAO;CACT;CACA,OAAO;AACT;AAEA,SAAS,WAAW,OAAuB;CACzC,OAAO,KAAK,MAAM,QAAQ,sBAAsB,OAAO,CAAC,CAAC,YAAY;AACvE;AAEA,SAAS,eAAe,QAA6B;CACnD,IAAI,UAAU;CACd,OAAO,MAAM;EACX,MAAM,QAAQ,QAAQ,KAAK;EAC3B,IAAI,UAAU,QAAW,OAAO;EAChC,MAAM,QAAQ,QAAQ,KAAK;EAC3B,IAAI,CAAC,OAAO;EACZ,UAAU;CACZ;AAEF;;;AAIA,SAAgB,cAAc,YAAyC;CACrE,MAAM,SAAS,qBAAqB,WAAW;CAC/C,MAAM,QAAQ,OAAO,MAAoB,CAAC,CAAC,KAAK;CAChD,IAAI,CAAC,OAAO,OAAO,CAAC;CAEpB,MAAM,QAAkB,CAAC;CACzB,KAAK,MAAM,CAA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