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

import { defineComponent, ref, reactive, computed, nextTick, onMounted, onBeforeUnmount } from '@vue/composition-api';
import { SATURATION_PANEL_DEFAULT_WIDTH, SATURATION_PANEL_DEFAULT_HEIGHT } from '../const.js';
import { Select, Option } from '../../select/index.js';
import { Draggable } from '../utils/index.js';
import { useBaseClassName } from '../hooks/index.js';
import baseProps from './base-props.js';
import '../../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../../config.js';
import '../../select/select.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import 'lodash/isObject';
import '../../../chunks/index.js';
import '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-vmodel.js';
import 'lodash/kebabCase';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import '../../../hooks/render-lnode.js';
import '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '../../../utils/transfer-dom.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-838e4192.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../popup/index.js';
import '../../../utils/map-props.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/style';
import '../../popup/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '@ldesign/icons-vue';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style';
import '../../input/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../../../hooks/use-config.js';
import '../../select-input/hooks/use-multiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../../hooks/use-global-icon.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style';
import '../../tag-input/type.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style';
import '../../select-input/type.js';
import '../../common/fake-arrow.js';
import '../../../hooks/use-form-disabled.js';
import '../../select/option.js';
import '../../../utils/ripple.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/style';
import '../../checkbox/type.js';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../../../hooks/use-common-classname.js';
import '../../select/utils.js';
import '../../select/props.js';
import '../../select/select-panel.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../select/hooks/use-select-options.js';
import '../../select/option-group.js';
import '../../select/style';
import '../../select/type.js';

var SaturationPanel = defineComponent({
  name: "SaturationPanel",
  components: {
    LSelect: Select,
    LOption: Option
  },
  props: {
    ...baseProps
  },
  setup(props) {
    const baseClassName = useBaseClassName();
    const refPanel = ref(null);
    const refThumb = ref(null);
    const dragInstance = ref(null);
    const panelRect = reactive({
      width: SATURATION_PANEL_DEFAULT_WIDTH,
      height: SATURATION_PANEL_DEFAULT_HEIGHT
    });
    const styles = computed(() => {
      const {
        saturation,
        value
      } = props.color;
      const {
        width,
        height
      } = panelRect;
      const top = Math.round((1 - value) * height);
      const left = Math.round(saturation * width);
      return {
        color: props.color.rgb,
        left: `${left}px`,
        top: `${top}px`
      };
    });
    const getSaturationAndValueByCoordinate = coordinate => {
      const {
        width,
        height
      } = panelRect;
      const {
        x,
        y
      } = coordinate;
      const saturation = Math.round(x / width * 100);
      const value = Math.round((1 - y / height) * 100);
      return {
        saturation,
        value
      };
    };
    const handleDrag = (coordinate, isEnded) => {
      if (props.disabled) return;
      const {
        saturation,
        value
      } = getSaturationAndValueByCoordinate(coordinate);
      props.handleChange({
        saturation: saturation / 100,
        value: value / 100,
        addUsedColor: isEnded
      });
    };
    const handleDragEnd = coordinate => {
      if (props.disabled) return;
      nextTick(() => {
        handleDrag(coordinate, true);
      });
    };
    const panelBackground = computed(() => `hsl(${props.color.hue}, 100%, 50%)`);
    onMounted(() => {
      panelRect.width = refPanel.value.offsetWidth || SATURATION_PANEL_DEFAULT_WIDTH;
      panelRect.height = refPanel.value.offsetHeight || SATURATION_PANEL_DEFAULT_HEIGHT;
      dragInstance.value = new Draggable(refPanel.value, {
        start() {
          panelRect.width = refPanel.value.offsetWidth;
          panelRect.height = refPanel.value.offsetHeight;
        },
        drag: coordinate => {
          handleDrag(coordinate);
        },
        end: handleDragEnd
      });
    });
    onBeforeUnmount(() => {
      dragInstance.value.destroy();
    });
    return {
      baseClassName,
      refThumb,
      refPanel,
      styles,
      panelBackground
    };
  },
  render() {
    const h = arguments[0];
    const {
      baseClassName,
      styles,
      panelBackground
    } = this;
    return h("div", {
      "class": [`${baseClassName}__saturation`],
      "ref": "refPanel",
      "style": {
        background: panelBackground
      }
    }, [h("span", {
      "class": [`${baseClassName}__thumb`],
      "attrs": {
        "role": "slider",
        "tabindex": 0
      },
      "ref": "refThumb",
      "style": styles
    })]);
  }
});

export { SaturationPanel as default };
//# sourceMappingURL=saturation.js.map
