"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
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 _Input = _interopRequireDefault(require("../Input"));
var _ColorPickerConverterSt = require("./ColorPickerConverter.st.css.js");
var _ColorPickerConverterViewer = _interopRequireDefault(require("./ColorPickerConverterViewer"));
var _utils = require("./utils");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/ColorPicker/ColorPickerConverterHex.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 ColorPickerConverterHex = exports["default"] = /*#__PURE__*/function (_React$PureComponent) {
  function ColorPickerConverterHex() {
    var _this;
    (0, _classCallCheck2["default"])(this, ColorPickerConverterHex);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ColorPickerConverterHex, [].concat(args));
    _this.state = {
      hex: (0, _utils.getHexOrEmpty)(_this.props.current),
      inFocus: false
    };
    _this.change = function (_ref) {
      var value = _ref.target.value;
      _this.setState({
        hex: value
      }, function () {
        var _color = (0, _utils.safeColor)(value, _this.props.allowEmpty);
        if (_color) {
          _this.props.onChange(_color);
        }
      });
    };
    _this.handleOnFocus = function () {
      _this.setState({
        inFocus: true
      });
    };
    _this.handleOnBlur = function () {
      _this.setState({
        inFocus: false,
        hex: (0, _utils.getHexOrEmpty)(_this.props.current)
      });
    };
    _this.handleKeyDown = function (event) {
      var key = event.key;
      if (key === 'Enter') {
        _this.props.onEnter();
      }
    };
    return _this;
  }
  (0, _inherits2["default"])(ColorPickerConverterHex, _React$PureComponent);
  return (0, _createClass2["default"])(ColorPickerConverterHex, [{
    key: "render",
    value: function render() {
      var dataHook = this.props.dataHook;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: (0, _ColorPickerConverterSt.st)(_ColorPickerConverterSt.classes.colorConverterWrapper),
        "data-hook": dataHook,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 17,
          columnNumber: 7
        }
      }, /*#__PURE__*/_react["default"].createElement(_Input["default"], {
        size: "medium",
        value: this.state.hex,
        placeholder: this.props.placeholder,
        onChange: this.change,
        onFocus: this.handleOnFocus,
        onBlur: this.handleOnBlur,
        onKeyDown: this.handleKeyDown,
        className: _ColorPickerConverterSt.classes.colorInput,
        onInputClicked: _utils.stopPropagation,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 18,
          columnNumber: 9
        }
      }), /*#__PURE__*/_react["default"].createElement(_ColorPickerConverterViewer["default"], (0, _extends2["default"])({}, this.props, {
        color: this.props.current,
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 29,
          columnNumber: 9
        }
      })));
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(props) {
      var hex = (0, _utils.getHexOrEmpty)(props.current);
      if (!this.state.inFocus && this.state.hex !== hex) {
        this.setState({
          hex: hex
        });
      }
    }
  }]);
}(_react["default"].PureComponent);