import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { Context } from 'gatsby-core-theme/src/context/MainProvider';
import { translate } from 'gatsby-core-theme/src/helpers/getters';
import keygen from 'gatsby-core-theme/src/helpers/keygen';
import styles from './site-contacts.module.scss';

const SiteContacts = ({ page, showHeading = false, heading = null }) => {
  const { translations } = useContext(Context) || {};
  const { siteSchema } = page || {};
  const email = siteSchema?.email || null;

  if (!email) return;

  return (
    <div className={styles.siteContactsContainer}>
      {showHeading && (
        <h2>
          {heading || translate(translations, 'reach_us', 'Reach out to one of our departments')}
        </h2>
      )}
      <div className={styles.siteContacts}>
        <ul className={styles?.generalContacts || ''}>
          {email?.map((item) => (
            <li key={keygen()}>
              <p>{item.contact_type}</p>
              <a href={`mailto:${item.email}`}>{item.email}</a>
            </li>
          ))}
        </ul>
      </div>
    </div>
  );
};

export default SiteContacts;

SiteContacts.propTypes = {
  page: PropTypes.shape({
    siteSchema: PropTypes.shape({}),
  }).isRequired,
  showHeading: PropTypes.bool,
  heading: PropTypes.string,
};
