import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import CircularProgress from '@material-ui/core/CircularProgress'; // Timeout [ms] 間 resolve がなければ、次の resolve まで hide

var Timeout = 1000;

var ResolveProgress =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(ResolveProgress, _PureComponent);

  function ResolveProgress() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ResolveProgress);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ResolveProgress)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      visible: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_timer", null);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleResolve", function () {
      if (!_this.state.visible) {
        _this.setState({
          visible: true
        });
      }

      clearInterval(_this._timer);
      _this._timer = setTimeout(function () {
        _this.setState({
          visible: false
        });
      }, Timeout);
    });

    return _this;
  }

  _createClass(ResolveProgress, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var globalEvent = this.props.globalEvent;
      globalEvent.on('message.resolve', this.handleResolve);
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.state.visible) {
        return null;
      }

      return React.createElement(CircularProgress, {
        size: this.props.size,
        thickness: Math.max(1, this.props.size / 8),
        color: "primary"
      });
    }
  }]);

  return ResolveProgress;
}(PureComponent);

_defineProperty(ResolveProgress, "propTypes", {
  size: PropTypes.number.isRequired,
  globalEvent: PropTypes.object.isRequired
});

export { ResolveProgress as default };