"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var autobahn_1 = require("autobahn");
var api_1 = require("./api");
var utils_1 = require("./utils");
var promise_utils_1 = require("./promise-utils");
exports.noConnectionError = "WampClientFileSystem hasn't opened connection yet (forgot to init()?).";
var WampClientFileSystem = /** @class */ (function () {
    function WampClientFileSystem(baseUrl, realm, initTimeout) {
        if (initTimeout === void 0) { initTimeout = 5000; }
        this.baseUrl = baseUrl;
        this.realm = realm;
        this.initTimeout = initTimeout;
        this.events = utils_1.makeEventsEmitter();
        this.realm = realm;
        this.connection = new autobahn_1.Connection({ url: baseUrl, realm: realm });
    }
    WampClientFileSystem.prototype.init = function () {
        var _this = this;
        var _a = this, baseUrl = _a.baseUrl, initTimeout = _a.initTimeout, connection = _a.connection;
        return promise_utils_1.timeoutPromise(new Promise(function (resolve) {
            connection.open();
            connection.onopen = function (session) {
                _this.session = session;
                _this.realmPrefix = _this.realm.replace(/(.*\..*)(\..*)$/, '$1.'); // 'xxx.yyy.zzz' => 'xxx.yyy.'
                api_1.fileSystemEventNames.forEach(function (fsEvent) {
                    _this.session.subscribe(_this.realmPrefix + fsEvent, function (res) { return _this.events.emit(fsEvent, res && res[0]); });
                });
                resolve(_this);
            };
        }), initTimeout, "Cant't open connection to the WAMP server at " + baseUrl + " for " + initTimeout + "ms.");
    };
    WampClientFileSystem.prototype.saveFile = function (fullPath, newContent) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_1;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "saveFile", [fullPath, newContent])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_1 = _a.sent();
                        throw new Error(error_1);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.deleteFile = function (fullPath) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_2;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "deleteFile", [fullPath])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_2 = _a.sent();
                        throw new Error(error_2);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.deleteDirectory = function (fullPath, recursive) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_3;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "deleteDirectory", [fullPath, recursive])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_3 = _a.sent();
                        throw new Error(error_3);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.ensureDirectory = function (fullPath) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_4;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "ensureDirectory", [fullPath])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_4 = _a.sent();
                        throw new Error(error_4);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.loadTextFile = function (fullPath) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_5;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "loadTextFile", [fullPath])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_5 = _a.sent();
                        throw new Error(error_5);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.loadDirectoryTree = function (fullPath) {
        if (fullPath === void 0) { fullPath = ''; }
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_6;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "loadDirectoryTree", [fullPath])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_6 = _a.sent();
                        throw new Error(error_6);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.loadDirectoryChildren = function (fullPath) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var error_7;
            return tslib_1.__generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.throwIfDisconnected();
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 3, , 4]);
                        return [4 /*yield*/, this.session.call(this.realmPrefix + "loadDirectoryChildren", [fullPath])];
                    case 2: return [2 /*return*/, _a.sent()];
                    case 3:
                        error_7 = _a.sent();
                        throw new Error(error_7);
                    case 4: return [2 /*return*/];
                }
            });
        });
    };
    WampClientFileSystem.prototype.dispose = function () {
        this.connection && this.connection.close();
    };
    WampClientFileSystem.prototype.throwIfDisconnected = function () {
        if (!this.session || !this.session.isOpen) {
            throw new Error(exports.noConnectionError);
        }
    };
    return WampClientFileSystem;
}());
exports.WampClientFileSystem = WampClientFileSystem;
//# sourceMappingURL=wamp-client-fs.js.map