/* eslint-disable import/no-extraneous-dependencies */
import React, { lazy, Suspense } from 'react';
import PropTypes from 'prop-types';
import { imagePrettyUrl } from '~helpers/getters';
import { getAltText } from '~helpers/image';
import LazyImage from '~hooks/lazy-image';
import Rating from '~atoms/ratings';
import OperatorBanner from '~atoms/header-operator-bannner';
import Ribbons from '~atoms/ribbons';
import VariableComponent from '../variables';
import { TrackingKeys } from '~constants/tracking-api'
import styles from './template-three.module.scss';
import PrettyLink from '~atoms/pretty-link';

const VerifyIcon = lazy(() => import('~images/icons/verify'));

const TemplateThree = ({
  relation,
  type,
  name,
  image,
  ribbons,
  numOfStars,
  pageTemplate,
  icon = <Suspense fallback={<></>}><VerifyIcon /></Suspense>,
  hasVariableComponent = false,
  width = 179,
  height = 179,
  moduleName = TrackingKeys?.HEADEROPERATOR
}) => {
  const ribbon = ribbons?.length > 0 ? ribbons[0] : null;

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

  return (
    <div
      className={`${styles.container} ${hasVariableComponent ? styles.hasVariableComponent : ''}`}
    >
      {logo()}
      <div className={styles.name}>
        <h1>{name}</h1>
        {icon}
      </div>
      {ribbon && <Ribbons item={[ribbon]} customStyle={styles.ribbon} />}
      <div className={styles.operatorBanner}>
        <OperatorBanner operator={relation} template={pageTemplate} moduleName={moduleName} clickedElement="header_operator_banner" />
      </div>
      <div className={styles.ratings}>
        <Rating item={relation} type={type} numOfStars={numOfStars} />
      </div>

      {hasVariableComponent && (
        <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.string),
  pageTemplate: PropTypes.string,
  hasVariableComponent: PropTypes.bool,
  numOfStars: PropTypes.number,
  icon: PropTypes.element,
  width: PropTypes.number,
  height: PropTypes.number,
  moduleName: PropTypes.string,
};

export default TemplateThree;
