"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usedBodyToString = usedBodyToString;
exports.wrapResponse = wrapResponse;
const _1 = require("../Class/$");
const ObjectPathTooDeep_1 = require("../exception/ObjectPathTooDeep");
const ObjectReadOnly_1 = require("../exception/ObjectReadOnly");
const NOOP_1 = require("../NOOP");
const Handle_1 = require("../system/platform/api/http/Handle");
const ReadableStream_1 = require("./ReadableStream");
function usedBodyToString(usedBody) {
    if (typeof usedBody === 'string') {
        return usedBody;
    }
    else {
        return '';
    }
}
function wrapResponse(response, system) {
    const $response = new (class Response_ extends _1.$ {
        constructor() {
            super(...arguments);
            this.__ = ['RES'];
        }
        read() {
            const res = {
                url: response.url,
                type: response.type,
                status: response.status,
                statusText: response.statusText,
                redirected: response.redirected,
                bodyUsed: response.bodyUsed,
                headers: (0, Handle_1.headersToObj)(response.headers),
                body: usedBodyToString(this._body ?? ''),
                ok: response.ok,
            };
            return res;
        }
        write(data) {
            throw new ObjectReadOnly_1.ReadOnlyError('');
        }
        async json() {
            this._body = await response.json();
            return this._body;
        }
        async text() {
            this._body = await response.text();
            return this._body;
        }
        async blob() {
            this._body = await response.blob();
            return this._body;
        }
        async arrayBuffer() {
            this._body = await response.arrayBuffer();
            return this._body;
        }
        raw() {
            return response;
        }
        get(name) {
            if (name === 'body') {
                return (0, ReadableStream_1.wrapReadableStream)(response.body, system);
            }
            if (name === 'headers') {
                return (0, Handle_1.headersToObj)(response.headers);
            }
            return response[name];
        }
        set(name, data) {
            throw new ObjectReadOnly_1.ReadOnlyError('request');
        }
        delete(name) {
            throw new ObjectReadOnly_1.ReadOnlyError('request');
        }
        hasKey(name) {
            return response[name] !== undefined;
        }
        keys() {
            return Object.keys(response);
        }
        deepGet(path) {
            if (path.length > 1) {
                throw new ObjectPathTooDeep_1.ObjectPathTooDeepError();
            }
            if (path.length === 0) {
                return this;
            }
            // @ts-ignore
            return this.get(path[0]);
        }
        deepSet(path, data) {
            throw new ObjectReadOnly_1.ReadOnlyError('request');
        }
        deepDelete(path) {
            throw new ObjectReadOnly_1.ReadOnlyError('request');
        }
        deepHas(path) {
            try {
                this.deepGet(path);
                return true;
            }
            catch (err) {
                return false;
            }
        }
        subscribe(path, key, listener) {
            return NOOP_1.NOOP;
        }
    })(system);
    return $response;
}
//# sourceMappingURL=Response.js.map