"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.N8nProvider = void 0;
const http_1 = require("../http");
const logger_1 = __importDefault(require("../../logger"));
class N8nProvider {
    constructor(config, options = {}) {
        this.config = options.config || {};
        this.providerConfig = config;
        // Create HttpProvider with N8n-specific configuration
        const method = this.providerConfig.method || 'POST';
        const supportsBody = method !== 'GET' && method !== 'HEAD' && method !== 'DELETE';
        // Build headers based on auth method
        const headers = {};
        // Only add Content-Type for methods that support body
        if (supportsBody) {
            headers['Content-Type'] = 'application/json';
        }
        if (this.providerConfig.authMethod === 'api_key' && this.providerConfig.apiKeyName && this.providerConfig.apiKeyValue) {
            // For API key auth, use Authorization header with Bearer token if key name is standard, otherwise use custom header
            if (this.providerConfig.apiKeyName.toLowerCase() === 'authorization' || this.providerConfig.apiKeyName.toLowerCase().includes('token')) {
                headers.Authorization = this.providerConfig.apiKeyValue.startsWith('Bearer ') ? this.providerConfig.apiKeyValue : `Bearer ${this.providerConfig.apiKeyValue}`;
            }
            else {
                headers[this.providerConfig.apiKeyName] = this.providerConfig.apiKeyValue;
            }
        }
        else if (this.providerConfig.authMethod === 'basic' && this.providerConfig.username && this.providerConfig.password) {
            const credentials = Buffer.from(`${this.providerConfig.username}:${this.providerConfig.password}`).toString('base64');
            headers.Authorization = `Basic ${credentials}`;
        }
        else if (this.providerConfig.authMethod === 'custom' && this.providerConfig.customHeaders) {
            this.providerConfig.customHeaders.forEach((header) => {
                headers[header.key] = header.value;
            });
        }
        const httpProviderConfig = {
            url: this.providerConfig.url,
            method,
            headers,
            transformResponse: options.config?.transformResponse || 'json.output || json.data || json',
        };
        // Only add body for methods that support it (not GET, HEAD, DELETE)
        if (supportsBody) {
            httpProviderConfig.body = {
                prompt: '{{prompt}}',
            };
        }
        this.httpProvider = new http_1.HttpProvider(httpProviderConfig.url, {
            ...options,
            config: httpProviderConfig,
        });
    }
    id() {
        return `n8n:${this.providerConfig.url}`;
    }
    async callApi(prompt, context) {
        logger_1.default.debug(`[N8n Provider]: Calling API with url: ${this.providerConfig.url}`);
        return this.httpProvider.callApi(prompt, context);
    }
}
exports.N8nProvider = N8nProvider;
//# sourceMappingURL=n8n.js.map