import React from 'react'
import { test } from 'ramda'
import MTButton from './mt-button'

const newNetworkMessage = 'Network error, connection has been lost. Please check your network connectivity and refresh the page.'
const isNetworkError = test(/^Request has been terminated/)

function translateErrorMessage (originalMessage) {
  return isNetworkError(originalMessage) ? newNetworkMessage : originalMessage
}

function translateError (newError) {
  return isNetworkError(newError.message)
    ? new Error(translateErrorMessage(newError.message))
    : newError
}

function MTError (props) {
  const { error, className, ...otherProps } = props
  const translatedError = translateError(error)
  return (
    <div
      { ...otherProps }
      className={ `mt-error ${className}` }
    >
      <div className="mt-error__inner">
        <h1 className="h1 m-b">Sorry, something went wrong.</h1>
        <div className="mt-error__end-info">
          Please contact support if this error keeps occurring.
        </div>
        <div className="mt-error__message">
          <div className="mt-error__message-inner">
            { translatedError.message }
          </div>
        </div>
        <a href="/" className="block m-t">
          <MTButton
            color="blue"
            label="Back to Camphouse"
          />
        </a>

      </div>
    </div>
  )
}

export default MTError
