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

import { _ as _toConsumableArray } from '../_chunks/dep-e091cfd9.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { defineComponent, toRefs, ref, computed, watch, onMounted, onBeforeUnmount, createVNode, provide, reactive } from 'vue';
import { i as isArray_1 } from '../_chunks/dep-e3b14d70.mjs';
import { i as isNumber_1 } from '../_chunks/dep-6eea369f.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-cfcedf47.mjs';
import '../_chunks/dep-b4041200.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-6b22dd27.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-c852a61a.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-99920870.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../config-provider/type.mjs';
import '../_chunks/dep-c4e1a112.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../tooltip/index.mjs';
import '../utils/withInstall.mjs';
import '../tooltip/tooltip.mjs';
import '../_chunks/dep-b2af48ba.mjs';
import '../_chunks/dep-7acc11cb.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-774ac2d0.mjs';
import '../popup/props.mjs';
import '../popup/index.mjs';
import '../popup/popup.mjs';
import '@popperjs/core';
import '../hooks/index.mjs';
import '../hooks/icon.mjs';
import '../hooks/slot.mjs';
import '../hooks/tnode.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../hooks/useCommonClassName.mjs';
import '../hooks/useDefaultValue.mjs';
import '../hooks/useDestroyOnClose.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useVirtualScroll.mjs';
import '../utils/dom.mjs';
import '../utils/easing.mjs';
import '../_common/js/utils/set-style.mjs';
import '../popup/container.mjs';
import '../hooks/useResizeObserver.mjs';
import '../popup/style/css.mjs';
import '../popup/type.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-0cfc2d3f.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-b6a73f93.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../_chunks/dep-30451c48.mjs';
import '../loading/style/css.mjs';
import '../loading/type.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 '../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-51aacdc9.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
