"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SliderProps = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _propsUtil = require("../_util/props-util");

var _Slider = _interopRequireDefault(require("../vc-slider/src/Slider"));

var _Range = _interopRequireDefault(require("../vc-slider/src/Range"));

var _Handle = _interopRequireDefault(require("../vc-slider/src/Handle"));

var _tooltip = _interopRequireDefault(require("../tooltip"));

var _configProvider = require("../config-provider");

var _abstractTooltipProps = _interopRequireDefault(require("../tooltip/abstractTooltipProps"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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; }

var tooltipProps = (0, _abstractTooltipProps.default)();

var SliderProps = function SliderProps() {
  return {
    prefixCls: _vueTypes.default.string,
    tooltipPrefixCls: _vueTypes.default.string,
    range: _vueTypes.default.bool,
    reverse: _vueTypes.default.bool,
    min: _vueTypes.default.number,
    max: _vueTypes.default.number,
    step: _vueTypes.default.oneOfType([_vueTypes.default.number, _vueTypes.default.any]),
    marks: _vueTypes.default.object,
    dots: _vueTypes.default.bool,
    value: _vueTypes.default.oneOfType([_vueTypes.default.number, _vueTypes.default.arrayOf(_vueTypes.default.number)]),
    defaultValue: _vueTypes.default.oneOfType([_vueTypes.default.number, _vueTypes.default.arrayOf(_vueTypes.default.number)]),
    included: _vueTypes.default.bool,
    disabled: _vueTypes.default.bool,
    vertical: _vueTypes.default.bool,
    tipFormatter: _vueTypes.default.oneOfType([_vueTypes.default.func, _vueTypes.default.object]),
    tooltipVisible: _vueTypes.default.bool,
    tooltipPlacement: tooltipProps.placement,
    getTooltipPopupContainer: _vueTypes.default.func,
    onChange: _vueTypes.default.func,
    'onUpdate:value': _vueTypes.default.func,
    onAfterChange: _vueTypes.default.func
  };
};

exports.SliderProps = SliderProps;

var defaultTipFormatter = function defaultTipFormatter(value) {
  return value.toString();
};

var Slider = {
  name: 'ASlider',
  inheritAttrs: false,
  // model: {
  //   prop: 'value',
  //   event: 'change',
  // },
  mixins: [_BaseMixin.default],
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  props: _objectSpread({}, SliderProps()),
  data: function data() {
    return {
      visibles: {}
    };
  },
  methods: {
    toggleTooltipVisible: function toggleTooltipVisible(index, visible) {
      this.setState(function (_ref) {
        var visibles = _ref.visibles;
        return {
          visibles: _objectSpread(_objectSpread({}, visibles), {}, _defineProperty({}, index, visible))
        };
      });
    },
    handleWithTooltip: function handleWithTooltip(tooltipPrefixCls, prefixCls, _ref2) {
      var _this = this;

      var value = _ref2.value,
          dragging = _ref2.dragging,
          index = _ref2.index,
          restProps = _objectWithoutProperties(_ref2, ["value", "dragging", "index"]);

      var _this$$props = this.$props,
          _this$$props$tipForma = _this$$props.tipFormatter,
          tipFormatter = _this$$props$tipForma === void 0 ? defaultTipFormatter : _this$$props$tipForma,
          tooltipVisible = _this$$props.tooltipVisible,
          tooltipPlacement = _this$$props.tooltipPlacement,
          getTooltipPopupContainer = _this$$props.getTooltipPopupContainer;
      var visibles = this.visibles;
      var isTipFormatter = tipFormatter ? visibles[index] || dragging : false;
      var visible = tooltipVisible || tooltipVisible === undefined && isTipFormatter;
      var tooltipProps = {
        prefixCls: tooltipPrefixCls,
        title: tipFormatter ? tipFormatter(value) : '',
        visible: visible,
        placement: tooltipPlacement || 'top',
        transitionName: 'zoom-down',
        overlayClassName: "".concat(prefixCls, "-tooltip"),
        getPopupContainer: getTooltipPopupContainer || function () {
          return document.body;
        },
        key: index
      };

      var handleProps = _objectSpread(_objectSpread({
        value: value
      }, restProps), {}, {
        onMouseenter: function onMouseenter() {
          return _this.toggleTooltipVisible(index, true);
        },
        onMouseleave: function onMouseleave() {
          return _this.toggleTooltipVisible(index, false);
        }
      });

      return _vue.createVNode(_tooltip.default, tooltipProps, {
        default: function _default() {
          return [_vue.createVNode(_Handle.default, handleProps, null)];
        }
      });
    },
    saveSlider: function saveSlider(node) {
      this.vcSlider = node;
    },
    focus: function focus() {
      this.vcSlider.focus();
    },
    blur: function blur() {
      this.vcSlider.blur();
    },
    handleChange: function handleChange(val) {
      this.$emit('update:value', val);
      this.$emit('change', val);
    }
  },
  render: function render() {
    var _this2 = this;

    var _getOptionProps$this$ = _objectSpread(_objectSpread({}, (0, _propsUtil.getOptionProps)(this)), this.$attrs),
        range = _getOptionProps$this$.range,
        customizePrefixCls = _getOptionProps$this$.prefixCls,
        customizeTooltipPrefixCls = _getOptionProps$this$.tooltipPrefixCls,
        restProps = _objectWithoutProperties(_getOptionProps$this$, ["range", "prefixCls", "tooltipPrefixCls"]);

    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('slider', customizePrefixCls);
    var tooltipPrefixCls = getPrefixCls('tooltip', customizeTooltipPrefixCls);

    if (range) {
      var vcRangeProps = _objectSpread(_objectSpread({}, restProps), {}, {
        prefixCls: prefixCls,
        tooltipPrefixCls: tooltipPrefixCls,
        handle: function handle(info) {
          return _this2.handleWithTooltip(tooltipPrefixCls, prefixCls, info);
        },
        ref: this.saveSlider,
        onChange: this.handleChange
      });

      return _vue.createVNode(_Range.default, vcRangeProps, null);
    }

    var vcSliderProps = _objectSpread(_objectSpread({}, restProps), {}, {
      prefixCls: prefixCls,
      tooltipPrefixCls: tooltipPrefixCls,
      handle: function handle(info) {
        return _this2.handleWithTooltip(tooltipPrefixCls, prefixCls, info);
      },
      ref: this.saveSlider,
      onChange: this.handleChange
    });

    return _vue.createVNode(_Slider.default, vcSliderProps, null);
  }
};
/* istanbul ignore next */

Slider.install = function (app) {
  app.component(Slider.name, Slider);
};

var _default2 = Slider;
exports.default = _default2;