"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDBRaw = void 0;
const react_1 = require("react");
function useDBRaw({ initialState, getter, setter: setterRaw, selector, }) {
    const [state, setState] = (0, react_1.useState)(initialState);
    (0, react_1.useEffect)(() => {
        getter().then(state => {
            if (!state) {
                setterRaw(initialState);
                return;
            }
            setState(state);
        });
        // Run once on mount
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const setter = (0, react_1.useCallback)(async (newState) => {
        const val = typeof newState === "function" ? newState(await getter()) : newState;
        setState(val);
        return setterRaw(val);
    }, [getter, setterRaw]);
    const result = (0, react_1.useMemo)(() => selector(state), [state, selector]);
    return [result, setter];
}
exports.useDBRaw = useDBRaw;
//# sourceMappingURL=useDBRaw.js.map