/* eslint-disable react/no-unused-prop-types */
/* eslint-disable import/no-unresolved */
/* eslint-disable import/extensions */
/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import PropTypes from 'prop-types';

import loadable from '@loadable/component';
import styles from './header.module.scss';
import Breadcrumbs from '~atoms/breadcrumbs';
import { isSearchPath } from '~helpers/isSearchPath';

/* eslint-disable camelcase, react/no-danger */

// eslint-disable-next-line react/prop-types
function Header({ section, content = null, serverData }) {
  const headerContent = () => {
    let ModuleComponent = null;
    switch (section?.page?.template) {
      case 'event': {
        ModuleComponent = loadable(() =>
          import('../../../../components/molecules/header/variants/event')
        );
        return <ModuleComponent page={section.page} serverData={serverData} />;
      }
      case 'tournament': {
        ModuleComponent = loadable(() =>
          import('../../../../components/molecules/header/variants/tournament')
        );
        return <ModuleComponent page={section.page} />;
      }
      case 'sportsbook':
      case 'operator_review': {
        ModuleComponent = loadable(() =>
          import('../../../../components/molecules/header/variants/operator')
        );
        return <ModuleComponent {...section.page} />;
      }
      case 'contact_us': {
        ModuleComponent = loadable(() =>
          import('../../../../components/molecules/header/variants/contact-us/index')
        );
        return <ModuleComponent page={section.page} />;
      }
      default:
        ModuleComponent = loadable(() =>
          import('../../../../components/molecules/header/variants/default')
        );
        return <ModuleComponent page={section.page} />;
    }
  };

  const customStyles = section.page.path === '/' ? 'homepage' : section.page.path.replace('/', '-');
  const isSearch = isSearchPath(section.allMarkets, section.page.path);

  const isContactUs = section?.page?.template === 'contact_us';

  return (
    !isSearch && (
      <>
        <header
          className={`${styles.header} ${styles[section.page.template]} ${
            styles[customStyles] ? styles[customStyles] || '' : ''
          }
        `}
        >
          {section.page.path && !isContactUs && (
            <div className={styles.breadcrumbContainer}>
              <Breadcrumbs page={section.page} />
            </div>
          )}
          <div
            className={`${styles.headerContent} ${styles[section.page.template]} ${
              !content && styles.default
            }`}
          >
            {headerContent()}
          </div>
        </header>
      </>
    )
  );
}

Header.propTypes = {
  content: PropTypes.element,
  section: PropTypes.shape({
    page: PropTypes.shape({
      template: PropTypes.string,
      path: PropTypes.string,
      updated_at: PropTypes.string,
      created_at: PropTypes.string,
      reading_time: PropTypes.string,
      reviewer: PropTypes.string,
      reviewer_id: PropTypes.number,
      author: PropTypes.shape({
        name: PropTypes.string,
        profile_page_path: PropTypes.string,
      }),
      authors: PropTypes.shape([PropTypes.shape({})]),
      sections: PropTypes.shape({
        header: PropTypes.shape({
          extra_fields: PropTypes.shape({
            header_background_image: PropTypes.oneOfType([
              PropTypes.string,
              PropTypes.shape({
                value: PropTypes.string,
                type: PropTypes.string,
              }),
            ]),
            header_text: PropTypes.oneOfType([
              PropTypes.string,
              PropTypes.shape({
                value: PropTypes.string,
                type: PropTypes.string,
              }),
            ]),
          }),
        }),
      }),
      title: PropTypes.string,
    }),
    allMarkets: PropTypes.shape({}),
  }).isRequired,
  backgroundImage: PropTypes.bool,
};

export default Header;
