"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.headersToObj = headersToObj;
const Holder_1 = require("../../../../../Class/Holder");
const ReadableStream_1 = require("../../../../../wrap/ReadableStream");
const _ids_1 = require("../../../../_ids");
function headersToObj(headers_) {
    const headers = {};
    headers_.forEach((value, key) => {
        headers[key] = value;
    });
    return headers;
}
class Handle extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['url'],
            fo: ['request', 'body', 'url'],
            i: [],
            o: [],
        }, {
            output: {
                body: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_HANDLE, 'done');
    }
    async f({ url: url_ }, done, fail) {
        const { cache: { requests }, } = this.__system;
        if (!requests[url_]) {
            fail('request cannot be found');
            return;
        }
        const { url, method, headers, body: body_ } = requests[url_];
        const body = (0, ReadableStream_1.wrapReadableStream)(body_, this.__system);
        const { search, pathname, hostname, origin, protocol, port } = new URL(url);
        const request = {
            url,
            headers,
            search,
            query: Object.fromEntries(new URLSearchParams(search)),
            method,
            path: pathname,
            hostname,
            origin,
            body: undefined,
            protocol,
            port,
        };
        done({
            url: url_,
            request,
            body,
        });
    }
    d() {
        //
    }
}
exports.default = Handle;
//# sourceMappingURL=index.js.map