export const initStore = (_, { name }) => { };
const put = async (table, value) => {
    try {
        // await table.put(json(value));
    }
    catch (ex) {
        console.log(ex);
        throw ex;
    }
};
export const store = async ({ state }, { label, value }) => {
    return put(state.cache.db.nodes[label], value);
};
export const storeMultiple = async ({ state }, { label, value }) => {
    const t = state.cache.db.nodes[label];
    Promise.all(value.map((d) => put(t, d)));
};
export const storeEdge = async ({ effects }, { fromLabel, toLabel, from, to, value }) => {
    return put(effects.cache.edges(), {
        __outE: [fromLabel, from.id, toLabel, to.id].join("+"),
        __inE: [toLabel, to.id, fromLabel, from.id].join("+"),
        fromLabel,
        from,
        toLabel,
        to,
        value,
    });
};
export const storeEdgeMultiple = async ({ effects }, { fromLabel, toLabel, from, to, value }) => {
    const tbl = effects.cache.edges();
    await Promise.all(from.flatMap((f) => to.map((t) => {
        const __outE = [fromLabel, f.id, toLabel, t.id].join("+");
        const __inE = [toLabel, t.id, fromLabel, f.id].join("+");
        return put(tbl, { __outE, __inE, value, fromLabel, fromId: f.id, toLabel, toId: t.id });
    })));
    return;
};
export const getPost = async ({ effects }, post) => {
    return await effects.cache.db()["posts"].get({ id: post.id });
};
export const onInitializeOvermind = async ({ effects, state, actions, reaction }) => {
    effects.cache.init("iosdk-cache", {
        // [EDGES_TABLE_NAME]: "++id,fromLabel,toLabel,fromId,toId",
        post: "id,created,author.username",
        user: "id,created,username",
        folder: "id,created,username",
    });
    const db = effects.cache.db();
    await effects.cache.open();
    db.db.on("ready", () => {
        state.cache.ready = true;
    });
    // setTimeout(() => {
    state.cache._db = () => db;
    // });
    // state.cache._edges = () => db[EDGES_TABLE_NAME];
};
// moods: "id,created,author.username",
// userMoods: "id,username",
//   const id = "test-124"; //Date.now().toString();
//   await actions.cache.cachePost({ post: { id, created: id, title: "Monkey gone to heaven" } });
//   const r = await actions.cache.getPost({ id });
//   console.log(r);
// export const cachePost: Action<{ label: PostReadResponse }> = async ({ state }, { post }) => {
//   try {
//     await state.cache.db["posts"].put(post);
//   } catch (ex) {
//     console.log(ex);
//   }
// };
//# sourceMappingURL=actions.js.map