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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, computed, inject, ref, reactive, watchEffect, nextTick, createVNode, mergeProps } from 'vue';
import isFunction from 'lodash/isFunction';
import { Tooltip } from '../tooltip/index.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useSliderTooltip } from './hooks/useSliderTooltip.js';
import { sliderPropsInjectKey } from './util/constants.js';
import '../utils/withInstall.js';
import '../tooltip/tooltip.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/omit';
import '../popup/props.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import 'lodash/isString';
import 'lodash/isObject';
import '../utils/dom.js';
import '../_chunks/dep-512eb181.js';
import 'lodash/isArray';
import '../utils/easing.js';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../_common/js/utils/set-style.js';
import '../hooks/useVModel.js';
import '../popup/container.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/isUndefined';
import '../hooks/useResizeObserver.js';
import '../popup/style';
import '../popup/type.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import 'dayjs';
import '../_chunks/dep-ac28b67c.js';
import '../config-provider/type.js';
import '../hooks/tnode.js';
import '../tooltip/util.js';
import '../tooltip/props.js';
import '../tooltip/style';
import '../tooltip/type.js';
import './util/common.js';

var _SliderButton = 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 = usePrefixClass("slider__button");
    var tooltipConfig = computed(function () {
      return props;
    });
    var _useSliderTooltip = useSliderTooltip(tooltipConfig),
      tooltipRef = _useSliderTooltip.tooltipRef,
      tooltipProps = _useSliderTooltip.tooltipProps,
      toggleTooltip = _useSliderTooltip.toggleTooltip,
      showTooltip = _useSliderTooltip.showTooltip;
    var parentProps = inject(sliderPropsInjectKey);
    var buttonRef = ref();
    var slideButtonProps = reactive({
      dragging: false,
      isClick: false,
      startX: 0,
      startY: 0,
      startPos: 0,
      newPos: null
    });
    var rangeDiff = computed(function () {
      return Number(parentProps.max) - Number(parentProps.min);
    });
    var currentPos = computed(function () {
      return "".concat((props.value - parentProps.min) / rangeDiff.value * 100, "%");
    });
    var step = computed(function () {
      return parentProps.step;
    });
    var wrapperStyle = computed(function () {
      return props.vertical ? {
        bottom: currentPos.value
      } : {
        left: currentPos.value
      };
    });
    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);
      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(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 createVNode("div", 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 ? createVNode(Tooltip, mergeProps({
        "ref": tooltipRef,
        "disabled": !showTooltip.value
      }, tooltipProps.value), {
        "default": function _default() {
          return [createVNode("div", {
            "class": [COMPONENT_NAME.value, _defineProperty({}, "".concat(COMPONENT_NAME.value, "--dragging"), slideButtonProps.dragging)]
          }, null)];
        }
      }) : createVNode("div", {
        "class": [COMPONENT_NAME.value, _defineProperty({}, "".concat(COMPONENT_NAME.value, "--dragging"), slideButtonProps.dragging)]
      }, null)]);
    };
  }
});

export { _SliderButton as default };
//# sourceMappingURL=slider-button.js.map
