import { AUTH_FLOW_STATUS } from "../../auth/state";
export const authorize = async ({ state, actions, effects }, params) => {
    params?.token && effects.api.updateToken(params.token);
    state.auth.status = Math.max(AUTH_FLOW_STATUS.AUTHORIZING, state.auth.status);
    try {
        state.api.auth.user = await effects.api.authorize();
    }
    catch (e) {
        if (e &&
            e.error.statusCode === 404 &&
            state.auth.authenticated &&
            window.localStorage.getItem("isSigningInProgress") === "true") {
            window.localStorage.setItem("isSigningUserUknown", "true");
            await actions.routing.historyPush({ location: "/signout" });
        }
    }
    const user = state.api.auth.user;
    state.api.auth.attempted = true;
    if (!user || !user.id) {
        state.auth.status = state.firebase.user ? AUTH_FLOW_STATUS.AUTHENTICATED : AUTH_FLOW_STATUS.ANONYMOUS;
        if (!state.routing.isAllowed)
            actions.routing.historyPush({ location: "/" });
        return;
    }
    state.auth.status = state.api.auth.user?.username ? AUTH_FLOW_STATUS.AUTHORIZED : AUTH_FLOW_STATUS.AUTHENTICATED;
    if (!user.username)
        return;
    actions.newcoin.getAccountBalance({ user });
    actions.newcoin.getPoolInfo({ pool: { owner: user.username } });
    if (!state.lists.top.moods.items.length) {
        actions.lists.top.moods({});
        actions.lists.top.users({});
        actions.lists.top.posts();
    }
    setTimeout(() => {
        actions.routing.routeAfterAuth();
    });
};
export const logout = async ({ state, actions, effects }, config) => {
    if (!config?.keepFbUser && state.firebase) {
        state.firebase.token = "";
        state.firebase.user = null;
    }
    state.api.auth.user = {};
    state.api.cache.stakeHistory = [];
    effects.api.updateToken("");
    effects.websockets.newlife.socket?.close();
};
//# sourceMappingURL=auth.js.map