/**
 * OpenAPI Extractors
 * 
 * Ported from Hoppscotch's openapi/index.ts
 * Converts fp-ts functional programming to native TypeScript
 */ /* eslint-disable @typescript-eslint/no-unused-vars */ import { isNumeric, getStatusCodeReasonPhrase } from '../../../utils';
// Known content types (from Hoppscotch)
const knownContentTypes = {
    'application/json': true,
    'application/xml': true,
    'text/plain': true,
    'text/html': true,
    'application/x-www-form-urlencoded': true,
    'multipart/form-data': true,
    'application/octet-stream': true
};
/**
 * Replaces OpenAPI path templating ({id}) with Brainfish templating (<<id>>)
 */ export function replaceOpenApiPathTemplating(path) {
    return path.replace(/{/g, '<<').replace(/}/g, '>>');
}
/**
 * Parses OpenAPI query parameters
 */ export function parseOpenAPIParams(params) {
    return params.filter((param)=>param.in === 'query').map((param)=>({
            key: param.name,
            value: '',
            active: true,
            description: param.description ?? ''
        }));
}
/**
 * Parses OpenAPI path variables
 */ export function parseOpenAPIVariables(variables) {
    return variables.filter((param)=>param.in === 'path').map((param)=>({
            key: param.name,
            value: '',
            active: true
        }));
}
/**
 * Parses OpenAPI header parameters
 */ export function parseOpenAPIHeaders(params) {
    return params.filter((param)=>param.in === 'header').map((header)=>({
            key: header.name,
            value: '',
            active: true,
            description: header.description ?? ''
        }));
}
/**
 * Parses OpenAPI v3 responses
 */ export function parseOpenAPIV3Responses(op, originalRequest) {
    const responses = op.responses;
    if (!responses) return {};
    const res = {};
    for (const [key, value] of Object.entries(responses)){
        const response = value;
        const contentType = Object.keys(response.content ?? {})[0];
        const body = response.content?.[contentType];
        const name = response.description ?? key;
        const code = isNumeric(key) ? Number(key) : 200;
        const status = getStatusCodeReasonPhrase(code);
        const headers = [
            {
                key: 'content-type',
                value: contentType ?? 'application/json',
                description: '',
                active: true
            }
        ];
        let stringifiedBody = '';
        try {
            stringifiedBody = JSON.stringify(body ?? '');
        } catch (e) {
        // Ignore circular reference errors
        }
        res[name] = {
            name,
            status,
            code,
            headers,
            body: stringifiedBody,
            originalRequest
        };
    }
    return res;
}
/**
 * Parses OpenAPI v2 responses
 */ export function parseOpenAPIV2Responses(op, originalRequest) {
    const responses = op.responses;
    if (!responses) return {};
    const res = {};
    for (const [key, value] of Object.entries(responses)){
        const response = value;
        const contentType = Object.keys(response.examples ?? {})[0];
        const body = response.examples?.[contentType];
        const name = response.description ?? key;
        const code = isNumeric(Number(key)) ? Number(key) : 200;
        const status = getStatusCodeReasonPhrase(code);
        const headers = [
            {
                key: 'content-type',
                value: contentType ?? 'application/json',
                description: '',
                active: true
            }
        ];
        res[name] = {
            name,
            status,
            code,
            headers,
            body: body ?? '',
            originalRequest
        };
    }
    return res;
}
/**
 * Parses OpenAPI responses (v2 or v3)
 */ export function parseOpenAPIResponses(doc, op, originalRequest) {
    return isOpenAPIV3Operation(doc, op) ? parseOpenAPIV3Responses(op, originalRequest) : parseOpenAPIV2Responses(op, originalRequest);
}
/**
 * Checks if an operation is OpenAPI v3
 */ export function isOpenAPIV3Operation(doc, op) {
    return 'openapi' in doc && typeof doc.openapi === 'string' && doc.openapi.startsWith('3.');
}
// Export knownContentTypes for use in body parsers
export { knownContentTypes };
