import { isNonNullish } from "@fern-api/ui-core-utils";
import { v4 } from "uuid";
import { FernRegistry } from "../../client/generated";
import { ComponentsConverterNode } from "../../openapi/3.1/schemas/ComponentsConverter.node";
import { coalesceServers } from "../../openapi/utils/3.1/coalesceServers";
import { computeSubpackages } from "../../utils/computeSubpackages";
import { BaseOpenrpcConverterNode, } from "../BaseOpenrpcConverter.node";
import { resolveMethodReference } from "../utils/resolveMethodReference";
import { MethodConverterNode } from "./MethodConverter.node";
export class OpenrpcDocumentConverterNode extends BaseOpenrpcConverterNode {
    servers = [];
    methods = [];
    components;
    constructor(args) {
        super(args);
        this.safeParse();
    }
    parse() {
        if (this.context.openrpc.servers != null) {
            this.servers = coalesceServers(this.servers, this.input.servers, this.context, this.accessPath);
        }
        if (this.input.methods != null) {
            for (const method of this.input.methods) {
                const resolvedMethod = resolveMethodReference(method, this.context.openrpc);
                if (resolvedMethod == null) {
                    continue;
                }
                this.methods.push(new MethodConverterNode({
                    input: resolvedMethod,
                    context: this.context,
                    accessPath: this.accessPath,
                    pathId: "methods",
                }, this.servers));
            }
        }
        if (this.context.openrpc.components != null) {
            this.components = new ComponentsConverterNode({
                input: this.context.openrpc.components,
                context: this.context,
                accessPath: this.accessPath,
                pathId: "components",
            });
        }
    }
    convert() {
        const apiDefinitionId = v4();
        const { types } = this.components?.convert() ?? {};
        const methods = this.methods
            ?.map((method) => {
            return method.convert();
        })
            .filter(isNonNullish);
        const endpoints = Object.fromEntries(methods?.map((method) => [method.id, method]) ?? []);
        const subpackages = computeSubpackages({ endpoints });
        return {
            id: FernRegistry.ApiDefinitionId(apiDefinitionId),
            types: types ?? {},
            endpoints,
            websockets: {},
            webhooks: {},
            subpackages,
            auths: {},
            globalHeaders: [],
        };
    }
}
