import { useAuth } from 'src/contexts/JWTContext';
import useCommunityFeature from 'src/hooks/useCommunityFeature';
import { CommunityFeature } from 'src/types/communityFeatures';
import { hasAnyPermissions, UserPermissions } from 'src/utils/permissions';

const useCanViewPostInsights = (postUserId?: number) => {
  const { user, permissions } = useAuth();
  const canViewStats = useCommunityFeature(CommunityFeature.VIEW_POST_STATS);

  return (
    canViewStats &&
    (hasAnyPermissions(permissions, [UserPermissions.MANAGE_POSTS]) ||
      user?.id === postUserId)
  );
};

export default useCanViewPostInsights;
