import { ROLE } from "../../redux/User/types";
import { userHasRole } from "../../utils/user";
import { useTypedSelector } from "../../redux/rootReducer";
import { selectCollaborator } from "../../redux/User/selector";

import { useLoggedInUser } from "./useLoggedInUser";

export function useHasRole(role: ROLE) {
  const hasRoleHasUser = useLoggedInUserHasRole(role);
  const hasRoleAsCollaborator = useCollaboratorHasRole(role);
  return hasRoleHasUser || hasRoleAsCollaborator;
}

function useLoggedInUserHasRole(role: ROLE) {
  const user = useLoggedInUser();

  return userHasRole(user, role);
}

function useCollaborator() {
  return useTypedSelector(selectCollaborator);
}

function useCollaboratorHasRole(role: ROLE) {
  const collaborator = useCollaborator();

  return userHasRole(collaborator, role);
}
