import { isNonNullish } from "@fern-api/ui-core-utils";
import { FernRegistry } from "../../../client/generated";
import { BaseOpenApiV3_1ConverterNode, } from "../../BaseOpenApiV3_1Converter.node";
import { extendType } from "../../utils/extendType";
import { maybeSingleValueToArray } from "../../utils/maybeSingleValueToArray";
import { SchemaConverterNode } from "../schemas";
import { X_FERN_GLOBAL_HEADERS } from "./fernExtension.consts";
export class XFernGlobalHeadersConverterNode extends BaseOpenApiV3_1ConverterNode {
    globalHeaders;
    constructor(args) {
        super(args);
        this.safeParse();
    }
    // This would be used to set a member on the node
    parse() {
        this.globalHeaders = extendType(this.input)[X_FERN_GLOBAL_HEADERS]?.map((header) => {
            const { header: headerName, ...schema } = header;
            return [
                headerName,
                new SchemaConverterNode({
                    input: schema,
                    context: this.context,
                    accessPath: this.accessPath,
                    pathId: this.pathId,
                    seenSchemas: new Set(),
                }),
            ];
        });
    }
    convert() {
        return this.globalHeaders
            ?.flatMap(([headerName, headerSchema]) => {
            const convertedSchema = maybeSingleValueToArray(headerSchema.convert());
            return convertedSchema?.map((schema) => ({
                key: FernRegistry.PropertyKey(headerName),
                valueShape: schema,
                description: headerSchema.description,
                availability: headerSchema.availability?.convert(),
            }));
        })
            .filter(isNonNullish);
    }
}
