import { useState } from "react";
import { useAuth } from "../../core/context/auth_context";
import { Convert, UserEntity } from "../../data/datasource/api/entities/user";

/**
 * Custom hook to manage user data and authentication state.
 * @returns {object} - An object containing the user data, logout handler, and toggle state.
 */
export const useUserData = () => {
  const { user, logout } = useAuth();
  
  // State to manage a toggle functionality
  const [toggle, setToggle] = useState(false);

  // Retrieve user data from local storage
  const userDataJson: string | null = localStorage.getItem("user");
  let userData: UserEntity | undefined;

  // Convert the JSON string to a UserEntity object if it exists
  if (userDataJson) {
    userData = Convert.toUser(userDataJson);
  }

  /**
   * Function to handle user logout.
   */
  const handleLogout = () => {
    logout();
  };

  return { user, handleLogout, setToggle, toggle, userData };
};
