"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterceptedRequest = exports.Interceptor = void 0;
const uuid_1 = require("uuid");
const cdp_util_js_1 = require("./cdp-util.js");
class Interceptor {
    constructor(browser, handler, label) {
        this.browser = browser;
        this.handler = handler;
        this.label = label;
        this.id = (0, uuid_1.v4)();
    }
    stop() {
        const i = this.browser.interceptors.findIndex(_ => _.id === this.id);
        if (i > -1) {
            this.browser.interceptors.splice(i, 1);
        }
    }
}
exports.Interceptor = Interceptor;
class InterceptedRequest {
    constructor(target, payload) {
        this.target = target;
        this.modifications = {};
        this.requestId = payload.requestId;
        this.frameId = payload.frameId;
        this.resourceType = payload.resourceType;
        this.request = payload.request;
        this.responseErrorReason = payload.responseErrorReason;
        this.responseStatusCode = payload.responseStatusCode;
        this.responseHeaders = payload.responseHeaders;
        this.networkId = payload.networkId;
    }
    fail(errorReason) {
        return {
            method: 'Fetch.failRequest',
            params: {
                requestId: this.requestId,
                errorReason,
            },
        };
    }
    fulfill(response) {
        const { responseCode, responseHeaders, body } = response;
        const params = {
            requestId: this.requestId,
            responseCode,
            responseHeaders: responseHeaders ? (0, cdp_util_js_1.convertHeadersToEntries)(responseHeaders) : undefined,
        };
        if (body) {
            const bodyBuffer = body instanceof Buffer ? body : Buffer.from(body);
            params.body = bodyBuffer.toString('base64');
        }
        return {
            method: 'Fetch.fulfillRequest',
            params,
        };
    }
    continue(mods = {}) {
        this.assignModifications(mods);
        const { method, url, postData } = this.modifications;
        return {
            method: 'Fetch.continueRequest',
            params: {
                requestId: this.requestId,
                method,
                url,
                postData,
                headers: this.getEffectiveHeaderEntries(),
            },
        };
    }
    pass(mods = {}) {
        this.assignModifications(mods);
        return {
            method: 'pass',
            params: {},
        };
    }
    getEffectiveHeaders() {
        if (this.modifications.headers) {
            return (0, cdp_util_js_1.convertHeadersToObject)(this.modifications.headers);
        }
        if (this.modifications.extraHeaders) {
            return {
                ...this.request.headers,
                ...(0, cdp_util_js_1.convertHeadersToObject)(this.modifications.extraHeaders),
            };
        }
        return undefined;
    }
    getEffectiveHeaderEntries() {
        const headers = this.getEffectiveHeaders();
        return headers ? (0, cdp_util_js_1.convertHeadersToEntries)(headers) : undefined;
    }
    assignModifications(mods) {
        if (mods.method) {
            this.modifications.method = mods.method;
        }
        if (mods.url) {
            this.modifications.url = mods.url;
        }
        if (mods.postData) {
            this.modifications.postData = mods.postData;
        }
        if (mods.headers) {
            this.modifications.headers = (0, cdp_util_js_1.convertHeadersToObject)(mods.headers);
        }
        if (mods.extraHeaders) {
            this.modifications.extraHeaders = {
                ...this.modifications.extraHeaders,
                ...(0, cdp_util_js_1.convertHeadersToObject)(mods.extraHeaders),
            };
        }
    }
}
exports.InterceptedRequest = InterceptedRequest;
//# sourceMappingURL=interceptor.js.map