/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isFunction = require('lodash/isFunction');
var tooltip_index = require('../tooltip/index.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var slider_hooks_useSliderTooltip = require('./hooks/useSliderTooltip.js');
var slider_util_constants = require('./util/constants.js');
require('../utils/withInstall.js');
require('../tooltip/tooltip.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/omit');
require('../popup/props.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('lodash/isString');
require('lodash/isObject');
require('../utils/dom.js');
require('../_chunks/dep-f4db2590.js');
require('lodash/isArray');
require('../utils/easing.js');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../_common/js/utils/set-style.js');
require('../hooks/useVModel.js');
require('../popup/container.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/isUndefined');
require('../hooks/useResizeObserver.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('dayjs');
require('../hooks/tnode.js');
require('../tooltip/util.js');
require('../tooltip/props.js');
require('./util/common.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);

var _SliderButton = vue.defineComponent({
  name: "TSliderButton",
  props: {
    value: {
      type: [Number],
      "default": 0
    },
    vertical: {
      type: Boolean,
      "default": false
    },
    tooltipProps: {
      type: [Boolean, Object],
      "default": true
    },
    label: {
      type: [String, Boolean, Function],
      "default": false
    }
  },
  emits: ["input"],
  setup: function setup(props, ctx) {
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("slider__button");
    var tooltipConfig = vue.computed(function () {
      return props;
    });
    var _useSliderTooltip = slider_hooks_useSliderTooltip.useSliderTooltip(tooltipConfig),
      tooltipRef = _useSliderTooltip.tooltipRef,
      tooltipProps = _useSliderTooltip.tooltipProps,
      toggleTooltip = _useSliderTooltip.toggleTooltip,
      showTooltip = _useSliderTooltip.showTooltip;
    var parentProps = vue.inject(slider_util_constants.sliderPropsInjectKey);
    var buttonRef = vue.ref();
    var slideButtonProps = vue.reactive({
      dragging: false,
      isClick: false,
      startX: 0,
      startY: 0,
      startPos: 0,
      newPos: null
    });
    var rangeDiff = vue.computed(function () {
      return Number(parentProps.max) - Number(parentProps.min);
    });
    var currentPos = vue.computed(function () {
      return "".concat((props.value - parentProps.min) / rangeDiff.value * 100, "%");
    });
    var step = vue.computed(function () {
      return parentProps.step;
    });
    var wrapperStyle = vue.computed(function () {
      return props.vertical ? {
        bottom: currentPos.value
      } : {
        left: currentPos.value
      };
    });
    vue.watchEffect(function () {
      parentProps.toggleDragging(slideButtonProps.dragging);
    });
    var setPosition = function setPosition(pos) {
      var newPos = pos;
      if (newPos === null || Number.isNaN(newPos)) return;
      if (newPos > 100) newPos = 100;else if (newPos < 0) newPos = 0;
      var perStepLen = 100 * step.value / rangeDiff.value;
      var steps = Math.round(newPos / perStepLen);
      var value = steps * perStepLen * rangeDiff.value * 0.01;
      value += parentProps.min;
      value = Number(parseFloat("".concat(value)).toFixed(parentProps.precision));
      ctx.emit("input", value);
      vue.nextTick(function () {
        var _tooltipRef$value$upd, _tooltipRef$value;
        tooltipRef.value && ((_tooltipRef$value$upd = (_tooltipRef$value = tooltipRef.value).updatePopper) === null || _tooltipRef$value$upd === void 0 ? void 0 : _tooltipRef$value$upd.call(_tooltipRef$value));
      });
    };
    var handleMouseEnter = function handleMouseEnter() {
      buttonRef.value.focus();
      toggleTooltip(true);
    };
    var handleMouseLeave = function handleMouseLeave() {
      if (!slideButtonProps.dragging) toggleTooltip(false);
    };
    var onDragStart = function onDragStart(event) {
      slideButtonProps.dragging = true;
      slideButtonProps.isClick = true;
      var type = event.type;
      var clientY = event.clientY,
        clientX = event.clientX;
      if (type === "touchstart") {
        var touch = event.touches;
        var _ref = [touch[0].clientY, touch[0].clientX];
        clientY = _ref[0];
        clientX = _ref[1];
      }
      if (props.vertical) slideButtonProps.startY = clientY;else slideButtonProps.startX = clientX;
      slideButtonProps.startPos = parseFloat(currentPos.value);
      slideButtonProps.newPos = slideButtonProps.startPos;
    };
    var onDragging = function onDragging(e) {
      var event = e;
      if (!slideButtonProps.dragging) return;
      slideButtonProps.isClick = false;
      if (parentProps !== null && parentProps !== void 0 && parentProps.resetSize && isFunction__default["default"](parentProps === null || parentProps === void 0 ? void 0 : parentProps.resetSize)) parentProps.resetSize();
      var diff = 0;
      var parentSliderSize = parentProps.sliderSize;
      if (props.vertical) diff = slideButtonProps.startY - event.clientY;else diff = event.clientX - slideButtonProps.startX;
      diff = diff / parentSliderSize * 100;
      slideButtonProps.newPos = slideButtonProps.startPos + diff;
      setPosition(slideButtonProps.newPos);
    };
    var onDragEnd = function onDragEnd() {
      if (slideButtonProps.dragging) {
        setTimeout(function () {
          slideButtonProps.dragging = false;
          toggleTooltip(false);
          if (!slideButtonProps.isClick) setPosition(slideButtonProps.newPos);
        }, 0);
        window.removeEventListener("mousemove", onDragging);
        window.removeEventListener("touchmove", onDragging);
        window.removeEventListener("mouseup", onDragEnd);
        window.removeEventListener("touchend", onDragEnd);
        window.removeEventListener("contextmenu", onDragEnd);
      }
    };
    function onButtonDown(event) {
      if (parentProps.disabled) return;
      event.preventDefault();
      onDragStart(event);
      window.addEventListener("mousemove", onDragging);
      window.addEventListener("mouseup", onDragEnd);
      window.addEventListener("touchmove", onDragging);
      window.addEventListener("touchend", onDragEnd);
      window.addEventListener("contextmenu", onDragEnd);
    }
    var onKeyDown = function onKeyDown(state) {
      if (parentProps.disabled) return;
      var stepLength = step.value / rangeDiff.value * 100;
      if (state === "sub") stepLength = -stepLength;
      slideButtonProps.newPos = parseFloat(currentPos.value) + stepLength;
      setPosition(slideButtonProps.newPos);
    };
    var onNativeKeyDown = function onNativeKeyDown(e) {
      var code = e.code;
      e.preventDefault();
      if (code === "ArrowDown" || code === "ArrowLeft") onKeyDown("sub");
      if (code === "ArrowUp" || code === "ArrowRight") onKeyDown("add");
    };
    ctx.expose({
      setPosition: setPosition
    });
    return function () {
      return vue.createVNode("div", vue.mergeProps({
        "ref": buttonRef,
        "class": "".concat(COMPONENT_NAME.value, "-wrapper"),
        "style": wrapperStyle.value,
        "tabindex": "0",
        "show-tooltip": showTooltip.value
      }, {
        disabled: parentProps.disabled
      }, {
        "onMouseenter": handleMouseEnter,
        "onMouseleave": handleMouseLeave,
        "onMousedown": onButtonDown,
        "onTouchstart": onButtonDown,
        "onFocus": handleMouseEnter,
        "onBlur": handleMouseLeave,
        "onKeydown": onNativeKeyDown
      }), [showTooltip.value ? vue.createVNode(tooltip_index.Tooltip, vue.mergeProps({
        "ref": tooltipRef,
        "disabled": !showTooltip.value
      }, tooltipProps.value), {
        "default": function _default() {
          return [vue.createVNode("div", {
            "class": [COMPONENT_NAME.value, _defineProperty__default["default"]({}, "".concat(COMPONENT_NAME.value, "--dragging"), slideButtonProps.dragging)]
          }, null)];
        }
      }) : vue.createVNode("div", {
        "class": [COMPONENT_NAME.value, _defineProperty__default["default"]({}, "".concat(COMPONENT_NAME.value, "--dragging"), slideButtonProps.dragging)]
      }, null)]);
    };
  }
});

exports["default"] = _SliderButton;
//# sourceMappingURL=slider-button.js.map
