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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var cloneDeep = require('lodash/cloneDeep');
var constants = require('../../../chunks/constants.js');
var color = require('../../../chunks/color.js');
var components_inputNumber_index = require('../../input-number/index.js');
var components_colorPicker_hooks_index = require('../hooks/index.js');
var hooks_useCommonClassname = require('../../../hooks/use-common-classname.js');
var components_colorPicker_panel_baseProps = require('./base-props.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../utils/map-props.js');
require('vue');
require('../../../utils/with-install.js');
require('lodash');
require('../../../config.js');
require('../../input-number/input-number.js');
require('../../../chunks/helper.js');
require('@ldesign/icons-vue');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../../chunks/log.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../../utils/easing.js');
require('../../../utils/render-node.js');
require('../../../utils/transfer-dom.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('../../../chunks/zh_CN.js');
require('../../../utils/mixins.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/set-style.js');
require('../../../chunks/helper2.js');
require('../../loading/plugin.js');
require('../../../utils/ripple.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../../utils/helper.js');
require('../../../utils/event.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../../hooks/use-global-icon.js');
require('../../config-provider/useConfig.js');
require('../../input-number/props.js');
require('../../input-number/hooks/use-input-number.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/isArray-cd7357b9.js');
require('../../../chunks/isNumber-c4eb560e.js');
require('../../../hooks/use-vmodel.js');
require('../../../hooks/use-form-disabled.js');
require('../../../hooks/use-config.js');

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

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

const DELETE_KEYS = ["delete", "backspace"];
var LinearGradient = VueCompositionAPI.defineComponent({
  name: "LinearGradient",
  components: {
    LInputNumber: components_inputNumber_index.InputNumber
  },
  inheritAttrs: false,
  props: {
    ...components_colorPicker_panel_baseProps["default"]
  },
  setup(props) {
    const baseClassName = components_colorPicker_hooks_index.useBaseClassName();
    const {
      statusClassNames
    } = hooks_useCommonClassname["default"]();
    const refSlider = VueCompositionAPI.ref(null);
    const sliderRect = VueCompositionAPI.reactive({
      left: 0,
      width: constants.GRADIENT_SLIDER_DEFAULT_WIDTH
    });
    const isDragging = VueCompositionAPI.ref(false);
    const isMoved = VueCompositionAPI.ref(false);
    const colorInstance = VueCompositionAPI.computed(() => cloneDeep__default["default"](props.color));
    const degree = VueCompositionAPI.ref(colorInstance.value.gradientDegree);
    const selectedId = VueCompositionAPI.ref(colorInstance.value.gradientSelectedId);
    const colors = VueCompositionAPI.ref(cloneDeep__default["default"](colorInstance.value.gradientColors));
    VueCompositionAPI.watch(() => props.color.gradientDegree, value => degree.value = value);
    VueCompositionAPI.watch(() => props.color.gradientSelectedId, value => selectedId.value = value);
    VueCompositionAPI.watch(() => props.color.gradientColors, value => {
      colors.value = cloneDeep__default["default"](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 || constants.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 = color.genGradientPoint(percentLeft, colorInstance.value.rgba);
      colors.value.push(newPoint);
      handleColorsChange(colors.value, true);
      handleSelectedIdChange(newPoint.id);
    };
    VueCompositionAPI.onMounted(() => {
      const rect = refSlider.value.getBoundingClientRect();
      sliderRect.left = rect.left;
      sliderRect.width = rect.width || constants.GRADIENT_SLIDER_DEFAULT_WIDTH;
    });
    VueCompositionAPI.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 = color.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
      }
    })])]);
  }
});

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