export var MemoryDefaultToken = "f";
;
var Channel = /** @class */ (function () {
    function Channel(token, data, memToken) {
        this.p_data = data;
        this.p_token = token;
        this.p_memToken = memToken || MemoryDefaultToken;
        this.listeners = {
            events: function () { },
        };
        this.p_completed = {};
    }
    ;
    Object.defineProperty(Channel.prototype, "token", {
        get: function () {
            return this.p_token;
        },
        enumerable: false,
        configurable: true
    });
    ;
    Object.defineProperty(Channel.prototype, "name", {
        get: function () {
            return this.p_data.name;
        },
        enumerable: false,
        configurable: true
    });
    ;
    Object.defineProperty(Channel.prototype, "type", {
        get: function () {
            return this.p_data.name;
        },
        enumerable: false,
        configurable: true
    });
    ;
    Object.defineProperty(Channel.prototype, "memToken", {
        get: function () {
            return this.p_memToken;
        },
        enumerable: false,
        configurable: true
    });
    ;
    Channel.prototype.events = function (memToken, list) {
        this.p_memToken = memToken;
        if (!Array.isArray(list))
            return;
        for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
            var item = list_1[_i];
            if (this.p_completed[item.token])
                continue;
            this.p_completed[item.token] = true;
            var data = {};
            var value = {};
            try {
                data = typeof item.value == "string" ? JSON.parse(item.value) : item.value;
                value = typeof (data.value) == "string" ? JSON.parse(data.value) : data.value;
            }
            catch (e) { }
            ;
            if (data.type == "join") {
                Channels.joinAction(data);
                continue;
            }
            else if (data.type == "leave") {
                Channels.leaveAction(data);
                continue;
            }
            ;
            this.listeners.events(data.action || "", value);
            Channels.listeners.events(this, data.action || "", data);
        }
        ;
    };
    ;
    Channel.prototype.on = function (name, fn) {
        if (!this.listeners[name])
            return;
        this.listeners[name] = fn;
    };
    ;
    return Channel;
}());
export { Channel };
;
var ChannelsInstance = /** @class */ (function () {
    function ChannelsInstance() {
        this.channels = {};
        this.listeners = {
            events: function () { },
        };
    }
    ;
    ChannelsInstance.prototype.join = function (token, data, memToken) {
        if (this.channels[token])
            return this.channels[token];
        var channel = new Channel(token, data, memToken);
        this.channels[token] = channel;
        console.log("Joined channel (" + channel.name + "), sid: " + channel.token);
        return channel;
    };
    ;
    ChannelsInstance.prototype.leave = function (token) {
        if (!this.channels[token])
            return;
        var channel = this.channels[token];
        console.log("Leaved channel (" + channel.name + "), sid: " + channel.token);
        delete this.channels[token];
    };
    ;
    ChannelsInstance.prototype.leaveAll = function () {
        var list = [];
        for (var channelToken in this.channels) {
            list.push(channelToken);
        }
        ;
        for (var _i = 0, list_2 = list; _i < list_2.length; _i++) {
            var channelToken = list_2[_i];
            this.leave(channelToken);
        }
        ;
    };
    ;
    ChannelsInstance.prototype.get = function (token) {
        return this.channels[token] || null;
    };
    ;
    ChannelsInstance.prototype.on = function (name, fn) {
        if (!this.listeners[name])
            return;
        this.listeners[name] = fn;
    };
    ;
    ChannelsInstance.prototype.leaveAction = function (event) {
        var list = Array.isArray(event.list) ? event.list : [];
        for (var _i = 0, list_3 = list; _i < list_3.length; _i++) {
            var item = list_3[_i];
            Channels.leave(item.channel);
        }
        ;
    };
    ;
    ChannelsInstance.prototype.joinAction = function (event) {
        var list = Array.isArray(event.list) ? event.list : [];
        for (var _i = 0, list_4 = list; _i < list_4.length; _i++) {
            var item = list_4[_i];
            if (!item.data)
                item.data = {};
            var channel = Channels.join(item.channel, {
                name: item.data.name || "", type: item.data.type || "", token: item.channel
            }, item.memToken);
        }
        ;
    };
    ;
    ChannelsInstance.prototype.attach = function (socket) {
        var _this = this;
        socket.on("connect", function () {
            for (var channelToken in _this.channels) {
                var channel = _this.channels[channelToken];
                socket.send({ type: "fetch", channel: channel.token, token: channel.memToken });
            }
            ;
        });
        socket.on("message", function (event) {
            if (event.type == "join") {
                _this.joinAction(event);
                return;
            }
            ;
            if (event.type == "leave") {
                _this.leaveAction(event);
                return;
            }
            ;
            var channel = Channels.get(event.channel);
            if (!channel)
                return;
            if (event.type == "update") {
                socket.send({ type: "fetch", channel: event.channel, token: channel.memToken });
            }
            else if (event.type == "events") {
                var data = event.data;
                channel.events(data.token, data.list);
            }
            ;
        });
    };
    ;
    return ChannelsInstance;
}());
export { ChannelsInstance };
;
export var Channels = new ChannelsInstance();
//# sourceMappingURL=channel.js.map