import { config } from '@config/website';
import { plural, t } from '@lingui/macro';
import { ArticleMeta } from '@ts/types/articles';
import { getFormattedDate } from '@utils/helpers/format';
import Link from 'next/link';
import { useRouter } from 'next/router';
import styles from './PostMeta.module.scss';
type PostMetaMode = 'list' | 'single';
const PostMeta = ({
  meta,
  mode = 'list',
}: {
  meta: ArticleMeta;
  mode?: PostMetaMode;
}) => {
  const {
    author,
    commentCount,
    dates,
    readingTime,
    results,
    thematics,
    topics,
    website,
    wordsCount,
  } = meta;
  const router = useRouter();
  const locale = router.locale ? router.locale : config.locales.defaultLocale;
  const isThematic = () => router.asPath.includes('/thematique/');
  const isArticle = () => router.asPath.includes('/article/');
  const getTopics = () => {
    return (
      topics &&
      topics.map((topic) => {
        return (
          
            
              {topic.title}
            
          
        );
      })
    );
  };
  const getThematics = () => {
    return (
      thematics &&
      thematics.map((thematic) => {
        return (
          
            
              {thematic.title}
            
          
        );
      })
    );
  };
  const getCommentsCount = () => {
    switch (commentCount) {
      case 0:
        return t`No comments`;
      case 1:
        return t`1 comment`;
      default:
        return t`${commentCount} comments`;
    }
  };
  const getReadingTime = () => {
    if (!readingTime) return;
    if (readingTime < 0) return t`less than 1 minute`;
    return plural(readingTime, {
      zero: '# minutes',
      one: '# minute',
      other: '# minutes',
    });
  };
  const getDates = () => {
    if (!dates) return <>>;
    const publicationDate = getFormattedDate(dates.publication, locale);
    const updateDate = getFormattedDate(dates.update, locale);
    return (
      <>
        
          
{t`Published on:`}
          
            
          
        
        {publicationDate !== updateDate && (
          
            
{t`Updated on:`}
            
              
            
          
        )}
      >
    );
  };
  const wrapperClass = styles[`wrapper--${mode}`];
  return (
    
      {author && (
        
          
- {t`Written by:`}
- {author.name})}
      {getDates()}
      {readingTime !== undefined && wordsCount !== undefined && (
          
- {t`Reading time:`}
- 
            {getReadingTime()}
          )}
      {results && (
          
- {t`Total: `}
- 
            {plural(results, {
              zero: '# articles',
              one: '# article',
              other: '# articles',
            })}
          )}
      {!isThematic() && thematics && thematics.length > 0 && (
          
- 
            {thematics.length > 1 ? t`Thematics:` : t`Thematic:`}
          {getThematics()}
        
      )}
      {isThematic() && topics && topics.length > 0 && (
          
- 
            {topics.length > 1 ? t`Topics:` : t`Topic:`}
          {getTopics()}
        
      )}
      {website && (
        
      )}
      {commentCount !== undefined && (
        
      )}
);
};
export default PostMeta;