import { getValueFromMdParsedExtension } from '../utils/index.js';
export function getSecurity(requirements = [], parser) {
    return requirements
        .map((requirement) => getSecurityRequirement({ requirement, parser }))
        .filter(({ schemes }) => schemes.length > 0);
}
function getSecurityRequirement({ requirement, parser, }) {
    const schemes = (parser.definition.components && parser.definition.components.securitySchemes) || {};
    const filteredSchemes = Object.keys(requirement || {})
        .map((id) => {
        const scheme = parser.deref(schemes[id]).resolved;
        const scopes = requirement[id] || [];
        if (!scheme) {
            return undefined;
        }
        return {
            ...scheme,
            id,
            sectionId: id,
            scopes,
        };
    })
        .filter(Boolean);
    return {
        schemes: filteredSchemes,
    };
}
export function getSecurityScheme(parser, id, scheme) {
    if (!parser?.deref) {
        return {};
    }
    const { resolved: info } = parser.deref(scheme);
    const sectionId = id;
    const type = info.type;
    const description = getValueFromMdParsedExtension(info, 'description') || '';
    let apiKey = undefined;
    let http = undefined;
    let openId = undefined;
    let flows = {};
    switch (info.type) {
        case 'apiKey': {
            apiKey = {
                name: info.name,
                in: info.in,
            };
            break;
        }
        case 'http': {
            http = {
                scheme: info.scheme,
                bearerFormat: info.bearerFormat,
            };
            break;
        }
        case 'openIdConnect': {
            openId = {
                connectUrl: info.openIdConnectUrl,
            };
            break;
        }
        case 'oauth2': {
            flows = info.flows;
            break;
        }
    }
    return {
        id,
        sectionId,
        type,
        description,
        apiKey,
        http,
        flows,
        openId,
    };
}
export function getSecuritySchemes({ parser, }) {
    if (!parser) {
        return [];
    }
    const schemes = parser.definition?.components?.securitySchemes || {};
    return Object.keys(schemes).map((name) => getSecurityScheme(parser, name, schemes[name]));
}
//# sourceMappingURL=security.js.map