import { useActions, useAppState } from "../overmind";
import { useEffect } from "react";
import { useLiveQuery } from "dexie-react-hooks";
export const useCachedUser = (user, force) => {
    const state = useAppState();
    const actions = useActions();
    const byIdOrUsername = (u) => {
        const cachedItem = u?.id
            ? state.api.cache.users.byId[u?.id]
            : u?.username
                ? state.api.cache.users.byUsername[u?.username]
                : {};
        return cachedItem && cachedItem.id && (cachedItem.username || cachedItem.fullName) ? cachedItem : null;
    };
    useEffect(() => {
        if ((user?.id || user?.username) && state.auth.authenticated && (force || !byIdOrUsername(user))) {
            actions.api.user.read(user);
            // if (force && !byIdOrUsername(user)?.moods?.length)
            //     actions.api.user.getMoods(user);
        }
    }, [state.auth.authenticated, user?.id || "", user?.username || ""]);
    const u = byIdOrUsername(user);
    return { ...(u || {}), moods: u?.moods };
};
export const useCachedPost = ({ id }, force) => {
    const state = useAppState();
    const actions = useActions();
    useEffect(() => {
        id && state.auth.authenticated && (force || !state.api.cache.posts[id]) && actions.api.post.read({ id });
    }, [state.auth.authenticated, id]);
    return (id && state.api.cache.posts[id]) || {};
};
export const useCachedPosts = (posts, force) => {
    const state = useAppState();
    const actions = useActions();
    const ids = posts.map((p) => p.id);
    const key = ids.join(",");
    const cachedPosts = //= [];
     useLiveQuery(() => {
        const should = (state.auth.authenticated && state.cache.ready && posts?.length) || null;
        const f = should && state.cache.db.nodes["post"];
        const bg = f
            ?.where("id")
            .anyOf(posts.map((p) => p.id))
            .toArray();
        return bg;
    }, [key, state.cache.ready, state.auth.authenticated]);
    return cachedPosts || []; //(posts?.length && state.api.cache.posts[id]) || {};
};
export const useCachedMood = ({ id }, force) => {
    const state = useAppState();
    const actions = useActions();
    useEffect(() => {
        id &&
            state.auth.authenticated &&
            (force || !state.api.cache.moods[id]) && //|| !state.api.cache.moods[id].posts?.length) &&
            actions.api.mood.read({ id });
    }, [state.auth.authenticated, id]);
    return (id && state.api.cache.moods[id]) || {};
};
export const useCachedMoodPosts = ({ id }, force) => {
    const state = useAppState();
    const actions = useActions();
    const cachedFolder = useLiveQuery(() => {
        const f = state.cache.db.nodes["folder"];
        return f?.where({ id }).toArray();
    }, [id, state.cache.ready, state.auth.authenticated]);
    const cachedFolderPostsEdges = useLiveQuery(() => {
        const w = state.cache.ready && state.cache.db.edges.where;
        // if (!w) return [];
        const wr = state.cache.db.edges.where("__outE").startsWith(["folder", id, "post"].join("+")).toArray();
        if (!wr)
            return [];
        return wr;
    }, [state.cache.ready, id, state.auth.authenticated]);
    useEffect(() => {
        id &&
            state.auth.authenticated &&
            state.cache.ready &&
            (force || !state.api.cache.moods[id]) && //|| !state.api.cache.moods[id].posts?.length) &&
            actions.api.mood.getPosts({ id });
    }, [state.cache.ready, state.auth.authenticated, id]);
    const posts = useCachedPosts((cachedFolderPostsEdges || []).map((p) => ({ id: p.toId || "" })));
    return { ...(cachedFolder || [])[0], posts: posts || [] }; //(id && state.api.cache.moods[id]) || {};
};
export const useCachedMoods = (moods, force) => {
    const state = useAppState();
    const actions = useActions();
    const moodsCollector = [];
    useEffect(() => {
        const res = [];
        moods &&
            moods.length &&
            state.auth.authenticated &&
            Promise.all(moods
                .map(({ id }) => id &&
                (force || !state.api.cache.moods[id] || !state.api.cache.moods[id].posts?.length) &&
                actions.api.mood.read({ id }))
                .filter(Boolean)).then((r) => r.reduce((m) => m));
    }, [state.auth.authenticated, moods]);
    return moodsCollector;
};
//# sourceMappingURL=useCached2.js.map