"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _react = _interopRequireDefault(require("react"));
var _color = _interopRequireDefault(require("color"));
var _clamp = _interopRequireDefault(require("lodash/clamp"));
var _ColorPickerHsbSt = require("./ColorPickerHsb.st.css.js");
var _ColorPickerKeyboardUtils = require("./ColorPickerKeyboardUtils");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/ColorPicker/ColorPickerHsb.jsx";
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2["default"])(o), (0, _possibleConstructorReturn2["default"])(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2["default"])(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var ColorPickerHsb = /*#__PURE__*/function (_React$PureComponent) {
  function ColorPickerHsb() {
    var _this;
    (0, _classCallCheck2["default"])(this, ColorPickerHsb);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ColorPickerHsb, [].concat(args));
    _this.onMarkerDragStart = function (e) {
      e.preventDefault();
      window.addEventListener('mousemove', _this.setNewColorByMouseEvent);
      window.addEventListener('touchmove', _this.setNewColorByMouseEvent);
      window.addEventListener('mouseup', _this.onMarkerDragEnd);
      window.addEventListener('touchend', _this.onMarkerDragEnd);
      window.addEventListener('touchcancel', _this.onMarkerDragEnd);
      _this.gradientRect = _this.gradient.getBoundingClientRect();
      _this.setNewColorByMouseEvent(e);
    };
    _this.onKeyDown = (0, _ColorPickerKeyboardUtils.createColorPickerKeyHandler)({
      onChange: _this.props.onChange,
      getCurrentColor: function getCurrentColor() {
        return _this.props.current;
      },
      getNewColor: _ColorPickerKeyboardUtils.getHsbColorFromKey,
      largeStep: 10
    });
    _this.onMarkerDragEnd = function () {
      window.removeEventListener('touchmove', _this.setNewColorByMouseEvent);
      window.removeEventListener('mousemove', _this.setNewColorByMouseEvent);
      window.removeEventListener('touchcancel', _this.onMarkerDragEnd);
      window.removeEventListener('touchend', _this.onMarkerDragEnd);
      window.removeEventListener('mouseup', _this.onMarkerDragEnd);
    };
    _this.getSVByMouseEvent = function (e) {
      var eventX = 0;
      var eventY = 0;
      if (e.clientX) {
        eventX = e.clientX;
        eventY = e.clientY;
      } else {
        eventX = e.touches[0].clientX;
        eventY = e.touches[0].clientY;
      }
      var x = eventX - _this.gradientRect.left;
      var y = eventY - _this.gradientRect.top;
      var s = (0, _clamp["default"])(100 * x / _this.gradientRect.width, 0, 100);
      var v = (0, _clamp["default"])(100 - 100 * y / _this.gradientRect.height, 0, 100);
      return {
        s: s,
        v: v
      };
    };
    _this.setNewColorByMouseEvent = function (e) {
      var _this$props = _this.props,
        onChange = _this$props.onChange,
        current = _this$props.current;
      var _this$getSVByMouseEve = _this.getSVByMouseEvent(e),
        s = _this$getSVByMouseEve.s,
        v = _this$getSVByMouseEve.v;
      onChange((0, _color["default"])({
        h: current.hue(),
        s: s,
        v: v
      }));
    };
    return _this;
  }
  (0, _inherits2["default"])(ColorPickerHsb, _React$PureComponent);
  return (0, _createClass2["default"])(ColorPickerHsb, [{
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.onMarkerDragEnd();
    }

    /**
     * Sets focus on the HSB selector
     */
  }, {
    key: "focus",
    value: function focus() {
      this.gradient && this.gradient.focus();
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;
      var _this$props2 = this.props,
        dataHook = _this$props2.dataHook,
        current = _this$props2.current;
      var hue = current.saturationv(100).lightness(50);
      var style = {
        left: "".concat(current.saturationv(), "%"),
        top: "".concat(100 - current.value(), "%")
      };
      var saturation = Math.round(current.saturationv());
      var value = Math.round(current.value());
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: _ColorPickerHsbSt.classes.root,
        "data-hook": dataHook,
        ref: function ref(e) {
          _this2.gradient = e;
        },
        onMouseDown: this.onMarkerDragStart,
        onTouchStart: this.onMarkerDragStart,
        onKeyDown: this.onKeyDown,
        tabIndex: 0,
        role: "application",
        "aria-label": "Saturation and brightness selector. Use arrow keys to adjust.",
        "aria-valuetext": "Saturation ".concat(saturation, "%, Brightness ").concat(value, "%"),
        "aria-valuemin": 0,
        "aria-valuemax": 100,
        "aria-valuenow": saturation,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 87,
          columnNumber: 7
        }
      }, /*#__PURE__*/_react["default"].createElement("div", {
        className: _ColorPickerHsbSt.classes.hue,
        style: {
          background: hue.hex()
        },
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 104,
          columnNumber: 9
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: _ColorPickerHsbSt.classes.saturation,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 105,
          columnNumber: 9
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: _ColorPickerHsbSt.classes.brightness,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 106,
          columnNumber: 9
        }
      }), /*#__PURE__*/_react["default"].createElement("div", {
        className: _ColorPickerHsbSt.classes.handle,
        style: style,
        "aria-hidden": "true",
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 107,
          columnNumber: 9
        }
      }));
    }
  }]);
}(_react["default"].PureComponent);
var _default = exports["default"] = ColorPickerHsb;