import React from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl } from 'gatsby-core-theme/src/helpers/getters';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import Link from 'gatsby-core-theme/src/hooks/link';
import useTranslate from '~hooks/useTranslate/useTranslate';
import VerifiedAuthor from '../../../../images/icons/verified-author';
import styles from './template-three.module.scss';

const AuthorTemplateThree = ({
  authorImage,
  authorImageHeight = 48,
  authorImageWidth = 48,
  authorName,
  authorPath,
  authorTitle,
  customStyles,
  isReviewer = false,
  prefix,
  showLinks = true,
  showPrefix = false,
  showTitle = true,
  showVerification = true,
  type,
  verify = <VerifiedAuthor />,
  imageLoading = 'eager',
}) => {
  const getPrefixBasedOnPage = type?.includes('review')
    ? useTranslate('reviewed_by', 'Review by')
    : useTranslate('written_by', 'Written by');

  const getAuthorTitle = <span className={styles.authorTitle}>{authorTitle}</span>;

  return (
    <>
      {showPrefix && (
        <span className={styles.prefix || ''}>
          {isReviewer
            ? useTranslate('fact_checked_by', 'Fact checked by')
            : useTranslate(prefix, prefix) || getPrefixBasedOnPage}
        </span>
      )}
      <div className={`${styles.authorContainer || ''} ${customStyles || ''}`}>
        {authorImage && (
          <LazyImage
            alt={authorName}
            loading={imageLoading}
            width={authorImageWidth}
            height={authorImageHeight}
            src={imagePrettyUrl(authorImage, authorImageWidth, authorImageHeight)}
          />
        )}
        <div className={styles.authorDataContainer || ''}>
          {showLinks ? (
            <Link to={authorPath} title={authorName} className={styles.authorName}>
              {authorName}
            </Link>
          ) : (
            <p>{authorName}</p>
          )}

          {showTitle && !isReviewer && getAuthorTitle}
          {isReviewer &&
            (showTitle ? (
              getAuthorTitle
            ) : (
              // eslint-disable-next-line jsx-a11y/label-has-associated-control
              <label className={styles.reviewerTitle}>
                {showVerification && verify}
                {useTranslate('fact_checker', 'Fact-checker')}
              </label>
            ))}
        </div>
      </div>
    </>
  );
};

AuthorTemplateThree.propTypes = {
  authorImage: PropTypes.string,
  authorImageHeight: PropTypes.number,
  authorImageWidth: PropTypes.number,
  authorName: PropTypes.string,
  authorPath: PropTypes.string,
  authorTitle: PropTypes.string,
  customStyles: PropTypes.string,
  prefix: PropTypes.string,
  isReviewer: PropTypes.bool,
  showLinks: PropTypes.bool,
  showPrefix: PropTypes.bool,
  showTitle: PropTypes.bool,
  showVerification: PropTypes.bool,
  type: PropTypes.string,
  verify: PropTypes.element,
  imageLoading: PropTypes.string,
};

export default AuthorTemplateThree;
