import { Code, Group, User } from "@/interfaces/user";
import axios from "axios";
import { useEffect, useState } from "react";

export default function useEntitiesCodes(entities?: string[]) {
	const [codes, setCodes] = useState<Code[]>([]);
	const [isLoading, setIsLoading] = useState(false);
	const [isError, setIsError] = useState(false);

	const getData = () => {
		setIsLoading(true);
		const params = new URLSearchParams()
		if (entities)
			entities.forEach(e => params.append("entities", e))

		axios
			.get<Code[]>(`/api/code/entities${entities ? `?${params.toString()}` : ""}`)
			.then((response) => setCodes(response.data))
			.finally(() => setIsLoading(false));
	};

	useEffect(getData, [entities]);

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