import type { ArticleStore } from "../domain/articleStore";
import { getArticleUseCase } from "../useCases/getArticleUseCase";
import { useCallback, useEffect } from "react";

function useArticleViewModel(store: ArticleStore) {
  const _getArticle = useCallback(
    (id: string) => getArticleUseCase(store.getArticle, store.setArticle, id),
    [store.getArticle, store.setArticle]
  );

  useEffect(() => {
    if (store.article != undefined) {
      _getArticle(store.article.id);
    }
  }, [store.article?.id]);

  return {
    article: store.article,
    shouldShowLoading: typeof store.article === "undefined" || store.isLoading,
    hasError: store.hasError,
  };
}

export { useArticleViewModel };
