/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { defineComponent, ref, reactive, computed, watch, onMounted, onBeforeUnmount } from '@vue/composition-api';
import cloneDeep from 'lodash/cloneDeep';
import { GRADIENT_SLIDER_DEFAULT_WIDTH } from '../const.js';
import { genGradientPoint, gradientColors2string } from '../utils/index.js';
import { InputNumber } from '../../input-number/index.js';
import { useBaseClassName } from '../hooks/index.js';
import useCommonClassName from '../../../hooks/use-common-classname.js';
import baseProps from './base-props.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import 'lodash';
import '../../../config.js';
import '../../input-number/input-number.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '@ldesign/icons-vue';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/transfer-dom.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style/css.js';
import '../../button/type.js';
import '../../input/index.js';
import '../../input/input.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../utils/helper.js';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style/css.js';
import '../../input/type.js';
import '../../../hooks/use-global-icon.js';
import '../../../hooks/use-config.js';
import '../../config-provider/useConfig.js';
import '../../input-number/props.js';
import '../../input-number/hooks/use-input-number.js';
import '../../../chunks/isObjectLike-c22c7e3b.js';
import '../../../chunks/isArray-cd7357b9.js';
import '../../../chunks/isNumber-c4eb560e.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/use-form-disabled.js';
import '../../input-number/style/css.js';
import '../../input-number/type.js';

