/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import loadable from '@loadable/component';
import { getExtraField, copyrightText } from 'gatsby-core-theme/src/helpers/getters';
import { generatePlaceholderString } from 'gatsby-core-theme/src/helpers/generators.mjs';

export const footerContent = (props) => {
  let ModuleComponent = null;

  switch (props.footerTemplate) {
    default:
      ModuleComponent = loadable(() =>
        import('../../../../components/molecules/footer/template-one')
      );
      return <ModuleComponent {...props} />;
  }
};

const Footer = (props) => {
  const helpText = `
  <p>
  Gambling aware notice
  </p>`;
  const helpline = () => {
    if (getExtraField(props.section?.extra_fields, 'helpline')) {
      return generatePlaceholderString(getExtraField(props.section?.extra_fields, 'helpline'));
    }
    return null;
  };

  const customCopyrightText = () => {
    if (getExtraField(props.section?.extra_fields, 'copyright_text')) {
      return generatePlaceholderString(
        getExtraField(props.section?.extra_fields, 'copyright_text')
      );
    }

    return copyrightText();
  };

  const footerLogos = getExtraField(props.section?.extra_fields, 'logos');

  const getFooterLogos = () => {
    let refactoredFooterLogos = { children: [] };

    if (footerLogos === null) {
      refactoredFooterLogos = {
        children: [...props.page?.responsibleGaming],
      };
    } else {
      refactoredFooterLogos = {
        ...footerLogos,
        children: [...props.page?.responsibleGaming, ...footerLogos.children],
      };
    }

    return refactoredFooterLogos;
  };
  props = {
    ...props,
    showLinks: true,
    copyrightDisclaimer: getExtraField(props.section?.extra_fields, 'copyright_disclaimer'),
    footerLogos: props.page?.responsibleGaming?.length ? getFooterLogos() : footerLogos,
    hasDisclaimer: true,
    showCopyright: true,
    footerMenu: getExtraField(props.section?.extra_fields, 'links'),
    asSeenOn: getExtraField(props.section?.extra_fields, 'as_seen_on'),
    gamblingText: getExtraField(props.section?.extra_fields, 'gambling_text'),
    socialIcons: getExtraField(props.section?.extra_fields, 'social_icons'),
    logo: '../../../../../images/logo.svg',
    dmcaImage: '../../../../../images/dmca.png',
    dmcaWidth: 100,
    dmcaHeight: 30,
    securityIcon: '../../../../../images/security.svg',
    helpText,
    phoneNum: helpline(),
    customCopyrightText: customCopyrightText(),
  };

  return footerContent(props);
};

export default Footer;
