import { type FieldKind, outputFieldShapeKey, type PothosOutputFieldConfig, type PothosTypeConfig, type SchemaTypes, } from '../types'; export class FieldRef implements PothosSchemaTypes.FieldRef { kind: FieldKind; fieldName?: string; $inferType!: T; [outputFieldShapeKey]!: T; protected pendingActions: (( config: PothosOutputFieldConfig, ) => PothosOutputFieldConfig | undefined)[] = []; private initConfig: ( name: string, typeConfig: PothosTypeConfig, ) => PothosOutputFieldConfig; private onUseCallbacks = new Set<(config: PothosOutputFieldConfig) => void>(); constructor( kind: Kind, initConfig: (name: string, typeConfig: PothosTypeConfig) => PothosOutputFieldConfig, ) { this.kind = kind; this.initConfig = initConfig; } updateConfig( cb: (config: PothosOutputFieldConfig) => PothosOutputFieldConfig | undefined, ) { this.pendingActions.push(cb); } getConfig(name: string, typeConfig: PothosTypeConfig): PothosOutputFieldConfig { const config = this.pendingActions.reduce( (cfg, cb) => cb(cfg) ?? cfg, this.initConfig(name, typeConfig), ); for (const cb of this.onUseCallbacks) { this.onUseCallbacks.delete(cb); cb(config); } return config; } onFirstUse(cb: (config: PothosOutputFieldConfig) => void) { this.onUseCallbacks.add(cb); } }