import { useEffect, useState } from "react";
import useAsyncControl from "../useAsyncControl";
/**
 * It can assume 3 states
 * [undefined] = The hook has just initialized, and will call the memoize function
 * [T] = The hook initialized the value
 * [null, error] = The hook failed to initialize and the error is available
 */
export default function useAsyncMemo(funcToMemoize, depArr) {
    const { process, error, ...control } = useAsyncControl();
    const [memo, setMemo] = useState();
    useEffect(() => {
        let depBasedSet = setMemo;
        process(funcToMemoize)
            .then((whatToStore) => depBasedSet?.(() => whatToStore))
            .catch(() => depBasedSet?.(null));
        return () => {
            depBasedSet = undefined;
        };
    }, depArr);
    return [
        memo,
        error,
        control.loading,
        () => process(funcToMemoize)
            .then((whatToStore) => {
            setMemo(() => whatToStore);
            return whatToStore;
        })
            .catch(() => setMemo(null)),
        setMemo,
    ];
}
//# sourceMappingURL=useAsyncMemo.js.map