"use client";
import { useMemo } from "react";
import { useTeamContext } from "../../contexts/TeamContext";
import { permissionRegistry } from "./init";
function usePermission(permission) {
  const { currentTeam, userTeams } = useTeamContext();
  return useMemo(() => {
    if (!currentTeam) return false;
    const membership = userTeams.find((t) => t.team.id === currentTeam.id);
    if (!(membership == null ? void 0 : membership.role)) return false;
    return permissionRegistry.hasPermission(membership.role, permission);
  }, [currentTeam, userTeams, permission]);
}
function usePermissions(permissions) {
  const { currentTeam, userTeams } = useTeamContext();
  return useMemo(() => {
    const result = {};
    const membership = currentTeam ? userTeams.find((t) => t.team.id === currentTeam.id) : null;
    for (const [key, permission] of Object.entries(permissions)) {
      if (!(membership == null ? void 0 : membership.role)) {
        result[key] = false;
      } else {
        result[key] = permissionRegistry.hasPermission(
          membership.role,
          permission
        );
      }
    }
    return result;
  }, [currentTeam, userTeams, permissions]);
}
function useAllPermissions() {
  const { currentTeam, userTeams } = useTeamContext();
  return useMemo(() => {
    if (!currentTeam) return [];
    const membership = userTeams.find((t) => t.team.id === currentTeam.id);
    if (!(membership == null ? void 0 : membership.role)) return [];
    return permissionRegistry.getRolePermissions(membership.role);
  }, [currentTeam, userTeams]);
}
function useTeamRole() {
  const { currentTeam, userTeams } = useTeamContext();
  return useMemo(() => {
    if (!currentTeam) return null;
    const membership = userTeams.find((t) => t.team.id === currentTeam.id);
    return (membership == null ? void 0 : membership.role) ?? null;
  }, [currentTeam, userTeams]);
}
export {
  useAllPermissions,
  usePermission,
  usePermissions,
  useTeamRole
};
