import React, { useMemo } from 'react'
import withDefaults from '../utils/with-defaults'
import useTheme from '../use-theme'
import { NormalTypes } from '../utils/prop-types'
import { HuiThemes } from '../themes'

interface Props {
  type?: NormalTypes
}

const defaultProps = {
  type: 'default' as NormalTypes
}

type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type DotProps = Props & typeof defaultProps & NativeAttrs

const getColor = (type: NormalTypes, theme: HuiThemes): string => {
  const colors: { [key in NormalTypes]?: string } = {
    default: theme.palette.accents_2,
    success: theme.palette.success,
    warning: theme.palette.warning,
    error: theme.palette.error
  }
  return colors[type] || (colors.default as string)
}

const DotIcon: React.FC<React.PropsWithChildren<DotProps>> = ({ type }) => {
  const theme = useTheme()
  const color = useMemo(() => getColor(type, theme), [type, theme])
  return (
    <span className="icon">
      <style jsx>{`
        .icon {
          width: 0.625rem;
          height: 0.625rem;
          border-radius: 50%;
          background-color: ${color};
          user-select: none;
        }
      `}</style>
    </span>
  )
}

const MemoDotIcon = React.memo(DotIcon)

export default withDefaults(MemoDotIcon, defaultProps)
