import { s as setMode } from './index-102a6ed2.js';
import { U as UUID } from './uuid-a982202a.js';
import { F as FileManager } from './file-manager-eda33a99.js';
var setColorScheme = function (scheme) {
    if (scheme === 'dark' || scheme === 'light') {
        localStorage.setItem('bce-color-scheme', scheme);
        document.documentElement.setAttribute('color-scheme', scheme);
    }
    else {
        localStorage.removeItem('bce-color-scheme');
        var match = window.matchMedia('(prefers-color-scheme: dark)');
        handleMediaQuery(match);
    }
};
var bce = {
    file: FileManager.inMemory(),
    setColorScheme: setColorScheme,
    generateId: function () { return UUID.v4(); },
    FileManager: FileManager
};
var main = function () {
    // Setup global config
    window.BCE = window.BCE || bce;
    // Detect preferred color scheme
    var match = window.matchMedia('(prefers-color-scheme: dark)');
    match.addEventListener
        ? match.addEventListener('change', handleMediaQuery)
        : match.addListener(handleMediaQuery);
    handleMediaQuery(match);
    setMode(function (el) {
        // Component specific mode's (e.g. bce-button within bce-fab)
        if (chain(el, 'bce-button', 'bce-fab'))
            return 'bce-fab';
        if (chain(el, 'bce-button', 'bce-menu'))
            return 'bce-menu';
        if (chain(el, 'bce-link', 'bce-link'))
            return 'bce-link';
        if (chain(el, 'bce-nav', 'bce-header'))
            return 'bce-header';
        if (chain(el, 'bce-nav', 'bce-side-bar'))
            return 'bce-side-bar';
        var root = el.closest('bce-root');
        return el.getAttribute('mode') || (root && root.mode) || 'default';
    });
};
var chain = function (el, child) {
    var parents = [];
    for (var _i = 2; _i < arguments.length; _i++) {
        parents[_i - 2] = arguments[_i];
    }
    if (!el.matches(child))
        return false;
    for (var _a = 0, parents_1 = parents; _a < parents_1.length; _a++) {
        var parent = parents_1[_a];
        var cur = el.parentElement;
        if (!cur || !cur.matches(parent))
            return false;
        el = cur;
    }
    return true;
};
var handleMediaQuery = function (event) {
    switch (event.media) {
        case '(prefers-color-scheme: dark)':
            var preference = localStorage.getItem('bce-color-scheme');
            var color = preference || (event.matches ? 'dark' : 'light');
            document.documentElement.setAttribute('color-scheme', color);
            break;
    }
};
var globalScripts = main;
export { globalScripts as g };
