"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var global_1 = require("@dojo/framework/shim/global");
var vdom_1 = require("@dojo/framework/core/vdom");
var diff_1 = require("@dojo/framework/core/diff");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var factory = vdom_1.create({
    destroy: vdom_1.destroy,
    diffProperty: vdom_1.diffProperty,
    icache: icache_1.createICacheMiddleware()
}).properties();
exports.GlobalEvent = factory(function (_a) {
    var children = _a.children, properties = _a.properties, _b = _a.middleware, destroy = _b.destroy, diffProperty = _b.diffProperty, icache = _b.icache;
    var registerListeners = function (type, previousListeners, newListeners) {
        var currentListeners = icache.getOrSet('listeners', {
            window: {},
            document: {}
        });
        var registeredListeners = {};
        previousListeners &&
            Object.keys(previousListeners).forEach(function (eventName) {
                var newListener = newListeners ? newListeners[eventName] : undefined;
                if (newListener === undefined) {
                    global_1.default[type].removeEventListener(eventName, currentListeners[type][eventName]);
                }
                else if (previousListeners[eventName] !== newListener) {
                    global_1.default[type].removeEventListener(eventName, currentListeners[type][eventName]);
                    global_1.default[type].addEventListener(eventName, newListener);
                    registeredListeners[eventName] = newListener;
                }
                else {
                    registeredListeners[eventName] = newListener;
                }
            });
        newListeners &&
            Object.keys(newListeners).forEach(function (eventName) {
                if (previousListeners === undefined || previousListeners[eventName] === undefined) {
                    global_1.default[type].addEventListener(eventName, newListeners[eventName]);
                    registeredListeners[eventName] = newListeners[eventName];
                }
            });
        currentListeners[type] = registeredListeners;
        icache.set('listeners', currentListeners);
    };
    diffProperty('window', properties, function (previous, next) {
        var changed = diff_1.shallow(previous.window, next.window).changed;
        changed && registerListeners('window', previous.window, next.window);
    });
    diffProperty('document', properties, function (previous, next) {
        var changed = diff_1.shallow(previous.document, next.document).changed;
        changed && registerListeners('document', previous.document, next.document);
    });
    var removeAllRegisteredListeners = function (type) {
        var currentListeners = icache.getOrSet('listeners', {
            window: {},
            document: {}
        });
        Object.keys(currentListeners[type]).forEach(function (eventName) {
            global_1.default[type].removeEventListener(eventName, currentListeners[type][eventName]);
        });
    };
    destroy(function () {
        removeAllRegisteredListeners('window');
        removeAllRegisteredListeners('document');
    });
    if (children().length > 0) {
        return children();
    }
    return null;
});
exports.default = exports.GlobalEvent;

/*# sourceMappingURL=index.js.map*/