import { isNonNullish } from "@fern-api/ui-core-utils";
import { FernRegistry } from "../../../client/generated";
import { BaseOpenApiV3_1ConverterNode, } from "../../BaseOpenApiV3_1Converter.node";
import { maybeSingleValueToArray } from "../../utils/maybeSingleValueToArray";
import { SchemaConverterNode } from "./SchemaConverter.node";
export class ComponentsConverterNode extends BaseOpenApiV3_1ConverterNode {
    typeSchemas;
    constructor(args) {
        super(args);
        this.safeParse();
    }
    parse() {
        if (this.input.schemas != null) {
            this.typeSchemas = Object.fromEntries(Object.entries(this.input.schemas).map(([key, value]) => {
                return [
                    key,
                    new SchemaConverterNode({
                        input: value,
                        context: this.context,
                        accessPath: this.accessPath,
                        pathId: key,
                        seenSchemas: new Set(),
                    }),
                ];
            }));
        }
    }
    convert() {
        if (this.typeSchemas == null) {
            return undefined;
        }
        return Object.fromEntries(Object.entries(this.typeSchemas)
            .map(([key, value]) => {
            const name = value.name ?? key;
            const maybeShapes = maybeSingleValueToArray(value.convert());
            if (maybeShapes == null) {
                return [key, undefined];
            }
            return [
                FernRegistry.TypeId(key),
                {
                    name,
                    shape: maybeShapes[0],
                    description: value.description,
                    availability: undefined,
                },
            ];
        })
            .filter(([_, value]) => isNonNullish(value)));
    }
}
