import type { PothosUnionTypeConfig, SchemaTypes } from '../types'; import { type ObjectParam, type OutputRef, outputShapeKey, parentShapeKey, } from '../types/type-params'; import { BaseTypeRef } from './base'; export class UnionRef extends BaseTypeRef implements OutputRef, PothosSchemaTypes.UnionRef { override kind = 'Union' as const; $inferType!: T; [outputShapeKey]!: T; [parentShapeKey]!: P; private types: (() => ObjectParam[])[] = []; constructor(name: string, config?: PothosUnionTypeConfig) { super('Union', name, config); } addTypes(types: ObjectParam[] | (() => ObjectParam[])) { if (Array.isArray(types) && types.length === 0) { return; } if (this.preparedForBuild) { this.updateConfig((cfg) => ({ ...cfg, types: [ ...cfg.types, ...(typeof types === 'function' ? types() : types), ] as ObjectParam[], })); } else { this.types.push(() => (Array.isArray(types) ? types : types())); } } override prepareForBuild(): void { if (this.preparedForBuild) { return; } super.prepareForBuild(); if (this.types.length > 0) { this.updateConfig((cfg) => ({ ...cfg, types: [ ...cfg.types, ...this.types.flatMap((types) => types()), ] as ObjectParam[], })); } } }