/**
 * Global helpers
 *
 */
const pageFocusElements = {};
export const setPageFocusElement = function setPageFocusElement(selectorOrElement) {
  let key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  pageFocusElements[key] = selectorOrElement;
};
export const applyPageFocus = function applyPageFocus() {
  let key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'default';

  try {
    let element = pageFocusElements[key];

    if (typeof element === 'string' && typeof document !== 'undefined') {
      element = document.querySelector(element);
    } else if (!element && typeof document !== 'undefined') {
      element = document.querySelector('.dnb-no-focus');
    }

    if (element instanceof HTMLElement) {
      if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'div', 'main', 'nav', 'header', 'footer', 'aside', 'section', 'article'].includes(String(element.nodeName).toLowerCase())) {
        if (!element.hasAttribute('tabindex')) {
          element.setAttribute('tabindex', '-1');
        }

        if (element.classList && !element.classList.contains('dnb-no-focus')) {
          element.classList.add('dnb-no-focus');
        }
      }

      element.focus({
        preventScroll: true
      });
    }
  } catch (e) {
    console.log('Error on applyPageFocus:', e);
  }
};
export const scrollToLocationHashId = function scrollToLocationHashId() {
  let {
    offset = 0
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

  if (typeof document !== 'undefined' && typeof window !== 'undefined') {
    const id = String(window.location.hash).replace('#', '');
    const elem = document.getElementById(id);

    if (elem instanceof HTMLElement) {
      const top = parseFloat(elem.offsetTop) - offset;

      try {
        window.scrollTop = top;
        window.scrollTo({
          top,
          behavior: 'smooth'
        });
      } catch (e) {
        console.log('Error on scrollToLocationHashId:', e);
      }
    }
  }
};
export const isIE11 = typeof window !== 'undefined' && typeof document !== 'undefined' ? !!window.MSInputMethodContext && !!document.documentMode : false;