"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Holder_1 = require("../../../../../Class/Holder");
const ServerSocket_1 = require("../../../../../wrap/ServerSocket");
const _ids_1 = require("../../../../_ids");
class Upgrade extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['url'],
            fo: ['socket'],
            i: [],
            o: [],
        }, {
            input: {},
            output: {
                socket: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_UPGRADE, 'close');
    }
    async f({ url }, done, fail) {
        const { cache: { requests, responses, ws, wss }, api: { http: { handleUpgrade }, }, } = this.__system;
        const _request = requests[url];
        const _response = responses[url];
        if (!_request) {
            fail('request not found');
            return;
        }
        let _socket;
        try {
            _socket = await handleUpgrade(_request, _response, ws, wss);
        }
        catch (err) {
            fail(err.message);
            return;
        }
        const socket = (0, ServerSocket_1.wrapServerSocket)(_socket, this.__system);
        done({ socket });
    }
    d() {
        if (this._unlisten) {
            this._unlisten();
            this._unlisten = undefined;
        }
    }
    onIterDataInputData(name, data) {
        super.onIterDataInputData(name, data);
    }
}
exports.default = Upgrade;
//# sourceMappingURL=index.js.map