import {defineStore} from "pinia";
import type Group from "@/mode/Group";
import GroupApi from "@/api/GroupApi";
// @ts-ignore
import Tools from "@/utils/Tools";
import GroupInviteApi from "@/api/GroupInviteApi";

export interface IState {
    groupList: Array<Group>;
    waitCheckMap: Map<string, number>;
    groupIndexMap: Map<string, Object>;
}

export const useGroupStore = defineStore({
    id: "group_store",
    state: (): IState => ({
        groupList: [],
        waitCheckMap: new Map<string, number>(),
        groupIndexMap: new Map<string, Object>()
    }),
    getters: {
        getGroupById: (state) => (id: string) => {
            return state.groupIndexMap.get(id);
        },
        getGroupList(): Array<Group> {
            return this.groupList;
        },
    },
    actions: {
        setGroupList(groupList: Array<Group>): Array<Group> {
            return (this.groupList = groupList);
        },
        loadWaitCheckList() {
            this.waitCheckMap = new Map<string, number>();
            GroupInviteApi.waitCheckList().then(res => {
                res.data.forEach((value: any) => {
                    this.waitCheckMap.set(value.groupId, value.count);
                })
            });
        },
        loadGroupList() {
            GroupApi.list().then(res => {
                this.groupList = res.data
                this.groupIndexMap = Tools.listToMap(res.data);
            })
        },
    },
});
