"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var throttle_debounce_1 = require("throttle-debounce"); var util_1 = require("./misc/util"); var defaultEvents = ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel']; var oneMinute = 60e3; var useIdle = function (ms, initialState, events) { if (ms === void 0) { ms = oneMinute; } if (initialState === void 0) { initialState = false; } if (events === void 0) { events = defaultEvents; } var _a = react_1.useState(initialState), state = _a[0], setState = _a[1]; react_1.useEffect(function () { var mounted = true; var timeout; var localState = state; var set = function (newState) { if (mounted) { localState = newState; setState(newState); } }; var onEvent = throttle_debounce_1.throttle(50, function () { if (localState) { set(false); } clearTimeout(timeout); timeout = setTimeout(function () { return set(true); }, ms); }); var onVisibility = function () { if (!document.hidden) { onEvent(); } }; for (var i = 0; i < events.length; i++) { util_1.on(window, events[i], onEvent); } util_1.on(document, 'visibilitychange', onVisibility); timeout = setTimeout(function () { return set(true); }, ms); return function () { mounted = false; for (var i = 0; i < events.length; i++) { util_1.off(window, events[i], onEvent); } util_1.off(document, 'visibilitychange', onVisibility); }; }, [ms, events]); return state; }; exports.default = useIdle;