import { useContext } from "react";
import { GroupInformation, InformationContext } from "./InformationProvider";

export function useGroupInformation(groupId: string) : {
    groupInfo: GroupInformation, 
    markAsDeleted: () => void,
    updateGroupInformation: (groupInfo: GroupInformation) => void
} {
    const context = useContext(InformationContext);
    const {cachedGroups, updateGroupInformation} = context;
    const group : GroupInformation = cachedGroups.find((group: GroupInformation) => group.groupId == groupId.replace("#group:", ""));

    if(!context || !context.cachedGroups) {
        throw new Error("useGroupInformation must be used within a InformationProvider");
    }

    if(groupId.trim() == ""){
        throw new Error("Empty string passed to groupId with useGroupInformation hook");
    }

    const markAsDeleted = () => {
        updateGroupInformation({
            groupId: groupId,
            title: "DELETED",
            description: "No description available."
        });
    }

    return {
        groupInfo: {
            groupId: groupId,
            title: group ? group.title : "DELETED",
            description: group ? group.description : "No description available."
        },
        markAsDeleted,
        updateGroupInformation
    };
}