/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
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 '../../select/select.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../hooks/render-lnode.js';
import 'lodash/isString';
import 'lodash/isObject';
import '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../select/props.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import 'vue';
import '../../../chunks/defineProperty-28395935.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../../../config.js';
import '../../../utils/dom.js';
import 'raf';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../../utils/with-install.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../../utils/map-props.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 '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style';
import '../../select-input/type.js';
import '../../common/fake-arrow.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 '../../select/utils.js';
import '../../../hooks/use-common-classname.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';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

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 SaturationPanel = defineComponent({
  name: "SaturationPanel",
  components: {
    LSelect: Select,
    LOption: Option
  },
  props: _objectSpread({}, baseProps),
  setup: function setup(props) {
    var baseClassName = useBaseClassName();
    var refPanel = ref(null);
    var refThumb = ref(null);
    var dragInstance = ref(null);
    var panelRect = reactive({
      width: SATURATION_PANEL_DEFAULT_WIDTH,
      height: SATURATION_PANEL_DEFAULT_HEIGHT
    });
    var styles = computed(function () {
      var _props$color = props.color,
          saturation = _props$color.saturation,
          value = _props$color.value;
      var width = panelRect.width,
          height = panelRect.height;
      var top = Math.round((1 - value) * height);
      var left = Math.round(saturation * width);
      return {
        color: props.color.rgb,
        left: "".concat(left, "px"),
        top: "".concat(top, "px")
      };
    });

    var getSaturationAndValueByCoordinate = function getSaturationAndValueByCoordinate(coordinate) {
      var width = panelRect.width,
          height = panelRect.height;
      var x = coordinate.x,
          y = coordinate.y;
      var saturation = Math.round(x / width * 100);
      var value = Math.round((1 - y / height) * 100);
      return {
        saturation: saturation,
        value: value
      };
    };

    var handleDrag = function handleDrag(coordinate, isEnded) {
      if (props.disabled) {
        return;
      }

      var _getSaturationAndValu = getSaturationAndValueByCoordinate(coordinate),
          saturation = _getSaturationAndValu.saturation,
          value = _getSaturationAndValu.value;

      props.handleChange({
        saturation: saturation / 100,
        value: value / 100,
        addUsedColor: isEnded
      });
    };

    var handleDragEnd = function handleDragEnd(coordinate) {
      if (props.disabled) {
        return;
      }

      nextTick(function () {
        handleDrag(coordinate, true);
      });
    };

    var panelBackground = computed(function () {
      return "hsl(".concat(props.color.hue, ", 100%, 50%)");
    });
    onMounted(function () {
      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: function start() {
          panelRect.width = refPanel.value.offsetWidth;
          panelRect.height = refPanel.value.offsetHeight;
        },
        drag: function drag(coordinate) {
          handleDrag(coordinate);
        },
        end: handleDragEnd
      });
    });
    onBeforeUnmount(function () {
      dragInstance.value.destroy();
    });
    return {
      baseClassName: baseClassName,
      refThumb: refThumb,
      refPanel: refPanel,
      styles: styles,
      panelBackground: panelBackground
    };
  },
  render: function render() {
    var h = arguments[0];
    var baseClassName = this.baseClassName,
        styles = this.styles,
        panelBackground = this.panelBackground;
    return h("div", {
      "class": ["".concat(baseClassName, "__saturation")],
      "ref": "refPanel",
      "style": {
        background: panelBackground
      }
    }, [h("span", {
      "class": ["".concat(baseClassName, "__thumb")],
      "attrs": {
        "role": "slider",
        "tabindex": 0
      },
      "ref": "refThumb",
      "style": styles
    })]);
  }
});

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