import PropTypes from '../../_util/vue-types'
import BaseMixin from '../../_util/BaseMixin'
import Tooltip from '../../vc-tooltip'
import Handle from './Handle'

export default function createSliderWithTooltip (Component) {
  return {
    mixins: [BaseMixin],
    props: {
      tipFormatter: PropTypes.func.def((value) => { return value }),
      handleStyle: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]).def([{}]),
      tipProps: PropTypes.object.def({}),
    },
    data () {
      return {
        visibles: {},
      }
    },
    methods: {
      handleTooltipVisibleChange (index, visible) {
        this.setState((prevState) => {
          return {
            visibles: {
              ...prevState.visibles,
              [index]: visible,
            },
          }
        })
      },
      handleWithTooltip ({ value, dragging, index, disabled, ...restProps }) {
        const {
          tipFormatter,
          tipProps,
          handleStyle,
        } = this.$props

        const {
          prefixCls = 'rc-slider-tooltip',
          overlay = tipFormatter(value),
          placement = 'top',
          visible = visible || false,
          ...restTooltipProps } = tipProps

        let handleStyleWithIndex
        if (Array.isArray(handleStyle)) {
          handleStyleWithIndex = handleStyle[index] || handleStyle[0]
        } else {
          handleStyleWithIndex = handleStyle
        }

        const tooltipProps = {
          props: {
            prefixCls,
            overlay,
            placement,
            visible: (!disabled && (this.visibles[index] || dragging)) || visible,
            ...restTooltipProps,
          },
          key: index,
        }
        const handleProps = {
          props: {
            value,
            ...restProps,
          },
          on: {
            mouseenter: () => this.handleTooltipVisibleChange(index, true),
            mouseleave: () => this.handleTooltipVisibleChange(index, false),
          },
          style: {
            ...handleStyleWithIndex,
          },
        }

        return (
          <Tooltip
            {...tooltipProps}
          >

            <Handle
              {...handleProps}
            />
          </Tooltip>
        )
      },
    },
    render () {
      return <Component {...this.$props} handle={this.handleWithTooltip} />
    },
  }
}
