"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClient = exports.createServer = void 0;
var tslib_1 = require("tslib");
var thrift_1 = require("thrift");
function wrapHandler(handler) {
    var wrap = {};
    var _loop_1 = function (key) {
        var func = handler[key];
        if (typeof func != 'function') {
            return "continue";
        }
        var wrapper = function () {
            var args = [];
            for (var _i = 0; _i < arguments.length; _i++) {
                args[_i] = arguments[_i];
            }
            return tslib_1.__awaiter(this, void 0, void 0, function () {
                var callback, ret, ex_1;
                return tslib_1.__generator(this, function (_a) {
                    switch (_a.label) {
                        case 0:
                            callback = args.pop();
                            _a.label = 1;
                        case 1:
                            _a.trys.push([1, 3, , 4]);
                            return [4 /*yield*/, this[key].apply(this, tslib_1.__spreadArray([], tslib_1.__read(args)))];
                        case 2:
                            ret = _a.sent();
                            callback(null, ret);
                            return [3 /*break*/, 4];
                        case 3:
                            ex_1 = _a.sent();
                            callback(ex_1);
                            return [3 /*break*/, 4];
                        case 4: return [2 /*return*/];
                    }
                });
            });
        }.bind(handler);
        Object.defineProperty(wrapper, 'length', { value: -1 });
        wrap[key] = wrapper;
    };
    for (var key in handler) {
        _loop_1(key);
    }
    return wrap;
}
function createServer(options) {
    var multiplex = new thrift_1.MultiplexedProcessor();
    var server = thrift_1.createMultiplexServer(multiplex, options);
    server.route = function route(name, processor, handler) {
        if (name in multiplex.services) {
            throw new Error("Service with name \"" + name + "\" already exists");
        }
        while (typeof processor.Processor == 'function') {
            processor = processor.Processor;
        }
        multiplex.registerProcessor(name, new processor(wrapHandler(handler)));
        return this;
    };
    return server;
}
exports.createServer = createServer;
function createClient(options) {
    var _a = options !== null && options !== void 0 ? options : {}, host = _a.host, port = _a.port, tls = _a.tls, opt = tslib_1.__rest(_a, ["host", "port", "tls"]);
    host = host || 'localhost';
    port = Number.parseInt(port);
    if (Number.isNaN(port) || port <= 0 || port > 65535) {
        port = 4000;
    }
    var connection = tls ? thrift_1.createSSLConnection(host, port, tslib_1.__assign(tslib_1.__assign({}, opt), tls)) : thrift_1.createConnection(host, port, opt);
    var multiplexer = new thrift_1.Multiplexer();
    var clients = new Map();
    connection.get = function (name, client) {
        var service = clients.get(name);
        if (service) {
            return service;
        }
        if (!client) {
            throw new Error("Service with name \"" + name + "\" has not created");
        }
        while (typeof client.Client == 'function') {
            client = client.Client;
        }
        service = multiplexer.createClient(name, client, this);
        clients.set(name, service);
        return service;
    };
    connection.has = function (name) {
        return !!clients.get(name);
    };
    return connection;
}
exports.createClient = createClient;
//# sourceMappingURL=index.js.map