'use strict';

const index = require('./index-578e90b1.js');
const color = require('./color-e75007c0.js');
const scrollSpy = require('./scroll-spy-e6c0bfcc.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: scrollSpy.FileManager.inMemory(),
    FileManager: scrollSpy.FileManager,
    generateId: function () { return color.UUID.v4(); },
    getColorScheme: getColorScheme,
    getColorShade: color.getColorShade,
    setColorScheme: setColorScheme,
    setColorShade: color.setColorShade,
    ScrollSpy: scrollSpy.ScrollSpy
};
var main = function () {
    // Setup global config
    window.BCE = window.BCE || bce;
    initColorScheme();
    index.setMode(function (el) {
        // Component specific mode's (e.g. bce-button within bce-fab)
        if (chain(el, 'bce-button', 'bce-fab'))
            return 'fab';
        if (chain(el, 'bce-button', 'bce-menu'))
            return 'menu';
        if (chain(el, 'bce-link', 'bce-link'))
            return 'link';
        if (chain(el, 'bce-link', 'bce-nav', 'bce-header'))
            return 'header';
        if (chain(el, 'bce-nav', 'bce-header'))
            return 'header';
        if (chain(el, 'bce-nav', 'bce-side-bar'))
            return 'sidebar';
        var root = el.closest('bce-root');
        return el.getAttribute('mode') || (root && root.mode) || 'regular';
    });
};
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;

exports.globalScripts = globalScripts;
