"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Observer_1 = require("./Observer");
const OBSERVATION_ARTIFACT_NAME = 'api_requests.json';
class ApiRequestResponseObserver extends Observer_1.AfterObserver {
    constructor(dispatcher) {
        super(Observer_1.ObservationType.API_REQUEST_RESPONSE, dispatcher);
        this.observeAfterFunction = async (context) => {
            const httpExecutor = context.executionContext.executorStore.getStepExecutorByType('send_http_request');
            const maybeLastRequestBufferCopy = httpExecutor.getInterceptedRequestBuffer();
            httpExecutor.clearInterceptBuffer();
            if (maybeLastRequestBufferCopy?.length === 0) {
                return {};
            }
            const stringifiedObservationData = JSON.stringify(maybeLastRequestBufferCopy);
            const tmpFile = await this.writeTempFile(OBSERVATION_ARTIFACT_NAME, stringifiedObservationData);
            const filesByObservationType = {};
            if (tmpFile) {
                filesByObservationType[this.observationType] = { uri: tmpFile };
            }
            return { filesByObservationType };
        };
        this.observeBefore = (options) => {
            if (options.properties?.isIntermediateAction ||
                !(0, Observer_1.isObservable)(options.executionIndex)) {
                return Promise.resolve();
            }
            const httpExecutor = options.executionContext.executorStore.getStepExecutorByType('send_http_request');
            httpExecutor.requestInterceptionEnabled(true);
            return Promise.resolve();
        };
        this.observeAfter = async (options) => {
            if (!options.properties?.isIntermediateAction) {
                await super.observeAfter(options);
            }
        };
    }
}
exports.default = ApiRequestResponseObserver;
