import { s as setMode } from './index-f5364a73.js';
import { U as UUID, g as getColorShade, s as setColorShade } from './color-6a39339f.js';
import { F as FileManager, S as ScrollSpy } from './scroll-spy-63b7ab76.js';

const handleMediaQuery = (event) => {
    if (event.media !== '(prefers-color-scheme: dark)')
        return;
    const pref = getColorScheme();
    const color = pref === 'auto' ? (event.matches ? 'dark' : 'light') : pref;
    document.documentElement.setAttribute('color-scheme', color);
};
const initColorScheme = () => {
    const match = window.matchMedia('(prefers-color-scheme: dark)');
    match.addEventListener
        ? match.addEventListener('change', handleMediaQuery)
        : match.addListener(handleMediaQuery);
    handleMediaQuery(match);
};
const getColorScheme = () => {
    const preference = localStorage.getItem('bce-color-scheme');
    return preference ? preference : 'auto';
};
const setColorScheme = (scheme) => {
    if (scheme === 'dark' || scheme === 'light') {
        localStorage.setItem('bce-color-scheme', scheme);
        document.documentElement.setAttribute('color-scheme', scheme);
    }
    else {
        localStorage.removeItem('bce-color-scheme');
        const match = window.matchMedia('(prefers-color-scheme: dark)');
        handleMediaQuery(match);
    }
};

var bce = {
    file: FileManager.inMemory(),
    FileManager: FileManager,
    generateId: function () { return UUID.v4(); },
    getColorScheme: getColorScheme,
    getColorShade: getColorShade,
    setColorScheme: setColorScheme,
    setColorShade: setColorShade,
    ScrollSpy: ScrollSpy
};
var main = function () {
    // Setup global config
    window.BCE = window.BCE || bce;
    initColorScheme();
    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-link', 'bce-nav', 'bce-header'))
            return 'bce-header';
        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;
};

const globalScripts = main;

export { globalScripts as g };
