"use strict";
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_1 = require("lodash");
var CrossAppTransport = /** @class */ (function () {
    function CrossAppTransport() {
        var _this = this;
        this.handlers = new Map();
        this.on = function (action, callback) {
            if (!(0, lodash_1.isString)(action)) {
                throw new Error('action can only be a string');
            }
            var currentHandler = _this.handlers.has(action) ? _this.handlers.get(action) : [];
            currentHandler.push(callback);
            _this.handlers.set(action, currentHandler);
            //callback disposer
            return function () { return _this.off(action, callback); };
        };
        this.emit = function (action) {
            var args = [];
            for (var _i = 1; _i < arguments.length; _i++) {
                args[_i - 1] = arguments[_i];
            }
            var callbacks = _this.handlers.get(action);
            var results = [];
            (0, lodash_1.each)(callbacks, function (callback) {
                results.push(callback.apply(void 0, __spreadArray([], __read(args), false)));
            });
            return results;
        };
        this.off = function (action, callback) {
            var callbacks = _this.handlers.get(action);
            var callbackIndex = (0, lodash_1.indexOf)(callbacks, callback);
            if (callbackIndex === -1) {
                return;
            }
            callbacks.splice(callbackIndex, 1);
            _this.handlers.set(action, callbacks);
        };
    }
    return CrossAppTransport;
}());
exports.default = CrossAppTransport;
//# sourceMappingURL=CrossAppTransport.js.map