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

export default function useInvites({
  from,
  to,
}: {
  from?: string;
  to?: string;
}) {
  const [invites, setInvites] = useState<Invite[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [isError, setIsError] = useState(false);

  const getData = () => {
    const filters: ((i: Invite) => boolean)[] = [];
    if (from) filters.push((i: Invite) => i.from === from);
    if (to) filters.push((i: Invite) => i.to === to);

    setIsLoading(true);
    axios
      .get<Invite[]>(`/api/invites`)
      .then((response) =>
        setInvites(filters.reduce((d, f) => d.filter(f), response.data)),
      )
      .finally(() => setIsLoading(false));
  };

  useEffect(getData, [to, from]);

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