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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var cloneDeep = require('lodash/cloneDeep');
var inputNumber_index = require('../../input-number/index.js');
var colorPicker_hooks = require('../hooks.js');
var hooks_useConfig = require('../../hooks/useConfig.js');
var colorPicker_panel_baseProps = require('./base-props.js');
var _common_js_colorPicker_constants = require('../../_common/js/color-picker/constants.js');
var _common_js_colorPicker_color = require('../../_common/js/color-picker/color.js');
require('../../utils/withInstall.js');
require('../../input-number/input-number.js');
require('@ldesign/icons-vue-next');
require('../../button/index.js');
require('../../button/button.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../../loading/index.js');
require('../../loading/directive.js');
require('../../loading/plugin.js');
require('../../utils/dom.js');
require('../../_chunks/dep-f4db2590.js');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isArray');
require('../../utils/easing.js');
require('../../loading/loading.js');
require('@babel/runtime/helpers/slicedToArray');
require('../../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/useTeleport.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('../../config-provider/useConfig.js');
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('dayjs');
require('../../hooks/useRipple.js');
require('../../utils/set-style.js');
require('../../hooks/useKeepAnimation.js');
require('../../form/hooks.js');
require('../../hooks/tnode.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('../../hooks/useVModel.js');
require('../../input/useLengthLimit.js');
require('../../_common/js/log/log.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');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('tinycolor2');
require('../../_common/js/color-picker/cmyk.js');
require('../../_common/js/color-picker/gradient.js');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var cloneDeep__default = /*#__PURE__*/_interopDefaultLegacy(cloneDeep);

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 DELETE_KEYS = ["delete", "backspace"];
var LinearGradient = vue.defineComponent({
  name: "LinearGradient",
  inheritAttrs: false,
  props: _objectSpread({}, colorPicker_panel_baseProps["default"]),
  setup: function setup(props) {
    var baseClassName = colorPicker_hooks.useBaseClassName();
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var statusClassNames = STATUS.value;
    var refSlider = vue.ref(null);
    var sliderRect = vue.reactive({
      left: 0,
      width: _common_js_colorPicker_constants.GRADIENT_SLIDER_DEFAULT_WIDTH
    });
    var isDragging = vue.ref(false);
    var isMoved = vue.ref(false);
    var degree = vue.ref(props.color.gradientDegree);
    var selectedId = vue.ref(props.color.gradientSelectedId);
    var colors = vue.ref(cloneDeep__default["default"](props.color.gradientColors));
    vue.watch(function () {
      return props.color.gradientDegree;
    }, function (value) {
      return degree.value = value;
    });
    vue.watch(function () {
      return props.color.gradientSelectedId;
    }, function (value) {
      return selectedId.value = value;
    });
    vue.watch(function () {
      return props.color.gradientColors;
    }, function (value) {
      colors.value = cloneDeep__default["default"](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 || _common_js_colorPicker_constants.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 = _common_js_colorPicker_color.genGradientPoint(percentLeft, props.color.rgba);
      colors.value.push(newPoint);
      handleColorsChange(colors.value, true);
      handleSelectedIdChange(newPoint.id);
    };
    vue.onMounted(function () {
      var rect = refSlider.value.getBoundingClientRect();
      sliderRect.left = rect.left;
      sliderRect.width = rect.width || _common_js_colorPicker_constants.GRADIENT_SLIDER_DEFAULT_WIDTH;
    });
    vue.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 = _common_js_colorPicker_color.gradientColors2string({
      points: gradientColors,
      degree: 90
    });
    return vue.createVNode("div", {
      "class": "".concat(baseClassName, "__gradient")
    }, [vue.createVNode("div", {
      "class": "".concat(baseClassName, "__gradient-slider")
    }, [vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__slider"), "".concat(baseClassName, "--bg-alpha")],
      "onKeyup": this.handleKeyup,
      "tabindex": 0,
      "ref": "refSlider"
    }, [vue.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 vue.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);
        }
      }, [vue.createVNode("span", {
        "class": ["gradient-thumbs__item-inner", "".concat(baseClassName, "--bg-alpha")]
      }, null)]);
    })])])]), vue.createVNode("div", {
      "class": "".concat(baseClassName, "__gradient-degree"),
      "title": "".concat(degree, "deg")
    }, [vue.createVNode(inputNumber_index.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)]), vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__gradient-preview"), "".concat(baseClassName, "--bg-alpha")]
    }, [vue.createVNode("span", {
      "class": "preview-inner",
      "style": {
        background: linearGradient
      }
    }, null)])]);
  }
});

exports["default"] = LinearGradient;
//# sourceMappingURL=linear-gradient.js.map
