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);
};
import { createRef, useCallback, useContext, useEffect, useState } from 'react';
import { useGlobal } from '../../lib/renderEnvironment/index.js';
import { ThemeContext } from '../../lib/theming/ThemeContext.js';
import { addResponsiveLayoutListener, checkMatches } from './ResponsiveLayoutEvents.js';
export function useResponsiveLayout(_a) {
    var _b = _a === void 0 ? {} : _a, customMediaQueries = _b.customMediaQueries;
    var theme = useContext(ThemeContext);
    var globalObject = useGlobal();
    var allMediaQueries = Object.entries(__assign({ isMobile: theme.mobileMediaQuery }, customMediaQueries)).map(function (_a) {
        var key = _a[0], value = _a[1];
        return ({
            flag: key,
            query: value,
            ref: createRef(),
        });
    });
    var getLayoutFromGlobal = function () {
        return allMediaQueries.reduce(function (result, mediaQuery) {
            var _a;
            return Object.assign(result, (_a = {}, _a[mediaQuery.flag] = checkMatches(globalObject, mediaQuery.query), _a));
        }, {});
    };
    var _c = useState(getLayoutFromGlobal()), state = _c[0], setState = _c[1];
    var prepareMediaQueries = useCallback(function () {
        if (!theme) {
            return;
        }
        allMediaQueries.forEach(function (mediaQuery) {
            return (mediaQuery.ref.current = addResponsiveLayoutListener(globalObject, mediaQuery.query, checkLayoutsMediaQueries));
        });
        // Checking for SSR use case
        var globalLayout = getLayoutFromGlobal();
        var hasChangedQuery = Object.entries(globalLayout).find(function (_a) {
            var key = _a[0], value = _a[1];
            return state[key] !== value;
        });
        if (hasChangedQuery) {
            setState(globalLayout);
        }
    }, [theme]);
    var checkLayoutsMediaQueries = useCallback(function (e) {
        if (!theme) {
            return;
        }
        allMediaQueries.forEach(function (mediaQuery) {
            if (e.media === mediaQuery.query) {
                setState(function (prevState) {
                    var _a;
                    return (__assign(__assign({}, prevState), (_a = {}, _a[mediaQuery.flag] = e.matches, _a)));
                });
            }
        });
    }, [theme]);
    useEffect(function () {
        prepareMediaQueries();
        return function () {
            allMediaQueries.forEach(function (mediaQuery) { var _a; return (_a = mediaQuery.ref.current) === null || _a === void 0 ? void 0 : _a.remove(); });
        };
    }, []);
    return state;
}
//# sourceMappingURL=useResponsiveLayout.js.map