"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleHTTPAPIStructure = void 0;
const lodash_1 = __importDefault(require("lodash"));
const handleHTTPAPIStructure = (providerConfig, label) => {
    if (!providerConfig || typeof providerConfig !== 'object') {
        return {
            id: 'http',
            label,
            config: {},
        };
    }
    // Handle raw HTTP request case
    let customHTTPProvider;
    if (providerConfig?.response_type === 'raw HTPP request') {
        customHTTPProvider = {
            id: 'http',
            config: {
                useHttps: providerConfig?.useHttps === 'false' ? false : true,
                request: lodash_1.default.isEmpty(providerConfig?.request) === false ? providerConfig?.request : providerConfig?.raw_config,
                transformResponse: providerConfig?.transformResponse,
            },
        };
    }
    else {
        // Handle regular HTTP API case
        // Check if any json_form_config field has type 'file'
        const hasFileField = providerConfig?.json_form_config?.some((field) => field?.type === 'file');
        const headers = {
            'Content-Type': hasFileField
                ? 'multipart/form-data'
                : providerConfig?.response_type === 'formData'
                    ? 'application/x-www-form-urlencoded'
                    : 'application/json',
        };
        // Handle authentication
        switch (providerConfig.auth) {
            case 'api_key':
                headers[providerConfig?.apiKey?.key] = `Bearer ${providerConfig?.apiKey?.value}`;
                break;
            case 'basic':
                headers['Authorization'] =
                    `Basic ${btoa(`${providerConfig.username}:${providerConfig.password}`)}`;
                break;
            case 'custom':
                // Add custom fields as headers
                providerConfig.customFields?.forEach((field) => {
                    headers[field?.key] = field?.value;
                });
                break;
        }
        // Add JSON form config as request body
        const body = providerConfig.json_form_config?.reduce((acc, field) => {
            acc[field.key] = field.value;
            return acc;
        }, {});
        customHTTPProvider = {
            id: 'http',
            label,
            config: {
                url: providerConfig?.url, // Must include http:// or https:// prefix
                method: providerConfig?.method,
                headers,
                body: providerConfig?.response_type === 'formData'
                    ? Object.entries(body || {})
                        .map(([key, value]) => `${key}=${value}`)
                        .join('&')
                    : providerConfig?.json_body_type === 'raw'
                        ? providerConfig?.raw_body
                        : Object.keys(body || {}).length > 0
                            ? body
                            : undefined,
                // timeout: 5000,
                ...(providerConfig?.method?.toUpperCase() === 'GET' && Object.keys(body || {}).length > 0
                    ? { queryParams: body }
                    : {}),
                transformResponse: providerConfig?.transformResponse,
            },
        };
    }
    return customHTTPProvider;
};
exports.handleHTTPAPIStructure = handleHTTPAPIStructure;
//# sourceMappingURL=index.js.map