function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { round, get, isNil } from 'lodash';
import cx from 'classnames';
import { Slider, Handles, Tracks, Ticks } from 'react-compound-slider';
import Handle from './Handle';
import Track from './Track';
import Tick from './Tick';

class SliderInput extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "getSliderProps", () => {
      const {
        options,
        value
      } = this.props;
      const domain = this.isLikert() ? [0, options.length - 1] : [0, 1];
      const step = this.isLikert() ? 1 : 0.0005;
      const values = this.isLikert() ? [options.findIndex(option => option.value === value)] : [value];
      return {
        domain,
        step,
        values
      };
    });

    _defineProperty(this, "getTickCount", () => {
      const {
        type,
        options
      } = this.props;

      switch (type) {
        case 'LIKERT':
          return options.length - 1;

        case 'VAS':
          return 1;

        default:
          return null;
      }
    });

    _defineProperty(this, "getLabelForValue", value => {
      const {
        options,
        parameters
      } = this.props;

      if (this.isLikert()) {
        return get(options, [value, 'label']);
      }

      if (this.isVisualAnalogScale()) {
        const index = value === 0 ? 'minLabel' : 'maxLabel';
        return get(parameters, index);
      }

      return round(value * 100);
    });

    _defineProperty(this, "normalizeValue", value => {
      const {
        options
      } = this.props;

      if (this.isLikert()) {
        return options[value].value;
      }

      return round(value, 3);
    });

    _defineProperty(this, "handleSlideEnd", value => {
      const {
        onBlur
      } = this.props;
      const normalizedValue = this.normalizeValue(value); // Use input.onBlur rather than input.onChange so that we can set 'touched'

      onBlur(normalizedValue);
    });

    _defineProperty(this, "isLikert", () => {
      const {
        type
      } = this.props;
      return type === 'LIKERT';
    });

    _defineProperty(this, "isVisualAnalogScale", () => {
      const {
        type
      } = this.props;
      return type === 'VAS';
    });
  }

  render() {
    const {
      value
    } = this.props;
    const sliderProps = this.getSliderProps();
    const tickCount = this.getTickCount();
    const showTooltips = !this.isVisualAnalogScale();
    const isNotSet = isNil(value);
    const className = cx('form-field-slider__slider', {
      'form-field-slider__slider--likert': this.isLikert()
    }, {
      'form-field-slider__slider--vas': this.isVisualAnalogScale()
    }, {
      'form-field-slider__slider--not-set': isNotSet
    });
    return /*#__PURE__*/React.createElement("div", {
      className: "form-field"
    }, /*#__PURE__*/React.createElement(Slider // eslint-disable-next-line react/jsx-props-no-spreading
    , _extends({}, sliderProps, {
      className: className,
      onSlideEnd: this.handleSlideEnd
    }), /*#__PURE__*/React.createElement(Handles, null, ({
      handles,
      activeHandleID,
      getHandleProps
    }) => /*#__PURE__*/React.createElement("div", {
      className: "form-field-slider__handles"
    }, handles.map(handle => /*#__PURE__*/React.createElement(Handle, {
      key: handle.id,
      handle: handle,
      getLabelForValue: this.getLabelForValue,
      domain: sliderProps.domain,
      isActive: handle.id === activeHandleID,
      getHandleProps: getHandleProps,
      showTooltips: showTooltips
    })))), /*#__PURE__*/React.createElement(Tracks, null, ({
      tracks,
      getTrackProps
    }) => /*#__PURE__*/React.createElement("div", {
      className: "form-field-slider__tracks"
    }, tracks.map(({
      id,
      source,
      target
    }) => /*#__PURE__*/React.createElement(Track, {
      key: id,
      source: source,
      target: target,
      getTrackProps: getTrackProps
    })))), tickCount && /*#__PURE__*/React.createElement(Ticks, {
      count: tickCount
    }, ({
      ticks
    }) => /*#__PURE__*/React.createElement("div", {
      className: "form-field-slider__ticks"
    }, ticks.map((tick, index) => /*#__PURE__*/React.createElement(Tick, {
      tick: tick,
      key: `${this.getLabelForValue}_${index}`,
      getLabelForValue: this.getLabelForValue
    }))))));
  }

}

SliderInput.propTypes = {
  options: PropTypes.array,
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  type: PropTypes.string.isRequired,
  onBlur: PropTypes.func.isRequired,
  parameters: PropTypes.shape({
    minLabel: PropTypes.string,
    maxLabel: PropTypes.string
  })
};
SliderInput.defaultProps = {
  options: [],
  value: null,
  parameters: {}
};
export default SliderInput;