"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _balloon = _interopRequireDefault(require("@alifd/next/lib/balloon"));
var _input = _interopRequireDefault(require("@alifd/next/lib/input"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var React = _interopRequireWildcard(require("react"));
var _reactColor = require("react-color");
require("./index.less");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var ColorSetter = exports["default"] = /*#__PURE__*/function (_React$Component) {
  function ColorSetter(props) {
    var _this;
    _this = _React$Component.call(this, props) || this;
    /**
     * 屏幕分辨率监听
     */
    _this.screenChange = function () {
      window.addEventListener('resize', _this.changeWidth);
    };
    /**
     * 屏幕分辨率 变换 =>  改变冒泡框的位置
     */
    _this.changeWidth = function () {
      _this.setState({
        width: document.body.clientWidth < 1860 ? -92 : -138
      });
    };
    _this.handleChange = function (color) {
      var onChange = _this.props.onChange;
      var rgb = color.rgb,
        hex = color.hex;
      var r = rgb.r,
        g = rgb.g,
        b = rgb.b,
        a = rgb.a;
      if (a === 1) {
        onChange(hex);
      } else {
        onChange("rgba(" + r + "," + g + "," + b + "," + a + ")");
      }
    };
    _this.state = {
      width: -92,
      setterValue: null
    };
    return _this;
  }
  (0, _inheritsLoose2["default"])(ColorSetter, _React$Component);
  var _proto = ColorSetter.prototype;
  _proto.componentDidMount = function componentDidMount() {
    this.screenChange();
    this.changeWidth();
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    window.removeEventListener('resize', this.changeWidth);
  };
  ColorSetter.getDerivedStateFromProps = function getDerivedStateFromProps(nextProps, prevState) {
    var value = nextProps.value,
      defaultValue = nextProps.defaultValue;
    if (prevState.setterValue == null) {
      if (value == undefined && defaultValue) {
        return {
          setterValue: defaultValue
        };
      }
    }
    return {
      setterValue: value
    };
  };
  _proto.render = function render() {
    var _this$state = this.state,
      width = _this$state.width,
      setterValue = _this$state.setterValue;
    var onChange = this.props.onChange;
    var InputTarget = /*#__PURE__*/React.createElement(_input["default"], {
      size: "small",
      className: "lowcode-setter-color",
      style: {
        width: '100%'
      },
      innerBefore: /*#__PURE__*/React.createElement("div", {
        className: "color-box",
        style: {
          backgroundColor: setterValue
        }
      }),
      value: setterValue,
      onChange: onChange
    });
    return /*#__PURE__*/React.createElement(_balloon["default"], {
      needAdjust: true,
      align: "tr",
      offset: [width, 0],
      style: {
        padding: 0
      },
      trigger: InputTarget,
      triggerType: "click",
      closable: false
    }, /*#__PURE__*/React.createElement(_reactColor.SketchPicker, {
      color: setterValue,
      onChange: this.handleChange
    }));
  };
  return ColorSetter;
}(React.Component);
ColorSetter.displayName = 'ColorSetter';