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

import { _ as _toConsumableArray } from '../_chunks/dep-bf1e33c8.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { defineComponent, toRefs, ref, computed, watch, onMounted, onBeforeUnmount, createVNode, provide, reactive } from 'vue';
import { i as isArray_1 } from '../_chunks/dep-b57180d0.mjs';
import { i as isNumber_1 } from '../_chunks/dep-3f21269a.mjs';
import { useFormDisabled } from '../form/hooks.mjs';
import { usePrefixClass, useCommonClassName } from '../hooks/useConfig.mjs';
import useVModel from '../hooks/useVModel.mjs';
import props from './props.mjs';
import _SliderButton from './slider-button.mjs';
import { useSliderMark } from './hooks/useSliderMark.mjs';
import { useSliderInput } from './hooks/useSliderInput.mjs';
import { formatSliderValue, getStopStyle } from './util/common.mjs';
import { sliderPropsInjectKey } from './util/constants.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../tooltip/index.mjs';
import '../utils/withInstall.mjs';
import '../tooltip/tooltip.mjs';
import '../_chunks/dep-350764bc.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../popup/props.mjs';
import '../popup/index.mjs';
import '../popup/popup.mjs';
import '@popperjs/core';
import '../utils/dom.mjs';
import '../utils/easing.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_common/js/utils/set-style.mjs';
import '../popup/container.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../hooks/useResizeObserver.mjs';
import '../popup/style/css.mjs';
import '../popup/type.mjs';
import '../hooks/tnode.mjs';
import '../tooltip/util.mjs';
import '../tooltip/props.mjs';
import '../tooltip/style/css.mjs';
import '../tooltip/type.mjs';
import './hooks/useSliderTooltip.mjs';
import '../_common/js/log/log.mjs';
import './slider-mark.mjs';
import '../_chunks/dep-35e3b1aa.mjs';
import '../input-number/index.mjs';
import '../input-number/input-number.mjs';
import '@ldesign/icons-vue-next';
import '../button/index.mjs';
import '../button/button.mjs';
import '../loading/index.mjs';
import '../loading/directive.mjs';
import '../loading/plugin.mjs';
import '../loading/loading.mjs';
import '../hooks/useTeleport.mjs';
import '../loading/props.mjs';
import '../loading/icon/gradient.mjs';
import '../_common/js/loading/circle-adapter.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../button/props.mjs';
import '../button/style/css.mjs';
import '../button/type.mjs';
import '../input/index.mjs';
import '../input/input.mjs';
import '../hooks/useGlobalIcon.mjs';
import '../input/props.mjs';
import '../input/useInput.mjs';
import '../form/const.mjs';
import '../hooks/index.mjs';
import '../hooks/icon.mjs';
import '../hooks/slot.mjs';
import '../hooks/useCommonClassName.mjs';
import '../hooks/useDefaultValue.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../hooks/useVirtualScroll.mjs';
import '../input/useLengthLimit.mjs';
import '../_common/js/log/index.mjs';
import '../input/useInputEventHandler.mjs';
import '../input/useInputWidth.mjs';
import '../input/input-group.mjs';
import '../input/input-group-props.mjs';
import '../input/style/css.mjs';
import '../input/type.mjs';
import '../input-number/props.mjs';
import '../input-number/useInputNumber.mjs';
import '../_common/js/input-number/number.mjs';
import '../_chunks/dep-01e3f446.mjs';
import '../_common/js/input-number/large-number.mjs';
import '../input-number/style/css.mjs';
import '../input-number/type.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var _Slider = defineComponent({
  name: "TSlider",
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread({}, props),
  setup: function setup(props2) {
    var disabled = useFormDisabled();
    var COMPONENT_NAME = usePrefixClass("slider");
    var _useCommonClassName = useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      sliderValue = _useVModel2[0],
      setSliderValue = _useVModel2[1];
    var sliderContainerRef = ref();
    var sliderRef = ref();
    var firstButtonRef = ref();
    var secondButtonRef = ref();
    var firstValue = ref(formatSliderValue(sliderValue.value, "first"));
    var secondValue = ref(formatSliderValue(sliderValue.value, "second"));
    var dragging = ref(false);
    var sliderSize = ref(1);
    var vertical = computed(function () {
      return props2.layout === "vertical";
    });
    var containerClass = computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__container"), {
        "is-vertical": vertical.value
      }];
    });
    var sliderClass = computed(function () {
      var _ref;
      return ["".concat(COMPONENT_NAME.value), (_ref = {
        "is-vertical": vertical.value
      }, _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--with-input"), props2.inputNumberProps), _defineProperty(_ref, "".concat(COMPONENT_NAME.value, "--vertical"), vertical.value), _defineProperty(_ref, STATUS.value.disabled, disabled.value), _ref)];
    });
    var sliderRailClass = computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__rail"), {
        "show-input": props2.inputNumberProps,
        "disabled": disabled.value
      }];
    });
    var runwayStyle = computed(function () {
      return vertical.value ? {
        height: "100%"
      } : {};
    });
    var barStyle = computed(function () {
      var barStart = props2.range ? "".concat(100 * (minValue.value - props2.min) / rangeDiff.value, "%") : "0%";
      var currentDiff = props2.range ? maxValue.value - minValue.value : firstValue.value - props2.min;
      var barSize = "".concat(100 * currentDiff / rangeDiff.value, "%");
      return vertical.value ? {
        height: barSize,
        bottom: barStart
      } : {
        width: barSize,
        left: barStart
      };
    });
    var rangeDiff = computed(function () {
      return props2.max - props2.min;
    });
    var minValue = computed(function () {
      return Math.min(firstValue.value, secondValue.value);
    });
    var maxValue = computed(function () {
      return Math.max(firstValue.value, secondValue.value);
    });
    var steps = computed(function () {
      if (!props2.showStep || props2.min > props2.max) return [];
      if (props2.step === 0) {
        console.warn("[Element Warn][Slider]step should not be 0.");
        return [];
      }
      var stepCount = rangeDiff.value / props2.step;
      var stepWidth = 100 * props2.step / rangeDiff.value;
      var result = [];
      for (var i = 1; i < stepCount; i++) result.push(i * stepWidth);
      if (props2.range) {
        var r = result.filter(function (step2) {
          return step2 < 100 * (minValue.value - props2.min) / rangeDiff.value || props2.step > 100 * (maxValue.value - props2.max) / rangeDiff.value;
        });
        return r;
      }
      return result.filter(function (step2) {
        return step2 > 100 * (firstValue.value - props2.min) / rangeDiff.value;
      });
    });
    var precision = computed(function () {
      var precisions = [props2.min, props2.max, props2.step].map(function (item) {
        var decimalArr = "".concat(item).split(".");
        return decimalArr[1] ? decimalArr[1].length : 0;
      });
      return Math.max.apply(null, precisions);
    });
    var setValues = function setValues(value2) {
      var _ref2 = [props2.min, props2.max],
        min2 = _ref2[0],
        max2 = _ref2[1];
      if (min2 > max2) {
        console.warn("[Slider] max should be greater than min.");
        return;
      }
      if (props2.range && isArray_1(value2)) {
        var _ref3 = [Math.min.apply(Math, _toConsumableArray(value2)), Math.max.apply(Math, _toConsumableArray(value2))],
          maxLimit = _ref3[0],
          minLimit = _ref3[1];
        if (maxLimit > max2) maxLimit = firstValue.value;
        if (maxLimit < min2) maxLimit = min2;
        if (minLimit < min2) minLimit = secondValue.value;
        if (minLimit > max2) minLimit = max2;
        var _ref4 = [maxLimit, minLimit];
        firstValue.value = _ref4[0];
        secondValue.value = _ref4[1];
        return [maxLimit, minLimit];
      }
      var preValue = value2;
      if (preValue < min2) preValue = min2;
      if (preValue > max2) preValue = max2;
      return preValue;
    };
    var emitChange = function emitChange(value2) {
      var changeValue2 = value2;
      if (changeValue2 === void 0) {
        if (props2.range) changeValue2 = [firstValue.value, secondValue.value];else changeValue2 = firstValue.value;
      }
      var fixValue = setValues(changeValue2);
      setSliderValue(fixValue);
    };
    var resetSize = function resetSize() {
      if (sliderRef.value) sliderSize.value = sliderRef.value["client".concat(vertical.value ? "Height" : "Width")];
    };
    var isAlreadyMount = ref(false);
    var init = function init() {
      var valuetext;
      if (props2.range) {
        if (isArray_1(sliderValue.value)) {
          firstValue.value = Math.max(props2.min || 0, sliderValue.value[0]);
          secondValue.value = Math.min(props2.max || 100, sliderValue.value[1]);
        } else {
          firstValue.value = props2.min || 0;
          secondValue.value = props2.max || 100;
        }
        valuetext = "".concat(firstValue.value, "-").concat(secondValue.value);
      } else {
        if (!isNumber_1(sliderValue.value)) firstValue.value = props2.min;else firstValue.value = Math.min(props2.max, Math.max(props2.min, sliderValue.value));
        valuetext = String(firstValue.value);
      }
      if (sliderContainerRef.value) sliderContainerRef.value.setAttribute("aria-valuetext", valuetext);
      resetSize();
      window.addEventListener("resize", resetSize);
    };
    var setPosition = function setPosition(percent) {
      var targetValue = percent * rangeDiff.value / 100;
      targetValue = props2.min + targetValue;
      if (!props2.range && firstButtonRef.value) {
        firstButtonRef.value.setPosition(percent);
        return;
      }
      var button;
      if (Math.abs(minValue.value - targetValue) < Math.abs(maxValue.value - targetValue)) button = firstValue.value < secondValue.value ? "button1" : "button2";else button = firstValue.value > secondValue.value ? "button1" : "button2";
      if (button === "button1" && firstButtonRef.value) firstButtonRef.value.setPosition(percent);
      if (button === "button2" && secondButtonRef.value) secondButtonRef.value.setPosition(percent);
    };
    var onSliderClick = function onSliderClick(event) {
      if (disabled.value || dragging.value) return;
      if (!sliderRef.value) return;
      resetSize();
      var value2 = 0;
      if (vertical.value) {
        var sliderOffsetBottom = sliderRef.value.getBoundingClientRect().bottom;
        value2 = (sliderOffsetBottom - event.clientY) / sliderSize.value * 100;
        setPosition(value2);
      } else {
        var sliderOffsetLeft = sliderRef.value.getBoundingClientRect().left;
        value2 = (event.clientX - sliderOffsetLeft) / sliderSize.value * 100;
        setPosition(value2);
      }
    };
    var changeValue = function changeValue(point) {
      if (disabled.value || dragging.value) return;
      resetSize();
      var value2 = Number(point / rangeDiff.value * 100);
      setPosition(value2);
      emitChange(point);
    };
    watch(function () {
      return sliderValue.value;
    }, function (newVal) {
      if (dragging.value === true) return;
      if (isArray_1(newVal) && props2.range) {
        var _newVal = _slicedToArray(newVal, 2);
        firstValue.value = _newVal[0];
        secondValue.value = _newVal[1];
      } else firstValue.value = newVal;
    });
    watch([firstValue, secondValue, dragging], function (newStates, prevStates) {
      if (newStates[0] !== prevStates[0]) {
        if (props2.range) emitChange([minValue.value, maxValue.value]);else emitChange(firstValue.value);
      }
      if (newStates[1] !== prevStates[1]) {
        if (props2.range) emitChange([minValue.value, maxValue.value]);
      }
      if (newStates[2] !== prevStates[2] && newStates[2] === false) init();
    });
    onMounted(function () {
      init();
      isAlreadyMount.value = true;
    });
    onBeforeUnmount(function () {
      window.removeEventListener("resize", resetSize);
    });
    var markConfig = computed(function () {
      return {
        max: props2.max,
        min: props2.min,
        marks: props2.marks,
        vertical: vertical.value,
        prefixName: COMPONENT_NAME.value
      };
    });
    var renderMask = useSliderMark(markConfig);
    var inputConfig = computed(function () {
      return {
        max: props2.max,
        min: props2.min,
        inputNumberProps: props2.inputNumberProps,
        step: props2.step,
        prefixName: COMPONENT_NAME.value,
        vertical: vertical.value,
        disabled: disabled.value
      };
    });
    var renderInputNumber = useSliderInput(inputConfig);
    var renderInputButton = function renderInputButton() {
      var firstInputVal = firstValue.value;
      var firstInputOnChange = function firstInputOnChange(v) {
        firstValue.value = v;
      };
      var secondInputVal = secondValue.value;
      var secondInputOnChange = function secondInputOnChange(v) {
        secondValue.value = v;
      };
      return createVNode("div", {
        "class": ["".concat(COMPONENT_NAME.value, "__input-container"), {
          "is-vertical": vertical.value
        }]
      }, [renderInputNumber(firstInputVal, firstInputOnChange), props2.range && createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__center-line")
      }, null), props2.range && renderInputNumber(secondInputVal, secondInputOnChange)]);
    };
    var toggleDragging = function toggleDragging(toState) {
      dragging.value = toState;
    };
    var _toRefs2 = toRefs(props2),
      min = _toRefs2.min,
      max = _toRefs2.max,
      step = _toRefs2.step;
    provide(sliderPropsInjectKey, reactive({
      min: min,
      max: max,
      step: step,
      dragging: dragging,
      toggleDragging: toggleDragging,
      precision: precision,
      disabled: disabled,
      resetSize: resetSize,
      sliderSize: sliderSize
    }));
    return function () {
      return createVNode("div", {
        "class": containerClass.value,
        "ref": sliderContainerRef
      }, [createVNode("div", {
        "class": sliderClass.value,
        "role": "slider",
        "aria-valuemin": props2.min,
        "aria-valuemax": props2.max,
        "aria-orientation": props2.layout,
        "aria-disabled": disabled.value,
        "tooltip-props": props2.tooltipProps
      }, [createVNode("div", {
        "class": sliderRailClass.value,
        "style": runwayStyle.value,
        "onClick": onSliderClick,
        "ref": sliderRef
      }, [createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__track"),
        "style": barStyle.value
      }, null), createVNode(_SliderButton, {
        "vertical": vertical.value,
        "value": firstValue.value,
        "ref": firstButtonRef,
        "disabled": disabled.value,
        "tooltip-props": props2.tooltipProps,
        "label": props2.label,
        "onInput": function onInput(v) {
          firstValue.value = v;
        }
      }, null), props2.range && createVNode(_SliderButton, {
        "vertical": vertical.value,
        "value": secondValue.value,
        "ref": secondButtonRef,
        "disabled": disabled.value,
        "label": props2.label,
        "tooltip-props": props2.tooltipProps,
        "onInput": function onInput(v) {
          secondValue.value = v;
        }
      }, null), props2.showStep && createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__stops")
      }, [steps.value.map(function (item, key) {
        if (item.position === 0 || item.position === 100) return null;
        return createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__stop"),
          "key": key,
          "style": getStopStyle(item, vertical.value)
        }, null);
      })]), renderMask(changeValue)])]), isAlreadyMount.value && props2.inputNumberProps && renderInputButton()]);
    };
  }
});

export { _Slider as default };
//# sourceMappingURL=slider.mjs.map
