/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var select_index = require('../../select/index.js');
var colorPicker_hooks = require('../hooks.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_draggable = require('../../_common/js/color-picker/draggable.js');
require('../../select/select.js');
require('../../_chunks/dep-99705d51.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/typeof');
require('vue');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/set');
require('tdesign-icons-vue');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../loading/icon/gradient.js');
require('../../config.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('@babel/runtime/helpers/slicedToArray');
require('../../utils/classnames.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/render-tnode.js');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../loading/plugin.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../utils/map-props.js');
require('../../utils/mixins.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('../../input/index.js');
require('../../input/addon.js');
require('../../input/input.js');
require('lodash/kebabCase');
require('../../utils/event.js');
require('../../input/props.js');
require('../../input/input-group.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../tag/check-tag-props.js');
require('../../common-components/fake-arrow.js');
require('../../select/option.js');
require('../../utils/ripple.js');
require('../../select/option-props.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/checkbox-group-props.js');
require('../../select/props.js');
require('../../select/optionGroup.js');
require('../../select/option-group-props.js');
require('../../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');

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

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

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 SaturationPanel = VueCompositionAPI.defineComponent({
  name: "SaturationPanel",
  components: {
    TSelect: select_index.Select,
    TOption: select_index.Option
  },
  props: _objectSpread({}, colorPicker_panel_baseProps["default"]),
  setup: function setup(props) {
    var baseClassName = colorPicker_hooks.useBaseClassName();
    var refPanel = VueCompositionAPI.ref(null);
    var refThumb = VueCompositionAPI.ref(null);
    var dragInstance = VueCompositionAPI.ref(null);
    var panelRect = VueCompositionAPI.reactive({
      width: _common_js_colorPicker_constants.SATURATION_PANEL_DEFAULT_WIDTH,
      height: _common_js_colorPicker_constants.SATURATION_PANEL_DEFAULT_HEIGHT
    });
    var styles = VueCompositionAPI.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;
      }

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

    var panelBackground = VueCompositionAPI.computed(function () {
      return "hsl(".concat(props.color.hue, ", 100%, 50%)");
    });
    VueCompositionAPI.onMounted(function () {
      panelRect.width = refPanel.value.offsetWidth || _common_js_colorPicker_constants.SATURATION_PANEL_DEFAULT_WIDTH;
      panelRect.height = refPanel.value.offsetHeight || _common_js_colorPicker_constants.SATURATION_PANEL_DEFAULT_HEIGHT;
      dragInstance.value = new _common_js_colorPicker_draggable.Draggable(refPanel.value, {
        start: function start() {
          panelRect.width = refPanel.value.offsetWidth;
          panelRect.height = refPanel.value.offsetHeight;
        },
        drag: function drag(coordinate) {
          handleDrag(coordinate);
        },
        end: handleDragEnd
      });
    });
    VueCompositionAPI.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
    })]);
  }
});

exports["default"] = SaturationPanel;
//# sourceMappingURL=saturation.js.map
