import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core'; import { ref } from '@hey-api/codegen-core'; import type { py } from '../../ts-python'; import type { MaybePyDsl } from '../base'; import type { BaseCtor, MixinCtor } from './types'; type Arg = NodeName | MaybePyDsl; export interface ArgsMethods extends Node { $args(): ReadonlyArray; arg(arg: Arg | undefined): this; args(...args: ReadonlyArray): this; } export function ArgsMixin>(Base: TBase) { abstract class Args extends Base { protected _args: Array> = []; override analyze(ctx: AnalysisContext): void { super.analyze(ctx); for (const arg of this._args) { ctx.analyze(arg); } } protected arg(arg: Arg | undefined): this { if (arg !== undefined) this._args.push(ref(arg)); return this; } protected args(...args: ReadonlyArray): this { this._args.push( ...args .filter((a): a is NonNullable => a !== undefined) .map((a) => ref(a as Arg)), ); return this; } protected $args(): ReadonlyArray { return this.$node(this._args).map((arg) => this.$node(arg) as py.Expression); } } return Args as unknown as MixinCtor; }