{"version":3,"file":"output-indicator.mjs","names":[],"sources":["../../src/output/output-indicator.ts"],"sourcesContent":["import type { KeyValueOptions, ListItem, ListOptions, TableOptions, TreeNode, TreeOptions } from './primitives.ts';\nimport { renderKeyValue, renderList, renderTable, renderTree } from './primitives.ts';\nimport type { OutputContext } from './styling.ts';\n\n/**\n * Runtime output helper injected into action context as `ctx.context.output`.\n * Provides format-aware output primitives (table, tree, list, key-value).\n *\n * Each method renders data using the resolved format (ANSI, text, JSON, markdown, HTML)\n * and writes it to the runtime's output function.\n */\nexport type PadroneOutputIndicator = {\n  /** Render data as a table. */\n  table(data: Record<string, unknown>[], options?: TableOptions): void;\n  /** Render data as a tree. */\n  tree(data: TreeNode | TreeNode[], options?: TreeOptions): void;\n  /** Render data as a list. */\n  list(data: ListItem[], options?: ListOptions): void;\n  /** Render data as aligned key-value pairs. */\n  kv(data: Record<string, unknown>, options?: KeyValueOptions): void;\n  /** Write raw output (same as runtime.output but sets the \"already called\" flag). */\n  raw(...args: unknown[]): void;\n  /** Whether any output method has been called. */\n  readonly called: boolean;\n};\n\n/** Declarative output configuration for a command. */\nexport type OutputPrimitiveType = 'table' | 'tree' | 'list' | 'kv' | 'json';\nexport type OutputConfig =\n  | OutputPrimitiveType\n  | { type: OutputPrimitiveType; options?: TableOptions | TreeOptions | ListOptions | KeyValueOptions };\n\n/** Create an output indicator that renders through the given output function and format context. */\nexport function createOutputIndicator(outputFn: (...args: unknown[]) => void, ctx: OutputContext): PadroneOutputIndicator {\n  let _called = false;\n\n  const emit = (rendered: string) => {\n    _called = true;\n    outputFn(rendered);\n  };\n\n  return {\n    table(data, options) {\n      emit(renderTable(data, options, ctx));\n    },\n    tree(data, options) {\n      emit(renderTree(data, options, ctx));\n    },\n    list(data, options) {\n      emit(renderList(data, options, ctx));\n    },\n    kv(data, options) {\n      emit(renderKeyValue(data, options, ctx));\n    },\n    raw(...args) {\n      _called = true;\n      outputFn(...args);\n    },\n    get called() {\n      return _called;\n    },\n  };\n}\n\n/** Format a return value using a declarative output config. */\nexport function formatDeclarativeOutput(value: unknown, config: OutputConfig, ctx: OutputContext): string | undefined {\n  const type = typeof config === 'string' ? config : config.type;\n  const options = typeof config === 'object' ? config.options : undefined;\n\n  switch (type) {\n    case 'table':\n      if (!Array.isArray(value)) return undefined;\n      return renderTable(value as Record<string, unknown>[], options as TableOptions | undefined, ctx);\n    case 'tree':\n      return renderTree(value as TreeNode | TreeNode[], options as TreeOptions | undefined, ctx);\n    case 'list':\n      if (!Array.isArray(value)) return undefined;\n      return renderList(value as ListItem[], options as ListOptions | undefined, ctx);\n    case 'kv':\n      if (typeof value !== 'object' || value === null || Array.isArray(value)) return undefined;\n      return renderKeyValue(value as Record<string, unknown>, options as KeyValueOptions | undefined, ctx);\n    case 'json':\n      return JSON.stringify(value, null, 2);\n    default:\n      return undefined;\n  }\n}\n"],"mappings":";;;AAiCA,SAAgB,sBAAsB,UAAwC,KAA4C;CACxH,IAAI,UAAU;CAEd,MAAM,QAAQ,aAAqB;EACjC,UAAU;EACV,SAAS,QAAQ;CACnB;CAEA,OAAO;EACL,MAAM,MAAM,SAAS;GACnB,KAAK,YAAY,MAAM,SAAS,GAAG,CAAC;EACtC;EACA,KAAK,MAAM,SAAS;GAClB,KAAK,WAAW,MAAM,SAAS,GAAG,CAAC;EACrC;EACA,KAAK,MAAM,SAAS;GAClB,KAAK,WAAW,MAAM,SAAS,GAAG,CAAC;EACrC;EACA,GAAG,MAAM,SAAS;GAChB,KAAK,eAAe,MAAM,SAAS,GAAG,CAAC;EACzC;EACA,IAAI,GAAG,MAAM;GACX,UAAU;GACV,SAAS,GAAG,IAAI;EAClB;EACA,IAAI,SAAS;GACX,OAAO;EACT;CACF;AACF;;AAGA,SAAgB,wBAAwB,OAAgB,QAAsB,KAAwC;CACpH,MAAM,OAAO,OAAO,WAAW,WAAW,SAAS,OAAO;CAC1D,MAAM,UAAU,OAAO,WAAW,WAAW,OAAO,UAAU,KAAA;CAE9D,QAAQ,MAAR;EACE,KAAK;GACH,IAAI,CAAC,MAAM,QAAQ,KAAK,GAAG,OAAO,KAAA;GAClC,OAAO,YAAY,OAAoC,SAAqC,GAAG;EACjG,KAAK,QACH,OAAO,WAAW,OAAgC,SAAoC,GAAG;EAC3F,KAAK;GACH,IAAI,CAAC,MAAM,QAAQ,KAAK,GAAG,OAAO,KAAA;GAClC,OAAO,WAAW,OAAqB,SAAoC,GAAG;EAChF,KAAK;GACH,IAAI,OAAO,UAAU,YAAY,UAAU,QAAQ,MAAM,QAAQ,KAAK,GAAG,OAAO,KAAA;GAChF,OAAO,eAAe,OAAkC,SAAwC,GAAG;EACrG,KAAK,QACH,OAAO,KAAK,UAAU,OAAO,MAAM,CAAC;EACtC,SACE;CACJ;AACF"}