import { isNonNullish } from "@fern-api/ui-core-utils";
import { FernRegistry } from "../../../client/generated";
import { BaseOpenApiV3_1ConverterNode, } from "../../BaseOpenApiV3_1Converter.node";
import { resolveWebhookReference } from "../../utils/3.1/resolveWebhookReference";
import { isWebhookDefinition } from "../guards/isWebhookDefinition";
import { PathItemObjectConverterNode } from "./PathItemObjectConverter.node";
export class WebhooksObjectConverterNode extends BaseOpenApiV3_1ConverterNode {
    webhooks;
    constructor(args) {
        super(args);
        this.safeParse(args);
    }
    parse({ servers, globalAuth, basePath, }) {
        this.webhooks = Object.entries(this.input ?? {})
            .map(([operation, operationItem]) => {
            const resolvedOperationItem = resolveWebhookReference(operationItem, this.context.document);
            if (resolvedOperationItem == null) {
                return undefined;
            }
            return new PathItemObjectConverterNode({
                input: resolvedOperationItem,
                context: this.context,
                accessPath: this.accessPath,
                pathId: operation,
                servers,
                globalAuth,
                basePath,
                isWebhook: true,
            });
        })
            .filter(isNonNullish);
    }
    convert() {
        return this.webhooks?.reduce((acc, webhook) => {
            webhook.convert()?.forEach((convertedWebhook) => {
                if (isWebhookDefinition(convertedWebhook)) {
                    acc[FernRegistry.WebhookId(convertedWebhook.id)] = convertedWebhook;
                }
            });
            return acc;
        }, {});
    }
}
