import React from 'react'
import PropTypes from 'prop-types'

function MTLoader (props) {
  const {
    size,
    color,
    className,
    ...otherProps
  } = props
  const loaderStyle = {
    position: 'relative',
    width: `${size * 10}px`,
    height: `${size * 10}px`,
    display: 'inline-block',
  }
  return (
    <div { ...otherProps } className={ `mt-loader ${className}` } style={ loaderStyle }>
      <div className="double-bounce1" style={ { backgroundColor: color } } />
      <div className="double-bounce2" style={ { backgroundColor: color } } />
    </div>
  )
}

MTLoader.defaultProps = {
  size: 1.4,
  color: '#000',
}

MTLoader.propTypes = {
  size: PropTypes.number,
  color: (props, propName, componentName) => {
    const isCorrectColorFormat = /^#+([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/
    if (!isCorrectColorFormat.test(props[propName])) {
      return new Error(`Invalid prop ${propName} supplied to ${componentName}. Must be on format "#111111" or "#111".`)
    }

    return null
  },
}

export default MTLoader
