import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import * as React from 'react';
// @ts-ignore
import { SketchPicker } from 'react-color';
import './index.less';
var _Next = Next,
  Input = _Next.Input,
  Balloon = _Next.Balloon;
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.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;
  }
  _inheritsLoose(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, {
      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, {
      needAdjust: true,
      align: "tr",
      offset: [width, 0],
      style: {
        padding: 0
      },
      trigger: InputTarget,
      triggerType: "click",
      closable: false
    }, /*#__PURE__*/React.createElement(SketchPicker, {
      color: setterValue,
      onChange: this.handleChange
    }));
  };
  return ColorSetter;
}(React.Component);
ColorSetter.displayName = 'ColorSetter';
export { ColorSetter as default };