import { isNil, mapIndexed, type } from 'ramda'
import React from 'react'
import cx from 'classnames'
import MTTypography from './mt-typography'
import MTIcon from './mt-icon'
import MTCircledNumber from './mt-circle-number'

function MTStepper ({
  steps = [],
  selectedIndex = 0,
  onClick,
  className,
  showCompleted = false,
  ...otherProps
}) {
  return (
    <div { ...otherProps } className={ cx('wrapper', className) }>
      { mapIndexed((item, idx) => (
        <div
          className={ cx('stepper', `${selectedIndex === idx ? 'active' : ''}`) }
          key={ `${item}-${idx}` }
        >
          <div className="stepper__item" onClick={ () => onClick(idx) }>
            { isNil(item.icon) && (
              <MTCircledNumber
                className={ `${selectedIndex === idx ? 'active' : ''}` }
                number={ idx + 1 }
              />
            ) }
            { !isNil(item.icon) && type(item.icon) === 'String'
              ? (
                <MTIcon
                  className="icon-custom"
                  align={ false }
                  type={ `${item.icon}` }
                />
              )
              : item.icon
            }
            <MTTypography
              className="stepper__item--title"
              variant="sectionTitle"
              margin={ false }
              padding={ false }
            >
              { item.label }
            </MTTypography>
            { showCompleted && (
              <MTIcon
                className={ `stepper__icon ${selectedIndex > idx ? 'complete' : 'incomplete'}` }
                type={ `${selectedIndex > idx ? 'status-approved' : 'checked'}` }
              />
            ) }
            { idx + 1 !== steps.length && <div className="stepper__item--line" /> }
          </div>
        </div>
      ), steps) }
    </div>
  )
}

export default MTStepper
