import { isNonNullish } from "@fern-api/ui-core-utils";
import { BaseOpenApiV3_1ConverterNodeWithTracking, } from "../../BaseOpenApiV3_1Converter.node";
import { maybeSingleValueToArray } from "../../utils/maybeSingleValueToArray";
import { wrapNullable } from "../../utils/wrapNullable";
import { SchemaConverterNode } from "./SchemaConverter.node";
export class MixedSchemaConverterNode extends BaseOpenApiV3_1ConverterNodeWithTracking {
    typeNodes;
    nullable;
    schemaName;
    constructor(args) {
        super(args);
        this.schemaName = args.schemaName;
        this.parse();
    }
    parse() {
        this.typeNodes = this.input
            .map((type) => {
            if (type.type === "null") {
                this.nullable = true;
            }
            else {
                return new SchemaConverterNode({
                    input: type,
                    context: this.context,
                    accessPath: this.accessPath,
                    pathId: this.pathId,
                    seenSchemas: this.seenSchemas,
                    nullable: this.nullable,
                    schemaName: this.schemaName,
                });
            }
            return undefined;
        })
            .filter(isNonNullish);
    }
    convert() {
        if (this.typeNodes == null) {
            return undefined;
        }
        const concreteTypeNodes = this.typeNodes
            .map((typeNode) => {
            const maybeShapes = maybeSingleValueToArray(typeNode.convert());
            return maybeShapes?.map((shape) => ({
                displayName: typeNode.name,
                shape,
                description: typeNode.description,
                availability: typeNode.availability?.convert(),
            }));
        })
            .filter(isNonNullish);
        const concreteTypeNodePermutations = concreteTypeNodes.reduce((acc, curr) => {
            return acc.flatMap((acc) => curr.length > 0 ? curr.map((c) => [...acc, c]) : [[...acc]]);
        }, [[]]);
        const unions = concreteTypeNodePermutations.map((variants) => ({
            type: "undiscriminatedUnion",
            variants,
        }));
        return this.nullable
            ? unions.map(wrapNullable).filter(isNonNullish)
            : unions;
    }
    example(exampleArgs) {
        return this.nullable ? null : this.typeNodes?.[0]?.example(exampleArgs);
    }
}
