import React from 'react'
import cx from 'classnames'
import { addIndex, isNil, map } from 'ramda'
import MTIcon from './mt-icon'
import MTLoader from './mt-loader'

const mapIndexed = addIndex(map)

function MTStepLoader (props) {
  const {
    steps,
    errorIndex,
    currentStepIndex,
    errorMessage,
    title,
    className,
    ...otherProps
  } = props

  const stepItems = mapIndexed((item, idx) => {
    const icon = idx === errorIndex
      ? <MTIcon type="status-rejected" className="red-text" />
      : idx === currentStepIndex
        ? <MTLoader />
        : idx < currentStepIndex
          ? <MTIcon type="success" className="green-text" />
          : <MTIcon type="time" className="light-text" />
    return (
      <div className="mt-step-loader__step" key={ idx }>
        <div className="mt-step-loader__icon">{ icon }</div>
        <div className="mt-step-loader__text">{ item }</div>
        { errorIndex === idx && <div className="mt-step-loader__error">{ errorMessage }</div> }
      </div>
    )
  }, steps)

  return (
    <div { ...otherProps } className={ cx('mt-step-loader', className) }>
      { isNil(title) || <h4 className="h4 mt-step-loader__title">{ title }</h4> }
      { stepItems }
    </div>
  )
}

export default MTStepLoader
