import { Button, ButtonProps } from 'antd';
import { usePermission } from '@/hooks/usePermission';

interface Props extends ButtonProps {
  menuCode: string;
  permCode: 'READ' | 'CREATE' | 'UPDATE' | 'DELETE' | 'APPROVE' | 'EXPORT' | 'EXECUTE';
}

/** 권한 없으면 렌더링 자체를 안 함 */
export default function PermissionButton({ menuCode, permCode, children, ...rest }: Props) {
  const perms = usePermission(menuCode);
  const allowed =
    (permCode === 'READ' && perms.canRead) ||
    (permCode === 'CREATE' && perms.canCreate) ||
    (permCode === 'UPDATE' && perms.canUpdate) ||
    (permCode === 'DELETE' && perms.canDelete) ||
    (permCode === 'APPROVE' && perms.canApprove) ||
    (permCode === 'EXPORT' && perms.canExport) ||
    (permCode === 'EXECUTE' && perms.canExecute);
  if (!allowed) return null;
  return <Button {...rest}>{children}</Button>;
}
