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

import { _ as _defineProperty } from '../../_chunks/dep-2db7b3ec.mjs';
import { defineComponent, ref, reactive, watch, onMounted, onBeforeUnmount, createVNode } from 'vue';
import { c as cloneDeep_1 } from '../../_chunks/dep-095107f7.mjs';
import '../const.mjs';
import '../utils/index.mjs';
import { InputNumber } from '../../input-number/index.mjs';
import { useBaseClassName } from '../hooks.mjs';
import { useCommonClassName } from '../../hooks/useConfig.mjs';
import baseProps from './base-props.mjs';
import { GRADIENT_SLIDER_DEFAULT_WIDTH } from '../../_common/js/color-picker/constants.mjs';
import { genGradientPoint, gradientColors2string } from '../../_common/js/color-picker/color.mjs';
import '../../_chunks/dep-255303ba.mjs';
import '../../_chunks/dep-eba0ea6e.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-469b4ae0.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-3da99438.mjs';
import '../../_chunks/dep-b57180d0.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 '../../_common/js/color-picker/index.mjs';
import '../../_common/js/color-picker/cmyk.mjs';
import '../../_common/js/color-picker/draggable.mjs';
import '../../_chunks/dep-ccbc47f4.mjs';
import '../../_common/js/color-picker/gradient.mjs';
import '../../_chunks/dep-876fb3ec.mjs';
import '../../_chunks/dep-ed5ebb35.mjs';
import '../../_chunks/dep-9ac6ced8.mjs';
import '../../_chunks/dep-84acf3c2.mjs';
import '../../_chunks/dep-b8d7992c.mjs';
import '../../utils/withInstall.mjs';
import '../../input-number/input-number.mjs';
import '@ldesign/icons-vue-next';
import '../../button/index.mjs';
import '../../button/button.mjs';
import '../../_chunks/dep-bf1e33c8.mjs';
import '../../loading/index.mjs';
import '../../loading/directive.mjs';
import '../../loading/plugin.mjs';
import '../../utils/dom.mjs';
import '../../utils/easing.mjs';
import '../../loading/loading.mjs';
import '../../utils/render-tnode.mjs';
import '../../_chunks/dep-888e80c4.mjs';
import '../../_chunks/dep-406cd044.mjs';
import '../../_chunks/dep-42cc3015.mjs';
import '../../_chunks/dep-2bc37d52.mjs';
import '../../_chunks/dep-327559e8.mjs';
import '../../_chunks/dep-b2235f26.mjs';
import '../../_chunks/dep-8b989474.mjs';
import '../../_chunks/dep-1372b2d3.mjs';
import '../../_chunks/dep-9a85dac1.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/set-style.mjs';
import '../../_common/js/utils/helper.mjs';
import '../../_chunks/dep-60fae668.mjs';
import '../../_chunks/dep-78847b2e.mjs';
import '../../_chunks/dep-3f21269a.mjs';
import '../../config-provider/useConfig.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 '../../loading/style/css.mjs';
import '../../loading/type.mjs';
import '../../hooks/useRipple.mjs';
import '../../utils/set-style.mjs';
import '../../hooks/useKeepAnimation.mjs';
import '../../form/hooks.mjs';
import '../../hooks/tnode.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 '../../hooks/useVModel.mjs';
import '../../input/useLengthLimit.mjs';
import '../../_common/js/log/index.mjs';
import '../../_common/js/log/log.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 DELETE_KEYS = ["delete", "backspace"];
var LinearGradient = defineComponent({
  name: "LinearGradient",
  inheritAttrs: false,
  props: _objectSpread({}, baseProps),
  setup: function setup(props) {
    var baseClassName = useBaseClassName();
    var _useCommonClassName = useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var statusClassNames = STATUS.value;
    var refSlider = ref(null);
    var sliderRect = reactive({
      left: 0,
      width: GRADIENT_SLIDER_DEFAULT_WIDTH
    });
    var isDragging = ref(false);
    var isMoved = ref(false);
    var degree = ref(props.color.gradientDegree);
    var selectedId = ref(props.color.gradientSelectedId);
    var colors = ref(cloneDeep_1(props.color.gradientColors));
    watch(function () {
      return props.color.gradientDegree;
    }, function (value) {
      return degree.value = value;
    });
    watch(function () {
      return props.color.gradientSelectedId;
    }, function (value) {
      return selectedId.value = value;
    });
    watch(function () {
      return props.color.gradientColors;
    }, function (value) {
      colors.value = cloneDeep_1(value);
    }, {
      deep: true
    });
    var handleChange = function handleChange(key, payload, addUsedColor) {
      if (props.disabled) return;
      props.onChange({
        key: key,
        payload: payload,
        addUsedColor: addUsedColor
      });
    };
    var handleDegreeChange = function handleDegreeChange(value) {
      if (props.disabled || value === props.color.gradientDegree) return;
      degree.value = value;
      handleChange("degree", value, true);
    };
    var handleSelectedIdChange = function handleSelectedIdChange(value) {
      if (props.disabled) return;
      selectedId.value = value;
      handleChange("selectedId", value);
    };
    var handleColorsChange = function handleColorsChange(value, isEnded) {
      if (props.disabled) return;
      colors.value = value;
      handleChange("colors", value, isEnded);
    };
    var updateActiveThumbLeft = function updateActiveThumbLeft(left) {
      var index = colors.value.findIndex(function (c) {
        return c.id === selectedId.value;
      });
      if (index === -1) return;
      var point = colors.value[index];
      left = Math.max(0, Math.min(sliderRect.width, left));
      var percentLeft = left / sliderRect.width * 100;
      colors.value.splice(index, 1, {
        color: point.color,
        left: percentLeft,
        id: point.id
      });
      handleColorsChange(colors.value);
    };
    var handleStart = function handleStart(id) {
      var rect = refSlider.value.getBoundingClientRect();
      sliderRect.left = rect.left;
      sliderRect.width = rect.width || GRADIENT_SLIDER_DEFAULT_WIDTH;
      if (isDragging.value || props.disabled) return;
      isMoved.value = false;
      isDragging.value = true;
      handleSelectedIdChange(id);
      refSlider.value.focus();
      window.addEventListener("mousemove", handleMove, false);
      window.addEventListener("mouseup", handleEnd, false);
      window.addEventListener("contextmenu", handleEnd, false);
    };
    var handleMove = function handleMove(e) {
      if (!isDragging.value || props.disabled) return;
      var left = e.clientX - sliderRect.left;
      isMoved.value = true;
      updateActiveThumbLeft(left);
    };
    var handleEnd = function handleEnd() {
      if (!isDragging.value) return;
      setTimeout(function () {
        isDragging.value = false;
      }, 0);
      if (isMoved.value) {
        handleColorsChange(colors.value, true);
        isMoved.value = false;
      }
      window.removeEventListener("mousemove", handleMove, false);
      window.removeEventListener("mouseup", handleEnd, false);
      window.removeEventListener("contextmenu", handleEnd, false);
    };
    var handleKeyup = function handleKeyup(e) {
      if (props.disabled) return;
      var points = colors.value;
      var pos = points.findIndex(function (c) {
        return c.id === selectedId.value;
      });
      var length = points.length;
      if (DELETE_KEYS.includes(e.key.toLocaleLowerCase()) && length > 2 && pos >= 0 && pos <= length - 1) {
        points.splice(pos, 1);
        if (!points[pos]) pos = points[pos + 1] ? pos + 1 : points[pos - 1] ? pos - 1 : 0;
        var current = points[pos];
        handleColorsChange(points, true);
        handleSelectedIdChange(current === null || current === void 0 ? void 0 : current.id);
      }
    };
    var handleThumbBarClick = function handleThumbBarClick(e) {
      if (props.disabled) return;
      var left = e.clientX - sliderRect.left;
      left = Math.max(0, Math.min(sliderRect.width, left));
      var percentLeft = left / sliderRect.width * 100;
      var newPoint = genGradientPoint(percentLeft, props.color.rgba);
      colors.value.push(newPoint);
      handleColorsChange(colors.value, true);
      handleSelectedIdChange(newPoint.id);
    };
    onMounted(function () {
      var rect = refSlider.value.getBoundingClientRect();
      sliderRect.left = rect.left;
      sliderRect.width = rect.width || GRADIENT_SLIDER_DEFAULT_WIDTH;
    });
    onBeforeUnmount(function () {
      window.removeEventListener("mousemove", handleMove, false);
      window.removeEventListener("mouseup", handleEnd, false);
      window.removeEventListener("contextmenu", handleEnd, false);
    });
    return {
      baseClassName: baseClassName,
      statusClassNames: statusClassNames,
      refSlider: refSlider,
      degree: degree,
      selectedId: selectedId,
      colors: colors,
      handleDegreeChange: handleDegreeChange,
      handleStart: handleStart,
      handleMove: handleMove,
      handleEnd: handleEnd,
      handleKeyup: handleKeyup,
      handleThumbBarClick: handleThumbBarClick
    };
  },
  render: function render() {
    var _this = this;
    var _this$color = this.color,
      linearGradient = _this$color.linearGradient,
      gradientColors = _this$color.gradientColors;
    var colors = this.colors,
      selectedId = this.selectedId,
      degree = this.degree,
      disabled = this.disabled,
      baseClassName = this.baseClassName,
      statusClassNames = this.statusClassNames;
    var thumbBackground = gradientColors2string({
      points: gradientColors,
      degree: 90
    });
    return createVNode("div", {
      "class": "".concat(baseClassName, "__gradient")
    }, [createVNode("div", {
      "class": "".concat(baseClassName, "__gradient-slider")
    }, [createVNode("div", {
      "class": ["".concat(baseClassName, "__slider"), "".concat(baseClassName, "--bg-alpha")],
      "onKeyup": this.handleKeyup,
      "tabindex": 0,
      "ref": "refSlider"
    }, [createVNode("ul", {
      "class": "gradient-thumbs",
      "onClick": this.handleThumbBarClick,
      "style": {
        background: thumbBackground
      }
    }, [colors.map(function (t) {
      var left = "".concat(Math.round(t.left * 100) / 100, "%");
      return createVNode("li", {
        "class": ["".concat(baseClassName, "__thumb"), "gradient-thumbs__item", selectedId === t.id ? statusClassNames.active : ""],
        "key": t.id,
        "title": "".concat(t.color, " ").concat(left),
        "style": {
          color: t.color,
          left: left
        },
        "onClick": function onClick(e) {
          return e.stopPropagation();
        },
        "onMousedown": function onMousedown() {
          return _this.handleStart(t.id);
        }
      }, [createVNode("span", {
        "class": ["gradient-thumbs__item-inner", "".concat(baseClassName, "--bg-alpha")]
      }, null)]);
    })])])]), createVNode("div", {
      "class": "".concat(baseClassName, "__gradient-degree"),
      "title": "".concat(degree, "deg")
    }, [createVNode(InputNumber, {
      "size": "small",
      "theme": "normal",
      "min": 0,
      "max": 360,
      "step": 1,
      "format": function format(value) {
        return "".concat(value, "\xB0");
      },
      "modelValue": _this.degree,
      "onUpdate:modelValue": function onUpdateModelValue($event) {
        return _this.degree = $event;
      },
      "onBlur": this.handleDegreeChange,
      "onEnter": this.handleDegreeChange,
      "disabled": disabled
    }, null)]), createVNode("div", {
      "class": ["".concat(baseClassName, "__gradient-preview"), "".concat(baseClassName, "--bg-alpha")]
    }, [createVNode("span", {
      "class": "preview-inner",
      "style": {
        background: linearGradient
      }
    }, null)])]);
  }
});

export { LinearGradient as default };
//# sourceMappingURL=linear-gradient.mjs.map
