import { SerializablePayload } from './SerializablePayload.js';
import { PayloadSerializer } from '../lib/transformers/PayloadSerializer.js';
import { Normalizer } from './legacy/Normalizer.js';
import { HttpResponseKind } from './kinds.js';
export class HttpResponse extends SerializablePayload {
    kind = HttpResponseKind;
    /**
     * The response status code
     */
    status = 0;
    /**
     * The reason part of the status message
     */
    statusText;
    /**
     * The response headers
     */
    headers;
    static fromValues(status, statusText, headers) {
        return new HttpResponse({
            kind: HttpResponseKind,
            status,
            statusText,
            headers,
        });
    }
    static async fromLegacy(response) {
        const init = {
            kind: HttpResponseKind,
            status: response.status || 0,
        };
        if (response.statusText) {
            init.statusText = response.statusText;
        }
        if (response.headers) {
            init.headers = response.headers;
        }
        if (response.payload) {
            const orig = Normalizer.restoreTransformedPayload(response.payload);
            if (orig) {
                init.payload = await PayloadSerializer.serialize(orig);
            }
        }
        return new HttpResponse(init);
    }
    /**
     * @param input The response definition used to restore the state.
     */
    constructor(input) {
        super();
        let init;
        if (typeof input === 'string') {
            init = JSON.parse(input);
        }
        else if (typeof input === 'object') {
            init = input;
        }
        else {
            init = {
                kind: HttpResponseKind,
                status: 0,
            };
        }
        this.new(init);
    }
    /**
     * Creates a new response clearing anything that is so far defined.
     *
     * Note, this throws an error when the object is not a response.
     */
    new(init) {
        if (!HttpResponse.isHttpResponse(init)) {
            throw new Error(`Not a response.`);
        }
        const { status, statusText, headers, payload, kind = HttpResponseKind } = init;
        this.kind = kind;
        this.status = status;
        this.statusText = statusText;
        this.headers = headers;
        this.payload = payload;
    }
    /**
     * Checks whether the input is a definition of an HTTP response.
     */
    static isHttpResponse(input) {
        const typed = input;
        if (!input || typeof typed.status !== 'number') {
            return false;
        }
        return true;
    }
    toJSON() {
        const result = {
            ...super.toJSON(),
            kind: this.kind,
            status: this.status,
        };
        if (this.statusText) {
            result.statusText = this.statusText;
        }
        if (this.headers) {
            result.headers = this.headers;
        }
        return result;
    }
    /**
     * A shortcut function to create an instance from a response object and to read the payload of it as string.
     */
    static readPayloadAsString(info) {
        const request = new HttpResponse(info);
        return request.readPayloadAsString();
    }
}
//# sourceMappingURL=HttpResponse.js.map