{"version":3,"file":"composable-builder.mjs","names":[],"sources":["../../src/lib/composable-builder.ts"],"sourcesContent":["import type { Expand, ParsedArgs } from '@cli-forge/parser';\nimport { CLI } from './public-api';\n\n/**\n * Extracts the TChildren type parameter from a CLI type.\n */\nexport type ExtractChildren<T> = T extends CLI<any, any, infer C, any, any>\n  ? C\n  : never;\n\n/**\n * Extracts the TArgs type parameter from a CLI type.\n */\nexport type ExtractArgs<T> = T extends CLI<infer A, any, any, any, any> ? A : never;\n\n/**\n * Type for a composable builder function that transforms a CLI.\n * Used with `chain` to compose multiple builders.\n */\nexport type ComposableBuilder<\n  TArgs2 extends ParsedArgs,\n  TAddedChildren = {},\n  TAddedProviders = {}\n> = <TInit extends ParsedArgs, THandlerReturn, TChildren, TParent, TProviders>(\n  init: CLI<TInit, THandlerReturn, TChildren, TParent, TProviders>\n) => CLI<Expand<TInit & TArgs2>, THandlerReturn, TChildren & TAddedChildren, TParent, TProviders & TAddedProviders>;\n\n/**\n * Creates a composable builder function that can be used with `chain`.\n * Can be used to add options, commands, or any other CLI modifications.\n * Children added by the builder function are properly tracked in the type.\n *\n * The builder function runs once at creation time against a recording Proxy.\n * Subsequent applications replay the captured operations, ensuring inline\n * middleware closures have stable references for Set-based deduplication.\n *\n * @typeParam TArgs2 - The args type after the builder runs\n * @typeParam TChildren2 - The children type added by the builder\n * @typeParam TProviders2 - The providers type added by the builder\n */\nexport function makeComposableBuilder<\n  TArgs2 extends ParsedArgs,\n  TChildren2 = {},\n  TProviders2 = {}\n>(\n  fn: (\n    init: CLI<ParsedArgs, any, {}, any, {}>\n  ) => CLI<TArgs2, any, TChildren2, any, TProviders2>\n) {\n  // Run builder once against a recording proxy to capture operations.\n  // Replaying these ensures inline closures (e.g. middleware) keep stable\n  // references across applications, enabling Set-based deduplication.\n  const operations: { method: string; args: any[] }[] = [];\n  const proxy = new Proxy({} as CLI, {\n    get(_target, prop) {\n      return (...args: any[]) => {\n        operations.push({ method: prop as string, args });\n        return proxy;\n      };\n    },\n  });\n  fn(proxy);\n\n  return <TInit extends ParsedArgs, THandlerReturn, TChildren, TParent, TProviders>(\n    init: CLI<TInit, THandlerReturn, TChildren, TParent, TProviders>\n  ) => {\n    let current: any = init;\n    for (const op of operations) {\n      current = current[op.method](...op.args);\n    }\n    return current as unknown as CLI<\n      Expand<TInit & TArgs2>,\n      THandlerReturn,\n      TChildren & TChildren2,\n      TParent,\n      TProviders & TProviders2\n    >;\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;AAwCA,SAAgB,sBAKd,IAGA;CAIA,MAAM,aAAgD,EAAE;CACxD,MAAM,QAAQ,IAAI,MAAM,EAAE,EAAS,EACjC,IAAI,SAAS,MAAM;AACjB,UAAQ,GAAG,SAAgB;AACzB,cAAW,KAAK;IAAE,QAAQ;IAAgB;IAAM,CAAC;AACjD,UAAO;;IAGZ,CAAC;AACF,IAAG,MAAM;AAET,SACE,SACG;EACH,IAAI,UAAe;AACnB,OAAK,MAAM,MAAM,WACf,WAAU,QAAQ,GAAG,QAAQ,GAAG,GAAG,KAAK;AAE1C,SAAO"}