import { HttpRequest, Kind } from './HttpRequest.js';
import { PayloadSerializer } from '../lib/transformers/PayloadSerializer.js';
/**
 * A class that describes a request that has been sent by the transport library
 * to the remote machine. It contains the base HTTP request properties plus
 * the sent message and start and end time.
 */
export class SentRequest extends HttpRequest {
    /**
     * The HTTP message sent to the server (full message).
     * Some HTTP clients may not give this information.
     */
    httpMessage;
    /**
     * The timestamp when the request was started (before the connection is made)
     */
    startTime = 0;
    /**
     * The timestamp of when the response ended.
     * When `0` then the requests is still being transported.
     */
    endTime = 0;
    static fromBaseValues(values) {
        return new SentRequest({
            ...values,
            kind: Kind,
        });
    }
    static async fromLegacy(input) {
        const init = {
            kind: Kind,
            url: input.url || '',
            startTime: input.startTime || 0,
        };
        if (typeof input.endTime === 'number') {
            init.endTime = input.endTime;
        }
        if (input.headers) {
            init.headers = input.headers;
        }
        if (input.httpMessage) {
            init.httpMessage = input.httpMessage;
        }
        if (input.method) {
            init.method = input.method;
        }
        if (input.payload) {
            init.payload = await PayloadSerializer.serialize(input.payload);
        }
        return new SentRequest(init);
    }
    /**
     * @param input The request definition used to restore the state.
     */
    constructor(input) {
        super(input);
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: Kind,
                url: '',
                startTime: 0,
            };
        }
        this.new(init);
    }
    /**
     * Creates a new sent request clearing anything that is so far defined.
     */
    new(init) {
        super.new(init);
        const { httpMessage, startTime = 0, endTime = 0 } = init;
        this.httpMessage = httpMessage;
        this.startTime = startTime;
        this.endTime = endTime;
    }
    toJSON() {
        const request = super.toJSON();
        request.httpMessage = this.httpMessage;
        request.startTime = this.startTime;
        request.endTime = this.endTime;
        return request;
    }
}
//# sourceMappingURL=SentRequest.js.map