const HAS_WINDOW_SUPPORT = typeof window !== 'undefined';
const HAS_DOCUMENT_SUPPORT = typeof document !== 'undefined';
const HAS_NAVIGATOR_SUPPORT = typeof navigator !== 'undefined';
const HAS_PROMISE_SUPPORT = typeof Promise !== 'undefined';
/* istanbul ignore next: JSDOM always returns false */
const HAS_MUTATION_OBSERVER_SUPPORT = typeof MutationObserver !== 'undefined' || typeof WebKitMutationObserver !== 'undefined' || typeof MozMutationObserver !== 'undefined';
const IS_BROWSER = HAS_WINDOW_SUPPORT && HAS_DOCUMENT_SUPPORT && HAS_NAVIGATOR_SUPPORT;
const WINDOW = HAS_WINDOW_SUPPORT ? window : {};
const DOCUMENT = HAS_DOCUMENT_SUPPORT ? document : {};
const NAVIGATOR = HAS_NAVIGATOR_SUPPORT ? navigator : {};
const USER_AGENT = (NAVIGATOR.userAgent || '').toLowerCase();
const IS_JSDOM = USER_AGENT.indexOf('jsdom') > 0;
const IS_IE = /msie|trident/.test(USER_AGENT);

// Determine if the browser supports the option passive for events
const HAS_PASSIVE_EVENT_SUPPORT = (() => {
  let passiveEventSupported = false;
  if (IS_BROWSER) {
    try {
      const options = {
        // This function will be called when the browser
        // attempts to access the passive property
        get passive() {
          /* istanbul ignore next: will never be called in JSDOM */
          passiveEventSupported = true;
        }
      };
      WINDOW.addEventListener('test', options, options);
      WINDOW.removeEventListener('test', options, options);
    } catch {
      /* istanbul ignore next: will never be called in JSDOM */
      passiveEventSupported = false;
    }
  }
  return passiveEventSupported;
})();
const HAS_TOUCH_SUPPORT = IS_BROWSER && ('ontouchstart' in DOCUMENT.documentElement || NAVIGATOR.maxTouchPoints > 0);
const HAS_POINTER_EVENT_SUPPORT = IS_BROWSER && Boolean(WINDOW.PointerEvent || WINDOW.MSPointerEvent);

/* istanbul ignore next: JSDOM only checks for 'IntersectionObserver' */
const HAS_INTERACTION_OBSERVER_SUPPORT = IS_BROWSER && 'IntersectionObserver' in WINDOW && 'IntersectionObserverEntry' in WINDOW &&
// Edge 15 and UC Browser lack support for `isIntersecting`
// but we an use `intersectionRatio > 0` instead
// 'isIntersecting' in window.IntersectionObserverEntry.prototype &&
'intersectionRatio' in WINDOW.IntersectionObserverEntry.prototype;

export { DOCUMENT, HAS_DOCUMENT_SUPPORT, HAS_INTERACTION_OBSERVER_SUPPORT, HAS_MUTATION_OBSERVER_SUPPORT, HAS_NAVIGATOR_SUPPORT, HAS_PASSIVE_EVENT_SUPPORT, HAS_POINTER_EVENT_SUPPORT, HAS_PROMISE_SUPPORT, HAS_TOUCH_SUPPORT, HAS_WINDOW_SUPPORT, IS_BROWSER, IS_IE, IS_JSDOM, NAVIGATOR, USER_AGENT, WINDOW };
