import Proxy from './Proxy.js';
import { HttpRequestRunner } from '../runtime/http-runner/HttpRequestRunner.js';
import { Exception } from '../exceptions/exception.js';
import { createLogger } from '../lib/logging/index.js';
/**
 * Proxies a single HTTP request
 */
export default class RequestProxy extends Proxy {
    init;
    async configure(init) {
        const { request } = init;
        const INVALID_REQUEST = 'Invalid request';
        const code = 'E_INVALID_REQUEST';
        if (!request) {
            throw new Exception(INVALID_REQUEST, {
                help: 'The "request" parameter is required.',
                status: 400,
                code,
            });
        }
        if (!request.url) {
            throw new Exception(INVALID_REQUEST, {
                help: 'The "request.url" parameter is required.',
                status: 400,
                code,
            });
        }
        this.init = init;
    }
    async execute() {
        const data = this.init;
        const { request, authorization, certificate, config = {}, variables = {}, flows, assertions } = data;
        const factory = new HttpRequestRunner();
        factory.variables = variables;
        factory.logger = createLogger();
        factory.config = { ...config, enabled: true };
        if (Array.isArray(authorization) && authorization.length) {
            factory.authorization = authorization;
        }
        if (Array.isArray(flows) && flows.length) {
            factory.flows = flows;
        }
        if (Array.isArray(assertions) && assertions.length) {
            factory.assertions = assertions;
        }
        if (certificate) {
            factory.certificates = [certificate];
        }
        const result = await factory.run(request);
        return {
            result: result,
            variables,
        };
    }
}
//# sourceMappingURL=RequestProxy.js.map