import {Exam} from "@/interfaces/exam";
import {Permission, PermissionType} from "@/interfaces/permissions";
import {ExamState} from "@/stores/examStore";
import axios from "axios";
import {useEffect, useState} from "react";

export default function usePermissions(user: string) {
	const [permissions, setPermissions] = useState<PermissionType[]>([]);
	const [isLoading, setIsLoading] = useState(false);
	const [isError, setIsError] = useState(false);

	const getData = () => {
		setIsLoading(true);
		axios
			.get<Permission[]>(`/api/permissions`)
			.then((response) => {
				const permissionTypes = response.data
					.filter((x) => !x.users.includes(user))
					.reduce((acc, curr) => [...acc, curr.type], [] as PermissionType[]);
				setPermissions(permissionTypes);
			})
			.finally(() => setIsLoading(false));
	};

	useEffect(getData, [user]);

	return {permissions, isLoading, isError, reload: getData};
}
