"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var autobahn_1 = require("autobahn");
var api_1 = require("./api");
var constants_1 = require("./constants");
var Router = require('wamp-server');
function wampServerOverFs(fs, port) {
    if (port === void 0) { port = 3000; }
    return new Promise(function (resolve) {
        var router = new Router({
            port: port,
            realms: [constants_1.wampRealm]
        });
        var connection = new autobahn_1.Connection({
            realm: constants_1.wampRealm,
            url: "ws://127.0.0.1:" + port + "/",
        });
        connection.onopen = function (session) {
            api_1.fileSystemEventNames.forEach(function (fsEvent) {
                fs.events.on(fsEvent, function (data) { return session.isOpen && session.publish("" + constants_1.wampRealmPrefix + fsEvent, [data]); });
            });
            api_1.fileSystemMethods.forEach(function (ev) {
                session.register("" + constants_1.wampRealmPrefix + ev, function (data) {
                    return (_a = fs)[ev].apply(_a, data);
                    var _a;
                });
            });
            resolve({
                router: router,
                connection: connection
            });
        };
        connection.onclose = function (_reason, details) {
            if (!details.will_retry && api_1.isDisposable(fs)) {
                fs.dispose();
            }
            return details.will_retry;
        };
        connection.open();
    });
}
exports.wampServerOverFs = wampServerOverFs;
//# sourceMappingURL=wamp-server-over-fs.js.map