import { ArticleMeta } from '@ts/types/articles';
import { settings } from '@utils/config';
import { getFormattedDate } from '@utils/helpers/format';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useIntl } from 'react-intl';
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 intl = useIntl();
  const router = useRouter();
  const locale = router.locale ? router.locale : settings.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 = () => {
    return intl.formatMessage(
      {
        defaultMessage:
          '{commentCount, plural, =0 {No comments} one {# comment} other {# comments}}',
        description: 'PostMeta: comment count value',
      },
      { commentCount }
    );
  };
  const getReadingTime = () => {
    if (!readingTime) return;
    if (readingTime < 0)
      return intl.formatMessage({
        defaultMessage: 'less than 1 minute',
        description: 'PostMeta: Reading time value',
      });
    return intl.formatMessage(
      {
        defaultMessage:
          '{readingTime, plural, =0 {# minutes} one {# minute} other {# minutes}}',
        description: 'PostMeta: reading time value',
      },
      { readingTime }
    );
  };
  const getDates = () => {
    if (!dates) return <>>;
    const publicationDate = getFormattedDate(dates.publication, locale);
    const updateDate = getFormattedDate(dates.update, locale);
    return (
      <>
        
          
            {intl.formatMessage({
              defaultMessage: 'Published on:',
              description: 'PostMeta: publication date label',
            })}
          
          
            
          
        
        {publicationDate !== updateDate && (
          
            
              {intl.formatMessage({
                defaultMessage: 'Updated on:',
                description: 'PostMeta: update date label',
              })}
            
            
              
            
          
        )}
      >
    );
  };
  const wrapperClass = styles[`wrapper--${mode}`];
  return (
    
      {author && (
        
          
- 
            {intl.formatMessage({
              defaultMessage: 'Written by:',
              description: 'Article meta',
            })}
          
- {author.name})}
      {getDates()}
      {readingTime !== undefined && wordsCount !== undefined && (
          
- 
            {intl.formatMessage({
              defaultMessage: 'Reading time:',
              description: 'Article meta',
            })}
          
- 
            {getReadingTime()}
          )}
      {results && (
          
- 
            {intl.formatMessage({
              defaultMessage: 'Total:',
              description: 'Article meta',
            })}
          
- 
            {intl.formatMessage(
              {
                defaultMessage:
                  '{results, plural, =0 {No articles} one {# article} other {# articles}}',
                description: 'PostMeta: total found articles',
              },
              { results }
            )}
          )}
      {!isThematic() && thematics && thematics.length > 0 && (
          
- 
            {intl.formatMessage(
              {
                defaultMessage:
                  '{thematicsCount, plural, =0 {Thematics:} one {Thematic:} other {Thematics:}}',
                description: 'PostMeta: thematics list label',
              },
              { thematicsCount: thematics.length }
            )}
          {getThematics()}
        
      )}
      {isThematic() && topics && topics.length > 0 && (
          
- 
            {intl.formatMessage(
              {
                defaultMessage:
                  '{topicsCount, plural, =0 {Topics:} one {Topic:} other {Topics:}}',
                description: 'PostMeta: topics list label',
              },
              { topicsCount: topics.length }
            )}
          {getTopics()}
        
      )}
      {website && (
          - 
            {intl.formatMessage({
              defaultMessage: 'Website:',
              description: 'PostMeta: website label',
            })}
          
- 
            {website}
          
 
      )}
      {commentCount !== undefined && (
        
          - 
            {intl.formatMessage({
              defaultMessage: 'Comments:',
              description: 'PostMeta: comment count label',
            })}
          
- 
            {isArticle() ? (
              {getCommentsCount()}
            ) : (
              getCommentsCount()
            )}
          
 
      )}
    
);
};
export default PostMeta;