import Proxy from './Proxy.js';
import { ApiError } from '../runtime/store/Errors.js';
import { DummyLogger } from '../lib/logging/DummyLogger.js';
import { HttpRequestRunner } from '../runtime/http-runner/HttpRequestRunner.js';
/**
 * Proxies a single HTTP request
 */
export default class RequestProxy extends Proxy {
    init;
    async configure(init) {
        const { request } = init;
        if (!request) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "request" parameter is required.',
                code: 400,
            });
        }
        if (!request.url) {
            throw new ApiError({
                error: true,
                message: 'Invalid request',
                detail: 'The "request.url" parameter is required.',
                code: 400,
            });
        }
        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 = new DummyLogger();
        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