import { isNonNullish } from "@fern-api/ui-core-utils";
import { FernRegistry } from "../../../client/generated";
import { BaseOpenApiV3_1ConverterNode, } from "../../BaseOpenApiV3_1Converter.node";
import { coalesceServers } from "../../utils/3.1/coalesceServers";
import { isWebhookDefinition } from "../guards/isWebhookDefinition";
import { PathItemObjectConverterNode } from "./PathItemObjectConverter.node";
export class PathsObjectConverterNode extends BaseOpenApiV3_1ConverterNode {
    servers;
    globalAuth;
    basePath;
    paths;
    constructor(args, servers, globalAuth, basePath) {
        super(args);
        this.servers = servers;
        this.globalAuth = globalAuth;
        this.basePath = basePath;
        this.safeParse();
    }
    parse() {
        this.paths = Object.entries(this.input)
            .map(([path, pathItem]) => {
            if (pathItem == null) {
                return undefined;
            }
            return new PathItemObjectConverterNode({
                input: pathItem,
                context: this.context,
                accessPath: this.accessPath,
                pathId: path,
            }, coalesceServers(this.servers, pathItem.servers, this.context, this.accessPath), this.globalAuth, this.basePath, undefined);
        })
            .filter(isNonNullish);
    }
    convert() {
        if (this.paths == null) {
            return undefined;
        }
        const endpoints = {};
        const webhookEndpoints = {};
        this.paths.forEach((pathItem) => {
            const pathItemDefinitions = pathItem.convert();
            if (pathItemDefinitions == null) {
                return undefined;
            }
            pathItemDefinitions.forEach((definition) => {
                if (isWebhookDefinition(definition)) {
                    webhookEndpoints[FernRegistry.WebhookId(definition.id)] = definition;
                }
                else {
                    endpoints[FernRegistry.EndpointId(definition.id)] = definition;
                }
            });
        });
        return {
            endpoints,
            webhookEndpoints,
        };
    }
}
