import { useRef } from "react";
import lodash from "lodash";
function useOptimizedMemo(factory, deps) {
    const ref = useRef();
    if (ref.current && deps.length === ref.current.deps.length && deps.every((dep, i)=>dep === ref.current.deps[i])) return ref.current.value;
    if (!ref.current || deps.length !== ref.current.deps.length) {
        const value = factory();
        ref.current = {
            deps,
            value
        };
        return value;
    }
    const depsChanged = deps.some((dep, i)=>{
        const prevDep = ref.current.deps[i];
        if (dep === prevDep) return false;
        return !lodash.isEqual(dep, prevDep);
    });
    if (depsChanged) {
        const value = factory();
        ref.current = {
            deps,
            value
        };
        return value;
    }
    ref.current.deps = deps;
    return ref.current.value;
}
export { useOptimizedMemo };
