import { HttpResponse } from './HttpResponse.js';
import { RequestTime } from './RequestTime.js';
import { ResponseKind } from './kinds.js';
export const Kind = 'Core#HttpResponseRedirect';
export class ResponseRedirect {
    kind = Kind;
    /**
     * Redirection response
     */
    response;
    /**
     * Redirection timings, if available.
     */
    timings;
    /**
     * The timestamp when the request was started (before the connection is made)
     */
    startTime = 0;
    /**
     * The timestamp of when the response ended.
     */
    endTime = 0;
    /**
     * The URL the request was redirected to
     */
    url = '';
    /**
     * Creates a redirect object from basic values.
     * @param url The redirect URL
     * @param response The response object created by the transport
     * @param startTime The time when the request started
     * @param endTime The time when the request ended
     */
    static fromValues(url, response, startTime = 0, endTime = 0) {
        return new ResponseRedirect({
            kind: Kind,
            url,
            startTime,
            endTime,
            response,
        });
    }
    static async fromLegacy(redirect) {
        const init = {
            kind: Kind,
            endTime: redirect.endTime || 0,
            startTime: redirect.startTime || 0,
            url: redirect.url || '',
            response: {
                kind: ResponseKind,
                status: 0,
            },
        };
        if (redirect.response) {
            const response = await HttpResponse.fromLegacy(redirect.response);
            init.response = response.toJSON();
        }
        if (redirect.timings) {
            init.timings = new RequestTime(redirect.timings).toJSON();
        }
        return new ResponseRedirect(init);
    }
    /**
     * @param input The redirect 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,
                endTime: 0,
                startTime: 0,
                url: '',
                response: {
                    kind: ResponseKind,
                    status: 0,
                },
            };
        }
        this.new(init);
    }
    /**
     * Creates a new redirect clearing anything that is so far defined.
     */
    new(init) {
        const { response, timings, startTime = 0, endTime = 0, url = '', kind = Kind } = init;
        this.kind = kind;
        this.startTime = startTime;
        this.endTime = endTime;
        this.url = url;
        this.response = new HttpResponse(response);
        if (timings) {
            this.timings = new RequestTime(timings);
        }
        else {
            this.timings = undefined;
        }
    }
    toJSON() {
        const result = {
            kind: this.kind,
            startTime: this.startTime,
            endTime: this.endTime,
            url: this.url,
            response: {
                kind: ResponseKind,
                status: 0,
            },
        };
        if (this.response) {
            result.response = this.response.toJSON();
        }
        if (this.timings) {
            result.timings = this.timings.toJSON();
        }
        return result;
    }
}
//# sourceMappingURL=ResponseRedirect.js.map