import _Balloon from "@alifd/next/es/balloon";
import _Input from "@alifd/next/es/input";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
import { SketchPicker } from 'react-color';
import './index.less';
var ColorSetter = /*#__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.inputChange = function (color) {
      var _this$props = _this.props,
        onStyleChange = _this$props.onStyleChange,
        styleKey = _this$props.styleKey;
      if (color == '') {
        onStyleChange([{
          styleKey: styleKey,
          value: null
        }]);
      }
    };
    _this.handleChange = function (color) {
      var _this$props2 = _this.props,
        onStyleChange = _this$props2.onStyleChange,
        styleKey = _this$props2.styleKey;
      var rgb = color.rgb,
        hex = color.hex;
      var r = rgb.r,
        g = rgb.g,
        b = rgb.b,
        a = rgb.a;
      if (a === 1) {
        onStyleChange([{
          styleKey: styleKey,
          value: hex
        }]);
      } else {
        onStyleChange([{
          styleKey: styleKey,
          value: "rgba(" + r + "," + g + "," + b + "," + a + ")"
        }]);
      }
    };
    _this.state = {
      width: 50
    };
    return _this;
  }
  _inheritsLoose(ColorSetter, _React$Component);
  var _proto = ColorSetter.prototype;
  _proto.componentDidMount = function componentDidMount() {
    this.screenChange();
    this.changeWidth();
    // const { onChange, value, defaultValue } = this.props;
    // if (value == undefined && defaultValue) {
    //   onChange(defaultValue);
    // }
  };
  _proto.componentWillUnmount = function componentWillUnmount() {
    window.removeEventListener('resize', this.changeWidth);
  };
  _proto.render = function render() {
    var _this$props3 = this.props,
      styleKey = _this$props3.styleKey,
      styleData = _this$props3.styleData,
      _this$props3$inputWid = _this$props3.inputWidth,
      inputWidth = _this$props3$inputWid === void 0 ? '108px' : _this$props3$inputWid,
      color = _this$props3.color;
    var InputTarget = /*#__PURE__*/React.createElement(_Input, {
      className: "lowcode-setter-color",
      style: {
        width: inputWidth
      },
      hasClear: true,
      innerBefore: /*#__PURE__*/React.createElement("div", {
        className: "color-box",
        style: {
          backgroundColor: color ? color : styleData[styleKey]
        }
      }),
      onChange: this.inputChange,
      value: color ? color : styleData[styleKey]
    });
    return /*#__PURE__*/React.createElement(_Balloon, {
      needAdjust: true,
      align: "tr",
      offset: [-30, 0],
      style: {
        padding: 0
      },
      trigger: InputTarget,
      triggerType: "click",
      closable: false
    }, /*#__PURE__*/React.createElement(SketchPicker, {
      width: 250,
      color: color ? color : styleData[styleKey],
      onChange: this.handleChange
    }));
  };
  return ColorSetter;
}(React.Component);
export { ColorSetter as default };