import { FernRegistry } from "../../../client/generated";
import { BaseOpenApiV3_1ConverterNodeWithTracking, } from "../../BaseOpenApiV3_1Converter.node";
import { getSchemaIdFromReference } from "../../utils/3.1/getSchemaIdFromReference";
import { resolveSchemaReference } from "../../utils/3.1/resolveSchemaReference";
import { isNonArraySchema } from "../guards/isNonArraySchema";
import { SchemaConverterNode } from "./SchemaConverter.node";
import { EnumConverterNode } from "./primitives/EnumConverter.node";
export class ReferenceConverterNode extends BaseOpenApiV3_1ConverterNodeWithTracking {
    description;
    availability;
    schemaId;
    maybeEnumConverterNode;
    nullable;
    schemaName;
    constructor(args, description, availability) {
        super(args);
        this.description = description;
        this.availability = availability;
        this.nullable = args.nullable;
        this.schemaName = args.schemaName;
        this.safeParse();
    }
    parse() {
        this.schemaId = getSchemaIdFromReference(this.input);
        const maybeEnum = resolveSchemaReference(this.input, this.context.document);
        if (maybeEnum?.enum != null && isNonArraySchema(maybeEnum)) {
            this.maybeEnumConverterNode = new EnumConverterNode({
                input: maybeEnum,
                context: this.context,
                accessPath: this.accessPath,
                pathId: this.schemaId ?? "",
                nullable: this.nullable,
            });
        }
        if (this.schemaId == null) {
            this.context.errors.error({
                message: `Unprocessable reference: ${this.input.$ref}`,
                path: this.accessPath,
            });
        }
    }
    convert() {
        if (this.schemaId == null) {
            return undefined;
        }
        return {
            type: "alias",
            value: {
                type: "id",
                id: FernRegistry.TypeId(this.schemaId),
                default: this.maybeEnumConverterNode?.default != null
                    ? {
                        type: "enum",
                        value: this.maybeEnumConverterNode.default,
                    }
                    : undefined,
            },
        };
    }
    example(exampleArgs) {
        const schema = resolveSchemaReference(this.input, this.context.document);
        if (schema == null) {
            return undefined;
        }
        return new SchemaConverterNode({
            input: schema,
            context: this.context,
            accessPath: this.accessPath,
            pathId: this.schemaId ?? "",
            seenSchemas: this.seenSchemas,
            nullable: this.nullable,
            schemaName: this.schemaName,
        }).example(exampleArgs);
    }
}
