import { useMatch } from 'react-router-dom';

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

export const useArticleEditMode = (article?: Article | null) => {
  const isEditRoot = useMatch('/library/:rootId/edit');
  const isEditChild = useMatch('/library/:rootId/child/:childId/edit');
  const isEdit = Boolean(isEditRoot || isEditChild);

  if (!article) {
    return { isEdit, showEdit: false, isUnauthorizedEdit: false };
  }

  return {
    isEdit,
    showEdit: isEdit && !!article.isEditor,
    isUnauthorizedEdit: isEdit && !article.isEditor,
  };
};
