var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
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));
};
export var eventListenersMap = new Map();
export function addResponsiveLayoutListener(globalObject, mediaQuery, callback) {
    if (eventListenersMap.has(mediaQuery)) {
        addCallbackToMQListener(mediaQuery, callback);
    }
    else {
        createMQListener(globalObject, mediaQuery, callback);
    }
    return {
        remove: function () {
            removeCallbackFromMQListener(mediaQuery, callback);
        },
    };
}
function addCallbackToMQListener(mediaQuery, callback) {
    var eventListener = eventListenersMap.get(mediaQuery);
    if (eventListener) {
        eventListenersMap.set(mediaQuery, __assign(__assign({}, eventListener), { listeners: __spreadArray(__spreadArray([], eventListener.listeners, true), [callback], false) }));
    }
}
function createMQListener(globalObject, mediaQuery, callback) {
    var _a;
    var mql = (_a = globalObject.matchMedia) === null || _a === void 0 ? void 0 : _a.call(globalObject, mediaQuery);
    if (mql) {
        var newMediaQueryInfo = { mql: mql, listeners: [callback] };
        eventListenersMap.set(mediaQuery, newMediaQueryInfo);
        if (mql.addEventListener) {
            mql.addEventListener('change', changeCallback);
        }
        else {
            mql.addListener(changeCallback);
        }
    }
}
function removeCallbackFromMQListener(mediaQuery, callback) {
    if (eventListenersMap.has(mediaQuery)) {
        var eventListener = eventListenersMap.get(mediaQuery);
        if (eventListener) {
            var newListeners = eventListener.listeners.filter(function (listener) { return listener !== callback; });
            if (newListeners.length === 0) {
                if (eventListener.mql.removeEventListener) {
                    eventListener.mql.removeEventListener('change', changeCallback);
                }
                else {
                    eventListener.mql.removeListener(changeCallback);
                }
                eventListenersMap.delete(mediaQuery);
                return;
            }
            eventListenersMap.set(mediaQuery, __assign(__assign({}, eventListener), { listeners: __spreadArray([], newListeners, true) }));
        }
    }
}
export function checkMatches(globalObject, mediaQuery) {
    var _a, _b;
    if (!globalObject.matchMedia) {
        return false;
    }
    if (!eventListenersMap.has(mediaQuery)) {
        return !!((_a = globalObject.matchMedia(mediaQuery)) === null || _a === void 0 ? void 0 : _a.matches);
    }
    var eventListener = eventListenersMap.get(mediaQuery);
    return (_b = eventListener === null || eventListener === void 0 ? void 0 : eventListener.mql.matches) !== null && _b !== void 0 ? _b : false;
}
function changeCallback(e) {
    var eventListener = eventListenersMap.get(e.media);
    if (eventListener) {
        eventListener.listeners.forEach(function (listener) {
            listener(e);
        });
    }
}
//# sourceMappingURL=ResponsiveLayoutEvents.js.map