var MDOMPlugin = /** @class */ (function () {
    function MDOMPlugin() {
    }
    MDOMPlugin.get = function (constructorFunction, element) {
        return element[constructorFunction.defaultMountPoint];
    };
    MDOMPlugin.getRecursive = function (constructorFunction, element) {
        var plugin;
        while (element && !plugin) {
            plugin = MDOMPlugin.get(constructorFunction, element);
            element = element.parentNode;
        }
        return plugin;
    };
    MDOMPlugin.attach = function (constructorFunction, element, options) {
        if (MDOMPlugin.get(constructorFunction, element)) {
            return MDOMPlugin.internalUpdate(constructorFunction, element, options);
        }
        else {
            return MDOMPlugin.internalAttach(constructorFunction, element, options);
        }
    };
    MDOMPlugin.detach = function (constructorFunction, element) {
        var plugin = MDOMPlugin.get(constructorFunction, element);
        if (plugin) {
            plugin.detach();
            delete element[constructorFunction.defaultMountPoint];
        }
    };
    MDOMPlugin.internalAttach = function (constructorFunction, element, options) {
        var plugin = element[constructorFunction.defaultMountPoint];
        if (plugin) {
            MDOMPlugin.detach(constructorFunction, element);
        }
        plugin = new constructorFunction(element, options);
        if (this.mountPlugin(plugin)) {
            element[constructorFunction.defaultMountPoint] = plugin;
            return plugin;
        }
        else {
            return new NullObjectDomPlugin(options);
        }
    };
    MDOMPlugin.internalUpdate = function (constructorFunction, element, options) {
        var plugin = element[constructorFunction.defaultMountPoint];
        if (plugin) {
            if (!this.refreshPlugin(plugin, options)) {
                MDOMPlugin.detach(constructorFunction, element);
                return new NullObjectDomPlugin(options);
            }
        }
        return plugin;
    };
    MDOMPlugin.mountPlugin = function (plugin) {
        var mounted = false;
        plugin.attach(this.getMountFunction(function () { return mounted = true; }));
        return mounted;
    };
    MDOMPlugin.refreshPlugin = function (plugin, options) {
        var updated = false;
        plugin.update(options, this.getMountFunction(function () { return updated = true; }));
        return updated;
    };
    MDOMPlugin.getMountFunction = function (onSuccess) {
        return function (callback) {
            callback();
            onSuccess();
        };
    };
    return MDOMPlugin;
}());
export { MDOMPlugin };
var NullObjectDomPlugin = /** @class */ (function () {
    function NullObjectDomPlugin(options) {
        this.options = options;
    }
    NullObjectDomPlugin.prototype.attach = function (mount) { };
    NullObjectDomPlugin.prototype.update = function (options, refresh) { };
    NullObjectDomPlugin.prototype.detach = function () { };
    NullObjectDomPlugin.prototype.addEventListener = function (eventName, listener) { };
    NullObjectDomPlugin.prototype.removeEventListener = function (eventName, listener) { };
    NullObjectDomPlugin.prototype.removeAllEvents = function () { };
    return NullObjectDomPlugin;
}());
var MElementDomPlugin = /** @class */ (function () {
    function MElementDomPlugin(element, options) {
        this.attachedEvents = new Map();
        this._element = element;
        this._options = options;
    }
    Object.defineProperty(MElementDomPlugin.prototype, "element", {
        get: function () { return this._element; },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(MElementDomPlugin.prototype, "options", {
        get: function () { return this._options; },
        enumerable: false,
        configurable: true
    });
    MElementDomPlugin.prototype.addEventListener = function (eventName, listener, useCapture) {
        var listeners = this.attachedEvents.get(eventName);
        if (!listeners) {
            this.attachedEvents.set(eventName, [listener]);
            this.element.addEventListener(eventName, listener, useCapture);
        }
        else {
            if (listeners.indexOf(listener) === -1) {
                listeners.push(listener);
                this.element.addEventListener(eventName, listener, useCapture);
            }
            this.attachedEvents.set(eventName, listeners);
        }
    };
    MElementDomPlugin.prototype.removeEventListener = function (eventName) {
        var _this = this;
        var listeners = this.attachedEvents.get(eventName);
        if (!listeners) {
            return;
        }
        listeners.forEach(function (listener) {
            _this.element.removeEventListener(eventName, listener);
        });
        this.attachedEvents.delete(eventName);
    };
    MElementDomPlugin.prototype.removeAllEvents = function () {
        var _this = this;
        this.attachedEvents
            .forEach(function (_listeners, eventName) { return _this.removeEventListener(eventName); });
    };
    return MElementDomPlugin;
}());
export { MElementDomPlugin };
//# sourceMappingURL=domPlugin.js.map