/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import PropTypes from 'prop-types';
import loadable from '@loadable/component';
import { imagePrettyUrl, getAltText } from 'gatsby-core-theme/src/helpers/getters';
import LazyImage from 'gatsby-core-theme/src/hooks/lazy-image';
import Rating from '../../../../../atoms/ratings';
import OperatorBanner from '../../../../../atoms/header-operator-bannner';
import styles from './template-three.module.scss';
import PrettyLink from '~atoms/pretty-link';

const TemplateThree = ({
  relation,
  type,
  name,
  image,
  ribbons,
  numOfStars,
  pageTemplate,
  icon,
  hasVariableComponent = false,
}) => {
  const ribbon = ribbons?.length > 0 ? ribbons[0] : null;

  const VariableComponent = hasVariableComponent ? loadable(() => import('../variables')) : null;

  const logo = () => (
    <PrettyLink
      operator={relation}
      pageTemplate={pageTemplate}
      module="header_operator"
      tracker="main"
      clickedElement="logo"
      className="header-gtm logo-cta "
    >
      <LazyImage
        loading="eager"
        src={imagePrettyUrl(image?.filename, 179, 179)}
        width={179}
        height={179}
        alt={getAltText(image, relation?.name)}
      />
    </PrettyLink>
  );

  return (
    <div
      className={`${styles.container} ${hasVariableComponent ? styles.hasVariableComponent : ''}`}
    >
      {logo()}
      <div className={styles.name}>
        <h1>{name}</h1>
        <LazyImage
          src={icon || '../../../../../../images/verify.svg'}
          loading="eager"
          alt="Verified"
          width={24}
          height={24}
        />
      </div>
      {ribbon && <p className={styles.ribbon}>{ribbon}</p>}
      <div className={styles.operatorBanner}>
        <OperatorBanner operator={relation} template={pageTemplate} />
      </div>
      <div className={styles.ratings}>
        <Rating item={relation} type={type} numOfStars={numOfStars} />
      </div>

      {VariableComponent && (
        <VariableComponent
          relation={relation}
          pageTemplate={pageTemplate}
          className={styles.variable}
        />
      )}
    </div>
  );
};

TemplateThree.propTypes = {
  relation: PropTypes.shape({
    type: PropTypes.string,
    template: PropTypes.string,
    name: PropTypes.string,
  }),
  name: PropTypes.string,
  type: PropTypes.string,
  image: PropTypes.shape({
    filename: PropTypes.string,
  }),
  ribbons: PropTypes.PropTypes.arrayOf(PropTypes.shape({})),
  pageTemplate: PropTypes.string,
  hasVariableComponent: PropTypes.bool,
  numOfStars: PropTypes.number,
  icon: PropTypes.node,
};

export default TemplateThree;
