import React from 'react'
import cx from 'classnames'
import { compose, merge, nthChar, prop } from 'ramda'

const firstLetter = nthChar(0)
const firstPropLetter = compose(firstLetter, prop)

const acronym = (item) => {
  if (item.firstName && item.lastName) {
    return firstPropLetter('firstName', item) + firstPropLetter('lastName', item)
  } if (item.firstName) {
    return firstPropLetter('firstName', item)
  } if (item.name) {
    return firstPropLetter('name', item)
  }
  return firstPropLetter('email', item)
}

const MTAcronym = (props) => {
  const {
    item,
    round,
    height,
    baseStyles,
    className,
    ...otherProps
  } = props

  const text = item.email || item.name ? acronym(item) : '?'

  const fontSize = text.length > 1 ? Math.round(height * 0.35) : Math.round((height * 2) / 3)

  const styles = merge(baseStyles, {
    fontSize: `${fontSize}px`,
  })

  return (
    <div
      { ...otherProps }
      className={ cx('mt-image', round && 'mt-image--round', className) }
      style={ styles }
    >
      <span className="mt-image__image">
        <span className="mt-image__letter">{ text }</span>
      </span>
    </div>
  )
}

export default MTAcronym
