import { usePermissions } from "@/shared/context/PermissionContext";
import { Permission } from "@/permissions";
import { ReactNode } from "react";

interface WithPermissionProps {
  perms: Permission[];
  fallback?: ReactNode;
  children: ReactNode;
}

export const useHas = (reqPerms: Permission[]) => {
  const { permissions } = usePermissions();
  return reqPerms.some((p) => permissions.includes(p));
};

export const WithPermission = ({
  perms,
  fallback = null,
  children,
}: WithPermissionProps) => {
  return useHas(perms) ? children : fallback;
};