const DELETE_KEYS = ["delete", "backspace"];
var LinearGradient = defineComponent({
  name: "LinearGradient",
  components: {
    LInputNumber: InputNumber
  },
  inheritAttrs: false,
  props: {
    ...baseProps
  },
  setup(props) {
    const baseClassName = useBaseClassName();
    const {
      statusClassNames
    } = useCommonClassName();
    const refSlider = ref(null);
    const sliderRect = reactive({
      left: 0,
      width: GRADIENT_SLIDER_DEFAULT_WIDTH
    });
    const isDragging = ref(false);
    const isMoved = ref(false);
    const colorInstance = computed(() => cloneDeep(props.color));
    const degree = ref(colorInstance.value.gradientDegree);
    const selectedId = ref(colorInstance.value.gradientSelectedId);
    const colors = ref(cloneDeep(colorInstance.value.gradientColors));
    watch(() => props.color.gradientDegree, value => degree.value = value);
    watch(() => props.color.gradientSelectedId, value => selectedId.value = value);
    watch(() => props.color.gradientColors, value => {
      colors.value = cloneDeep(value);
    }, {
      deep: true
    });
    const handleChange = (key, payload, addUsedColor) => {
      if (props.disabled) return;
      props.handleChange({
        key,
        payload,
        addUsedColor
      });
    };
    const handleDegreeChange = value => {
      if (props.disabled || value === colorInstance.value.gradientDegree) return;
      degree.value = value;
      handleChange("degree", value, true);
    };
    const handleSelectedIdChange = value => {
      if (props.disabled) return;
      selectedId.value = value;
      handleChange("selectedId", value);
    };
    const handleColorsChange = (value, isEnded) => {
      if (props.disabled) return;
      colors.value = value;
      handleChange("colors", value, isEnded);
    };
    const updateActiveThumbLeft = left => {
      const index = colors.value.findIndex(c => c.id === selectedId.value);
      if (index === -1) return;
      const point = colors.value[index];
      left = Math.max(0, Math.min(sliderRect.width, left));
      const percentLeft = left / sliderRect.width * 100;
      colors.value.splice(index, 1, {
        color: point.color,
        left: percentLeft,
        id: point.id
      });
      handleColorsChange(colors.value);
    };
    const handleStart = (id, e) => {
      const 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;
      e.preventDefault();
      e.stopPropagation();
      handleSelectedIdChange(id);
      refSlider.value.focus();
      window.addEventListener("mousemove", handleMove, false);
      window.addEventListener("mouseup", handleEnd, false);
      window.addEventListener("contextmenu", handleEnd, false);
    };
    const handleMove = e => {
      if (!isDragging.value || props.disabled) return;
      const left = e.clientX - sliderRect.left;
      isMoved.value = true;
      updateActiveThumbLeft(left);
    };
    const handleEnd = () => {
      if (!isDragging.value) return;
      setTimeout(() => {
        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);
    };
    const handleKeyup = e => {
      if (props.disabled) return;
      const points = colors.value;
      let pos = points.findIndex(c => c.id === selectedId.value);
      const {
        length
      } = points;
      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;
        const current = points[pos];
        handleColorsChange(points, true);
        handleSelectedIdChange(current === null || current === void 0 ? void 0 : current.id);
      }
    };
    const handleThumbBarClick = e => {
      if (props.disabled) return;
      let left = e.clientX - sliderRect.left;
      left = Math.max(0, Math.min(sliderRect.width, left));
      const percentLeft = left / sliderRect.width * 100;
      const newPoint = genGradientPoint(percentLeft, colorInstance.value.rgba);
      colors.value.push(newPoint);
      handleColorsChange(colors.value, true);
      handleSelectedIdChange(newPoint.id);
    };
    onMounted(() => {
      const rect = refSlider.value.getBoundingClientRect();
      sliderRect.left = rect.left;
      sliderRect.width = rect.width || GRADIENT_SLIDER_DEFAULT_WIDTH;
    });
    onBeforeUnmount(() => {
      window.removeEventListener("mousemove", handleMove, false);
      window.removeEventListener("mouseup", handleEnd, false);
      window.removeEventListener("contextmenu", handleEnd, false);
    });
    return {
      baseClassName,
      statusClassNames,
      refSlider,
      degree,
      selectedId,
      colors,
      colorInstance,
      handleDegreeChange,
      handleStart,
      handleMove,
      handleEnd,
      handleKeyup,
      handleThumbBarClick
    };
  },
  render() {
    const h = arguments[0];
    const {
      linearGradient,
      gradientColors
    } = this.colorInstance;
    const {
      colors,
      selectedId,
      degree,
      disabled,
      baseClassName,
      statusClassNames
    } = this;
    const thumbBackground = gradientColors2string({
      points: gradientColors,
      degree: 90
    });
    return h("div", {
      "class": `${baseClassName}__gradient`
    }, [h("div", {
      "class": `${baseClassName}__gradient-slider`
    }, [h("div", {
      "class": [`${baseClassName}__slider`, `${baseClassName}--bg-alpha`],
      "on": {
        "keyup": this.handleKeyup
      },
      "attrs": {
        "tabindex": 0
      },
      "ref": "refSlider"
    }, [h("ul", {
      "class": "gradient-thumbs",
      "on": {
        "click": this.handleThumbBarClick
      },
      "style": {
        background: thumbBackground
      }
    }, [colors.map(t => {
      const left = `${Math.round(t.left * 100) / 100}%`;
      return h("li", {
        "class": [`${baseClassName}__thumb`, "gradient-thumbs__item", selectedId === t.id ? statusClassNames.active : ""],
        "key": t.id,
        "attrs": {
          "title": `${t.color} ${left}`
        },
        "style": {
          color: t.color,
          left
        },
        "on": {
          "click": e => e.stopPropagation(),
          "mousedown": e => this.handleStart(t.id, e)
        }
      }, [h("span", {
        "class": ["gradient-thumbs__item-inner", `${baseClassName}--bg-alpha`]
      })]);
    })])])]), h("div", {
      "class": `${baseClassName}__gradient-degree`,
      "attrs": {
        "title": `${degree}deg`
      }
    }, [h("l-input-number", {
      "attrs": {
        "theme": "normal",
        "size": "small",
        "min": 0,
        "max": 360,
        "step": 1,
        "format": value => `${value}\xB0`,
        "disabled": disabled
      },
      "on": {
        "blur": this.handleDegreeChange,
        "enter": this.handleDegreeChange
      },
      "model": {
        value: this.degree,
        callback: $$v => {
          this.degree = $$v;
        }
      }
    })]), h("div", {
      "class": [`${baseClassName}__gradient-preview`, `${baseClassName}--bg-alpha`]
    }, [h("span", {
      "class": "preview-inner",
      "style": {
        background: linearGradient
      }
    })])]);
  }
});

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