import { useEffect } from 'react';

import { type Article } from '../types';

import { useArticleEditMode } from './useArticleEditMode';
import { useNavigateToCurrentArticle } from './useNavigateToCurrentArticle';

export const useEditPermissionGuard = (article?: Article | null) => {
  const navigateToCurrentArticle = useNavigateToCurrentArticle();
  const { isUnauthorizedEdit } = useArticleEditMode(article);

  useEffect(() => {
    if (!isUnauthorizedEdit || !article) return;
    navigateToCurrentArticle({ replace: true });
  }, [isUnauthorizedEdit, article, navigateToCurrentArticle]);
};
