"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 _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _lodash = require("lodash");

var echarts = _interopRequireWildcard(require("echarts"));

var _title = _interopRequireDefault(require("../title"));

var _context = _interopRequireDefault(require("../context"));

var _Utils = require("../Utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * echarts图表
 *
 * @class EChart
 * @extends {PureComponent<IProps>}
 */
var EChart = /*#__PURE__*/function (_PureComponent) {
  (0, _inheritsLoose2["default"])(EChart, _PureComponent);

  function EChart(props) {
    var _this;

    _this = _PureComponent.call(this, props) || this;
    _this.containerRef = void 0;
    _this.titleRef = void 0;
    _this.chartRef = void 0;
    _this.chart = void 0;

    _this.handlerResize = function () {
      if (_this.chart) {
        _this.chart.resize();
      }
    };

    _this.containerRef = /*#__PURE__*/_react["default"].createRef();
    _this.titleRef = /*#__PURE__*/_react["default"].createRef();
    _this.chartRef = /*#__PURE__*/_react["default"].createRef();
    return _this;
  }

  var _proto = EChart.prototype;

  _proto.componentDidMount = function componentDidMount() {
    var _this$props = this.props,
        loading = _this$props.loading,
        options = _this$props.options,
        onInit = _this$props.onInit;
    var containerDom = this.containerRef.current;
    var titleDom = this.titleRef.current;
    window.addEventListener('resize', this.handlerResize);
    this.chart = echarts.init(this.chartRef.current, null, {
      height: titleDom ? containerDom.clientHeight - (0, _Utils.Dimension)(titleDom) : containerDom.clientHeight
    });

    if (loading) {
      this.chart.showLoading('default', {
        maskColor: 'transparent'
      });
    }

    this.chart.setOption((0, _extends2["default"])({
      color: _Utils.DATA_COLORS
    }, options));
    onInit(this.chart);
  };

  _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
    var _this$props2 = this.props,
        loading = _this$props2.loading,
        options = _this$props2.options;

    if (!(0, _lodash.isEqual)(prevProps.loading, loading)) {
      if (loading) {
        this.chart.showLoading();
      } else {
        this.chart.hideLoading();
      }
    }

    if (!(0, _lodash.isEqual)(prevProps.options, options)) {
      this.chart.setOption(options);
    }
  };

  _proto.componentWillUnmount = function componentWillUnmount() {
    window.removeEventListener('resize', this.handlerResize);
  };

  _proto.render = function render() {
    var _cssPrefix = this.context._cssPrefix;
    var _this$props3 = this.props,
        className = _this$props3.className,
        title = _this$props3.title,
        titleOptions = _this$props3.titleOptions,
        universalStyle = _this$props3.universalStyle;
    return /*#__PURE__*/_react["default"].createElement("div", {
      ref: this.containerRef,
      className: (0, _classnames["default"])(className, _cssPrefix + "chart-wrapper"),
      style: {
        width: (0, _lodash.get)(universalStyle, 'width'),
        height: (0, _lodash.get)(universalStyle, 'height')
      }
    }, title &&
    /*#__PURE__*/
    // @ts-ignore
    _react["default"].createElement(_title["default"], (0, _extends2["default"])({}, titleOptions, {
      forwardRef: this.titleRef,
      className: (0, _classnames["default"])(_cssPrefix + "chart-title", _cssPrefix + "main-chart-title", (0, _lodash.get)(titleOptions, 'className'))
    }), title), /*#__PURE__*/_react["default"].createElement("div", {
      ref: this.chartRef,
      className: _cssPrefix + "chart-content"
    }));
  };

  return EChart;
}(_react.PureComponent); // @ts-ignore


EChart.defaultProps = {
  className: null,
  loading: false,
  options: {},
  onInit: function onInit() {},
  titleOptions: {
    align: 'left',
    textStyle: {
      marginBottom: 8
    }
  }
};
EChart.contextType = _context["default"];
var _default = EChart;
exports["default"] = _default;