"use client";

import type { ReactNode } from "react";

import { hasAllPermissions, hasAnyPermission, hasPermission } from "@/lib/permissions";
import type { CurrentUser, PermissionName } from "@/types/rbac";

type Props = {
  user: CurrentUser | null;
  permission?: PermissionName;
  any?: PermissionName[];
  all?: PermissionName[];
  children: ReactNode;
};

export default function Can({ user, permission, any, all, children }: Props) {
  if (permission && !hasPermission(user, permission)) {
    return null;
  }

  if (any && !hasAnyPermission(user, any)) {
    return null;
  }

  if (all && !hasAllPermissions(user, all)) {
    return null;
  }

  return <>{children}</>;
}
