"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
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 = _interopRequireWildcard(require("react"));
var _Focusable = require("../common/Focusable");
var _wixUiIconsCommon = require("@wix/wix-ui-icons-common");
var _color = _interopRequireDefault(require("color"));
var _ColorPickerConverterSt = require("./ColorPickerConverter.st.css.js");
var _Tooltip = _interopRequireDefault(require("../Tooltip"));
var _constants = require("./constants");
var _IconThemeContext = require("../WixDesignSystemIconThemeProvider/IconThemeContext");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/ColorPicker/ColorPickerConverterViewer.jsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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; })(); }
function getContrastColor(bg) {
  var light = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '#ffffff';
  var dark = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '#162d3d';
  try {
    var color = (0, _color["default"])(bg);
    var luminosity = color.luminosity();
    if (luminosity > 0.5) {
      return dark;
    } else {
      return light;
    }
  } catch (err) {}
}
var ColorPickerConverterViewer = /*#__PURE__*/function (_PureComponent) {
  function ColorPickerConverterViewer() {
    var _this;
    (0, _classCallCheck2["default"])(this, ColorPickerConverterViewer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ColorPickerConverterViewer, [].concat(args));
    _this.onAddClick = function () {
      var _this$props = _this.props,
        color = _this$props.color,
        onAdd = _this$props.onAdd;
      var noColorSelected = color.alpha() === 0;
      !!onAdd && !noColorSelected && onAdd(color.hex());
    };
    _this.addTooltip = function (element) {
      var addTooltipContent = _this.props.addTooltipContent;
      return /*#__PURE__*/_react["default"].createElement(_Tooltip["default"], {
        disabled: !addTooltipContent,
        content: addTooltipContent,
        size: "small",
        __self: _this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 36,
          columnNumber: 7
        }
      }, element);
    };
    return _this;
  }
  (0, _inherits2["default"])(ColorPickerConverterViewer, _PureComponent);
  return (0, _createClass2["default"])(ColorPickerConverterViewer, [{
    key: "render",
    value: function render() {
      var _this2 = this;
      var _this$props2 = this.props,
        color = _this$props2.color,
        onAdd = _this$props2.onAdd,
        focusableOnFocus = _this$props2.focusableOnFocus,
        focusableOnBlur = _this$props2.focusableOnBlur,
        className = _this$props2.className;
      var noColorSelected = color.alpha() === 0;
      var clickable = !!onAdd && !noColorSelected;
      return /*#__PURE__*/_react["default"].createElement(_IconThemeContext.IconThemeContext.Consumer, {
        __self: this,
        __source: {
          fileName: _jsxFileName,
          lineNumber: 53,
          columnNumber: 7
        }
      }, function (_ref) {
        var _icons$ColorPickerCon;
        var _ref$icons = _ref.icons,
          icons = _ref$icons === void 0 ? {} : _ref$icons;
        var AddSmallIcon = ((_icons$ColorPickerCon = icons.ColorPickerConverterViewer) == null ? void 0 : _icons$ColorPickerCon.AddSmall) || _wixUiIconsCommon.AddSmall;
        var viewer = /*#__PURE__*/_react["default"].createElement(clickable ? 'button' : 'div', {
          style: {
            backgroundColor: noColorSelected ? undefined : color.hex()
          },
          'data-hook': _constants.DataHooks.addColor,
          onFocus: focusableOnFocus,
          onBlur: focusableOnBlur,
          className: (0, _ColorPickerConverterSt.st)(_ColorPickerConverterSt.classes.preview, {
            clickable: clickable,
            noColorSelected: noColorSelected
          }, className),
          onClick: _this2.onAddClick
        }, clickable && /*#__PURE__*/_react["default"].createElement(AddSmallIcon, {
          style: {
            color: getContrastColor(color.hex())
          },
          __self: _this2,
          __source: {
            fileName: _jsxFileName,
            lineNumber: 78,
            columnNumber: 15
          }
        }));
        return clickable ? _this2.addTooltip(viewer) : viewer;
      });
    }
  }]);
}(_react.PureComponent);
var _default = exports["default"] = (0, _Focusable.withFocusable)(ColorPickerConverterViewer);