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

'use strict';

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

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var isArray = require('lodash/isArray');
var isNumber = require('lodash/isNumber');
var form_hooks = require('../form/hooks.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var slider_props = require('./props.js');
var slider_sliderButton = require('./slider-button.js');
var slider_hooks_useSliderMark = require('./hooks/useSliderMark.js');
var slider_hooks_useSliderInput = require('./hooks/useSliderInput.js');
var slider_util_common = require('./util/common.js');
var slider_util_constants = require('./util/constants.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
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('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('lodash/kebabCase');
require('../tooltip/index.js');
require('../utils/withInstall.js');
require('../tooltip/tooltip.js');
require('lodash/omit');
require('../popup/props.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('lodash/isObject');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('../hooks/useResizeObserver.js');
require('../hooks/tnode.js');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('../tooltip/util.js');
require('../tooltip/props.js');
require('./hooks/useSliderTooltip.js');
require('../_common/js/log/log.js');
require('./slider-mark.js');
require('lodash/isBoolean');
require('../input-number/index.js');
require('../input-number/input-number.js');
require('@ldesign/icons-vue-next');
require('../button/index.js');
require('../button/button.js');
require('../loading/index.js');
require('../loading/directive.js');
require('../loading/plugin.js');
require('../loading/loading.js');
require('../hooks/useTeleport.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../_common/js/loading/circle-adapter.js');
require('../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('../hooks/useRipple.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../button/props.js');
require('../input/index.js');
require('../input/input.js');
require('../hooks/useGlobalIcon.js');
require('../input/props.js');
require('../input/useInput.js');
require('../form/const.js');
require('../input/useLengthLimit.js');
require('../input/useInputEventHandler.js');
require('../input/useInputWidth.js');
require('../input/input-group.js');
require('../input/input-group-props.js');
require('../input-number/props.js');
require('../input-number/useInputNumber.js');
require('../hooks/useCommonClassName.js');
require('../_common/js/input-number/number.js');
require('lodash/isNil');
require('../_common/js/input-number/large-number.js');

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

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);

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__default["default"](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 = vue.defineComponent({
  name: "TSlider",
  model: {
    prop: "value",
    event: "change"
  },
  props: _objectSpread({}, slider_props["default"]),
  setup: function setup(props2) {
    var disabled = form_hooks.useFormDisabled();
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("slider");
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var _toRefs = vue.toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = hooks_useVModel["default"](value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      sliderValue = _useVModel2[0],
      setSliderValue = _useVModel2[1];
    var sliderContainerRef = vue.ref();
    var sliderRef = vue.ref();
    var firstButtonRef = vue.ref();
    var secondButtonRef = vue.ref();
    var firstValue = vue.ref(slider_util_common.formatSliderValue(sliderValue.value, "first"));
    var secondValue = vue.ref(slider_util_common.formatSliderValue(sliderValue.value, "second"));
    var dragging = vue.ref(false);
    var sliderSize = vue.ref(1);
    var vertical = vue.computed(function () {
      return props2.layout === "vertical";
    });
    var containerClass = vue.computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__container"), {
        "is-vertical": vertical.value
      }];
    });
    var sliderClass = vue.computed(function () {
      var _ref;
      return ["".concat(COMPONENT_NAME.value), (_ref = {
        "is-vertical": vertical.value
      }, _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME.value, "--with-input"), props2.inputNumberProps), _defineProperty__default["default"](_ref, "".concat(COMPONENT_NAME.value, "--vertical"), vertical.value), _defineProperty__default["default"](_ref, STATUS.value.disabled, disabled.value), _ref)];
    });
    var sliderRailClass = vue.computed(function () {
      return ["".concat(COMPONENT_NAME.value, "__rail"), {
        "show-input": props2.inputNumberProps,
        "disabled": disabled.value
      }];
    });
    var runwayStyle = vue.computed(function () {
      return vertical.value ? {
        height: "100%"
      } : {};
    });
    var barStyle = vue.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 = vue.computed(function () {
      return props2.max - props2.min;
    });
    var minValue = vue.computed(function () {
      return Math.min(firstValue.value, secondValue.value);
    });
    var maxValue = vue.computed(function () {
      return Math.max(firstValue.value, secondValue.value);
    });
    var steps = vue.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 = vue.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__default["default"](value2)) {
        var _ref3 = [Math.min.apply(Math, _toConsumableArray__default["default"](value2)), Math.max.apply(Math, _toConsumableArray__default["default"](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 = vue.ref(false);
    var init = function init() {
      var valuetext;
      if (props2.range) {
        if (isArray__default["default"](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__default["default"](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);
    };
    vue.watch(function () {
      return sliderValue.value;
    }, function (newVal) {
      if (dragging.value === true) return;
      if (isArray__default["default"](newVal) && props2.range) {
        var _newVal = _slicedToArray__default["default"](newVal, 2);
        firstValue.value = _newVal[0];
        secondValue.value = _newVal[1];
      } else firstValue.value = newVal;
    });
    vue.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();
    });
    vue.onMounted(function () {
      init();
      isAlreadyMount.value = true;
    });
    vue.onBeforeUnmount(function () {
      window.removeEventListener("resize", resetSize);
    });
    var markConfig = vue.computed(function () {
      return {
        max: props2.max,
        min: props2.min,
        marks: props2.marks,
        vertical: vertical.value,
        prefixName: COMPONENT_NAME.value
      };
    });
    var renderMask = slider_hooks_useSliderMark.useSliderMark(markConfig);
    var inputConfig = vue.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 = slider_hooks_useSliderInput.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 vue.createVNode("div", {
        "class": ["".concat(COMPONENT_NAME.value, "__input-container"), {
          "is-vertical": vertical.value
        }]
      }, [renderInputNumber(firstInputVal, firstInputOnChange), props2.range && vue.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 = vue.toRefs(props2),
      min = _toRefs2.min,
      max = _toRefs2.max,
      step = _toRefs2.step;
    vue.provide(slider_util_constants.sliderPropsInjectKey, vue.reactive({
      min: min,
      max: max,
      step: step,
      dragging: dragging,
      toggleDragging: toggleDragging,
      precision: precision,
      disabled: disabled,
      resetSize: resetSize,
      sliderSize: sliderSize
    }));
    return function () {
      return vue.createVNode("div", {
        "class": containerClass.value,
        "ref": sliderContainerRef
      }, [vue.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
      }, [vue.createVNode("div", {
        "class": sliderRailClass.value,
        "style": runwayStyle.value,
        "onClick": onSliderClick,
        "ref": sliderRef
      }, [vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__track"),
        "style": barStyle.value
      }, null), vue.createVNode(slider_sliderButton["default"], {
        "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 && vue.createVNode(slider_sliderButton["default"], {
        "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 && vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "__stops")
      }, [steps.value.map(function (item, key) {
        if (item.position === 0 || item.position === 100) return null;
        return vue.createVNode("div", {
          "class": "".concat(COMPONENT_NAME.value, "__stop"),
          "key": key,
          "style": slider_util_common.getStopStyle(item, vertical.value)
        }, null);
      })]), renderMask(changeValue)])]), isAlreadyMount.value && props2.inputNumberProps && renderInputButton()]);
    };
  }
});

exports["default"] = _Slider;
//# sourceMappingURL=slider.js.map
