"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../Class/Functional");
const _ids_1 = require("../../../../_ids");
class Fetch extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['url', 'opt'],
            o: ['response'],
        }, {}, system, _ids_1.ID_FETCH);
    }
    async f({ url, opt }, done) {
        const { api: { http: { fetch }, }, cache: { servers }, } = this.__system;
        try {
            fetch(url, opt, servers)
                .then((response) => {
                const { bodyUsed, headers, redirected, ok, status, statusText, type, url, } = response;
                return response.text().then((text) => {
                    done({
                        response: {
                            url,
                            status,
                            bodyUsed,
                            headers: { ...headers },
                            redirected,
                            ok,
                            statusText,
                            type,
                            body: text,
                        },
                    });
                });
            })
                .catch((err) => {
                if (err.message.toLowerCase() ===
                    "failed to execute 'fetch' on 'window': request with get/head method cannot have body.") {
                    done(undefined, 'GET/HEAD request cannot have body');
                    return;
                }
                if (err.message.startsWith("Failed to execute 'fetch' on 'Window': Failed to parse URL from ")) {
                    done(undefined, 'failed to parse url');
                    return;
                }
                done(undefined, err.message.toLowerCase());
            });
        }
        catch (err) {
            // console.log(err)
            done(undefined, 'malformed');
        }
    }
}
exports.default = Fetch;
//# sourceMappingURL=index.js.map