"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAutoSave = exports.useRenderTime = exports.useComponentWillMount = exports.useElementState = void 0;
exports.useBooleanState = useBooleanState;
exports.useDebounce = useDebounce;
const react_1 = require("react");
const react_use_1 = require("react-use");
const lodash_1 = require("../lodash.js");
const useElementState = () => (0, react_1.useState)(null);
exports.useElementState = useElementState;
function useBooleanState(initial) {
    const [value, setValue] = (0, react_1.useState)(initial);
    return [
        value,
        (0, react_1.useCallback)(() => setValue(true), []),
        (0, react_1.useCallback)(() => setValue(false), []),
        (0, react_1.useCallback)(() => setValue((val) => !val), []),
    ];
}
const useComponentWillMount = (cb) => {
    const willMount = (0, react_1.useRef)(true);
    if (willMount.current)
        cb();
    willMount.current = false;
};
exports.useComponentWillMount = useComponentWillMount;
const useRenderTime = (cb) => {
    let time = 0;
    (0, exports.useComponentWillMount)(() => {
        time = Date.now();
    });
    (0, react_use_1.useEffectOnce)(() => {
        cb(Date.now() - time);
    });
};
exports.useRenderTime = useRenderTime;
function useDebounce(cb, wait) {
    const latestCb = (0, react_use_1.useLatest)(cb);
    const debouncedFn = (0, react_1.useMemo)(() => (0, lodash_1.debounce)((...args) => {
        latestCb.current.apply(null, args);
    }, wait), [latestCb, wait]);
    // cancel function on unmount
    (0, react_1.useEffect)(() => () => debouncedFn.cancel(), [debouncedFn]);
    return debouncedFn;
}
var useAutoSave_1 = require("./hooks/useAutoSave.js");
Object.defineProperty(exports, "useAutoSave", { enumerable: true, get: function () { return useAutoSave_1.useAutoSave; } });
//# sourceMappingURL=hooks.js.map
