import React from 'react';
import { hasPermission, hasAnyPermission, hasAllPermissions, hasRole } from '../utils/permission';

interface PermissionWrapperProps {
  children: React.ReactNode;
  permission?: string;
  permissions?: string[];
  requireAll?: boolean;
  role?: string;
  fallback?: React.ReactNode;
}

/**
 * 权限包装组件
 * 根据权限控制子组件的显示
 */
export default function PermissionWrapper({
  children,
  permission,
  permissions,
  requireAll = false,
  role,
  fallback = null,
}: PermissionWrapperProps) {
  // 检查角色权限
  if (role && !hasRole(role)) {
    return <>{fallback}</>;
  }

  // 检查单个权限
  if (permission && !hasPermission(permission)) {
    return <>{fallback}</>;
  }

  // 检查多个权限
  if (permissions && permissions.length > 0) {
    const hasAccess = requireAll
      ? hasAllPermissions(permissions)
      : hasAnyPermission(permissions);
    
    if (!hasAccess) {
      return <>{fallback}</>;
    }
  }

  // 如果没有指定权限检查，默认显示
  return <>{children}</>;
}

