'use client';

import { useAuthStore } from '@/lib/stores/auth-store';

interface PermissionGateProps {
  children: React.ReactNode;
  permission?: string;
  role?: string;
  fallback?: React.ReactNode;
}

/**
 * 权限门控组件
 * 根据权限码或角色控制组件显示
 */
export function PermissionGate({
  children,
  permission,
  role,
  fallback = null,
}: PermissionGateProps) {
  const { user } = useAuthStore();

  if (!user) {
    return <>{fallback}</>;
  }

  // 检查权限
  if (permission && !user.permissions?.includes(permission)) {
    return <>{fallback}</>;
  }

  // 检查角色
  if (role && user.role !== role) {
    return <>{fallback}</>;
  }

  return <>{children}</>;
}