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 {
    paths;
    constructor(args) {
        super(args);
        this.safeParse(args);
    }
    parse({ servers, globalAuth, basePath, }) {
        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,
                servers: coalesceServers(servers, pathItem.servers, this.context, this.accessPath),
                globalAuth,
                basePath,
                isWebhook: 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,
        };
    }
}
