import { useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";

import { QueryKeys } from "../constants/query-keys";
import { UsersService } from "../services/api";
import { IUserProfile } from "../types/user";

const useOnlineUser = () => {
  const [onlineUser, setOnlineUser] = useState<IUserProfile>();

  const { data: onlineData, refetch: getUser } = useQuery({
    queryKey: [QueryKeys.users.GET_USER],
    queryFn: () => UsersService.usersControllerGetProfile(),
    enabled: true,
  });

  const { refetch: getAletaAuth } = useQuery({
    queryKey: ["ALETA_AUTH"],
    queryFn: () => UsersService.paymentGatewayControllerGetProfile(),
    enabled: false,
  });

  useEffect(() => {
    (async () => await setOnlineUser(onlineData))();
  }, [setOnlineUser, onlineData]);

  useEffect(() => {
    getUser();
  }, [getUser]);

  return {
    onlineUser,
    getUser,
    getAletaAuth,
  };
};

export default useOnlineUser;
