import { isNonNullish } from "@fern-api/ui-core-utils";
import { BaseOpenApiV3_1ConverterNode, } from "../../BaseOpenApiV3_1Converter.node";
import { resolveSecurityScheme } from "../../utils/3.1/resolveSecurityScheme";
import { SecuritySchemeConverterNode } from "./SecuritySchemeConverter.node";
export class SecurityRequirementObjectConverterNode extends BaseOpenApiV3_1ConverterNode {
    authNodesMap;
    constructor(args) {
        super(args);
        this.safeParse();
    }
    parse() {
        this.input.map((securityScheme, index) => Object.keys(securityScheme).map((key) => {
            const resolvedSecurityScheme = resolveSecurityScheme(key, this.context.document);
            if (resolvedSecurityScheme == null) {
                this.context.errors.warning({
                    message: `No auth scheme found for ${key}. Inline security schemes are not supported.`,
                    path: this.accessPath,
                });
                return;
            }
            const resolvedAuthScheme = new SecuritySchemeConverterNode({
                input: resolvedSecurityScheme,
                context: this.context,
                accessPath: this.accessPath,
                pathId: ["security", `${index}`],
            });
            if (resolvedAuthScheme.convert() != null) {
                this.authNodesMap ??= {};
                this.authNodesMap[key] = resolvedAuthScheme;
            }
            else {
                this.context.errors.warning({
                    message: `No auth scheme found for ${key}`,
                    path: this.accessPath,
                });
            }
        }));
    }
    convert() {
        return Object.fromEntries(Object.entries(this.authNodesMap ?? {})
            .map(([key, value]) => {
            const convertedValue = value.convert();
            if (convertedValue == null) {
                return undefined;
            }
            return [key, convertedValue];
        })
            .filter(isNonNullish));
    }
}
