import { ResponseRedirect } from './ResponseRedirect.js';
import { SentRequest } from './SentRequest.js';
import { ErrorResponse } from './ErrorResponse.js';
import { Response } from './Response.js';
import { RequestsSize } from './RequestsSize.js';
export const Kind = 'Core#ResponseLog';
/**
 * A request / response pair associated with a request.
 */
export class RequestLog {
    kind = Kind;
    /**
     * Describes an HTTP request sent by the transport.
     */
    request;
    /**
     * The last response made with this request, if any.
     */
    response;
    /**
     * The list of redirects, if any.
     */
    redirects;
    /**
     * Request and response size. Some HTTP clients may not give this information.
     */
    size;
    /**
     * Optional request ID defined on an HTTP project that triggered this log.
     */
    requestId;
    /**
     * The assertions result performed on the response.
     * Note, Error response can also be passed through tests.
     */
    assertions;
    static fromRequest(request) {
        return new RequestLog({
            kind: Kind,
            request,
        });
    }
    static fromRequestResponse(request, response) {
        return new RequestLog({
            kind: Kind,
            request,
            response,
        });
    }
    /**
     * @param input The response definition used to restore the state.
     */
    constructor(input) {
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: Kind,
            };
        }
        this.new(init);
    }
    /**
     * Creates a new response clearing anything that is so far defined.
     */
    new(init) {
        const { request, response, redirects, size, requestId, assertions } = init;
        this.kind = Kind;
        if (request) {
            this.request = new SentRequest(request);
        }
        if (response) {
            if (Response.isErrorResponse(response)) {
                this.response = new ErrorResponse(response);
            }
            else {
                this.response = new Response(response);
            }
        }
        else {
            this.response = undefined;
        }
        if (Array.isArray(redirects)) {
            this.redirects = redirects.map((i) => new ResponseRedirect(i));
        }
        else {
            this.redirects = undefined;
        }
        if (size) {
            this.size = new RequestsSize(size);
        }
        else {
            this.size = undefined;
        }
        if (requestId) {
            this.requestId = requestId;
        }
        else {
            this.requestId = undefined;
        }
        if (Array.isArray(assertions) && assertions.length) {
            this.assertions = assertions.map((i) => ({ ...i }));
        }
        else {
            this.assertions = undefined;
        }
    }
    toJSON() {
        const result = {
            kind: Kind,
        };
        if (this.request) {
            result.request = this.request.toJSON();
        }
        if (Array.isArray(this.redirects)) {
            result.redirects = this.redirects.map((i) => i.toJSON());
        }
        if (this.size) {
            result.size = this.size.toJSON();
        }
        if (this.response) {
            result.response = this.response.toJSON();
        }
        if (this.requestId) {
            result.requestId = this.requestId;
        }
        if (Array.isArray(this.assertions) && this.assertions.length) {
            result.assertions = this.assertions.map((i) => ({ ...i }));
        }
        return result;
    }
    /**
     * Adds a redirect to this log.
     * It checks whether the redirects array has been initialized.
     */
    addRedirect(redirect) {
        if (!this.redirects) {
            this.redirects = [];
        }
        const instance = new ResponseRedirect(redirect);
        this.redirects.push(instance);
        return instance;
    }
    async addLegacyRedirect(redirect) {
        if (!this.redirects) {
            this.redirects = [];
        }
        const info = await ResponseRedirect.fromLegacy(redirect);
        this.redirects.push(info);
        return info;
    }
}
//# sourceMappingURL=RequestLog.js.map