import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import "core-js/modules/web.timers.js";
import { useCallback, useEffect, useState } from 'react';
import useMeasure from 'react-use-measure';

// FIXME: Files in 'lib' are commonjs modules.
//        There's no variable binding unless you reference the whole module.
import * as theme_ from '../theme/generate';
export var useUnmount = function useUnmount(callback) {
  useEffect(function () {
    return callback;
  }, []);
};
export var LanguageName = {
  zh: '简体中文',
  en: 'English'
};
export var useTheme = function useTheme() {
  var _useState = useState(theme_.themeMode),
    _useState2 = _slicedToArray(_useState, 2),
    theme = _useState2[0],
    setTheme = _useState2[1];
  var toggleTheme = useCallback(function () {
    var nextTheme = theme_.themeMode === 'light' ? 'dark' : 'light';
    theme_.setThemeMode(nextTheme);
    setTheme(theme_.themeMode);
    theme_.generateTheme();
    document.documentElement.style.colorScheme = nextTheme;
  }, []);
  return [theme, toggleTheme];
};
export var useWindowFocus = function useWindowFocus() {
  var _useState3 = useState(true),
    _useState4 = _slicedToArray(_useState3, 2),
    focus = _useState4[0],
    setFocus = _useState4[1];
  useEffect(function () {
    var onFocus = function onFocus() {
      return setFocus(true);
    };
    var onBlur = function onBlur() {
      return setFocus(false);
    };
    window.addEventListener('focus', onFocus);
    window.addEventListener('blur', onBlur);
    return function () {
      window.removeEventListener('focus', onFocus);
      window.removeEventListener('blur', onBlur);
    };
  }, []);
  return focus;
};
export var useDelayedValue = function useDelayedValue(realValue, delay) {
  var _useState5 = useState(null),
    _useState6 = _slicedToArray(_useState5, 2),
    value = _useState6[0],
    setValue = _useState6[1];
  useEffect(function () {
    var timer = setTimeout(function () {
      return setValue(realValue);
    }, delay);
    return function () {
      return clearTimeout(timer);
    };
  }, [realValue, delay]);
  return value;
};
// Delay `useMeasure()`'s refreshing, improve re-layout performance.
// Also a workaround for 'loop completed with undelivered notifications' error.
// Since we only need width and height, other properties are hidden from type.
export var useDelayedMeasure = function useDelayedMeasure() {
  var delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  var _useMeasure = useMeasure(),
    _useMeasure2 = _slicedToArray(_useMeasure, 2),
    ref = _useMeasure2[0],
    bounds_ = _useMeasure2[1];
  var bounds = useDelayedValue(bounds_, delay) || {
    width: 0,
    height: 0
  };
  return [ref, bounds];
};