import { Http } from './Http.js';
import { Headers } from '../lib/headers/Headers.js';
export class HttpWeb extends Http {
    /**
     * Performs the GET request.
     *
     * @param url The request URL
     * @param opts The request options
     * @returns The response info.
     */
    async get(url, opts = {}) {
        const { method = 'GET', headers = {}, token = this.sdk.token } = opts;
        const requestHeaders = new Headers({ ...headers });
        if (token) {
            requestHeaders.append('Authorization', `Bearer ${token}`);
        }
        if (!requestHeaders.has('Accept')) {
            requestHeaders.append('Accept', 'application/json');
        }
        const init = {
            method,
            headers: requestHeaders.toJSON(),
            redirect: 'follow',
            credentials: 'include',
            mode: 'cors',
        };
        const response = await fetch(url, init);
        let body;
        try {
            body = await response.text();
        }
        catch {
            // ...
        }
        const responseHeaders = new Headers();
        response.headers.forEach((value, key) => {
            responseHeaders.append(key, value);
        });
        const result = {
            status: response.status,
            headers: responseHeaders,
            body,
        };
        return result;
    }
    async post(url, opts = {}) {
        const { method = 'POST', headers = {}, token = this.sdk.token } = opts;
        const requestHeaders = new Headers({ ...headers });
        if (token) {
            requestHeaders.append('Authorization', `Bearer ${token}`);
        }
        if (!requestHeaders.has('Content-Type')) {
            requestHeaders.append('Content-Type', 'application/json');
        }
        if (!requestHeaders.has('Accept')) {
            requestHeaders.append('Accept', 'application/json');
        }
        const init = {
            method,
            headers: requestHeaders.toJSON(),
            redirect: 'follow',
            credentials: 'include',
        };
        if (opts.body) {
            init.body = opts.body.toString();
        }
        const response = await fetch(url, init);
        let body;
        try {
            body = await response.text();
        }
        catch {
            // ...
        }
        const responseHeaders = new Headers();
        response.headers.forEach((value, key) => {
            responseHeaders.append(key, value);
        });
        const result = {
            status: response.status,
            headers: responseHeaders,
            body,
        };
        return result;
    }
    patch(url, opts = {}) {
        const options = { ...opts, method: 'PATCH' };
        return this.post(url, options);
    }
    put(url, opts = {}) {
        const options = { ...opts, method: 'PUT' };
        return this.post(url, options);
    }
    delete(url, opts = {}) {
        const options = { ...opts, method: 'DELETE' };
        return this.post(url, options);
    }
}
//# sourceMappingURL=HttpWeb.js.map