import _extends from "@babel/runtime/helpers/esm/extends";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * Created by zhangce on 17/8/10.
 */
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { LinearProgress } from '../progress';

var ProgressBar =
/*#__PURE__*/
function (_Component) {
  _inherits(ProgressBar, _Component);

  function ProgressBar() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ProgressBar);

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

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

    _defineProperty(_assertThisInitialized(_this), "state", {
      value: 0
    });

    _defineProperty(_assertThisInitialized(_this), "update", function (value) {
      _this.setState({
        value: value
      });
    });

    return _this;
  }

  _createClass(ProgressBar, [{
    key: "render",
    value: function render() {
      var value = this.state.value;
      return React.createElement(LinearProgress, _extends({}, this.props, {
        value: value,
        showInfo: false
      }));
    }
  }]);

  return ProgressBar;
}(Component);

var defaultProps = {
  className: 'ne-loading-bar',
  duration: 2000,
  strokeWidth: 4
};
var loadingBar = {
  initial: function initial() {
    var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultProps;
    var callback = arguments.length > 1 ? arguments[1] : undefined;
    var root = document.body;
    var containerElement = document.createElement('div'); // 不在DOM中创建节点

    root.appendChild(containerElement);
    var mounted = false;

    function getBar(bar) {
      if (mounted) {
        return;
      }

      mounted = true;
      callback && callback({
        element: bar,
        destroy: function destroy() {
          ReactDOM.unmountComponentAtNode(containerElement);
          containerElement.parentNode.removeChild(containerElement);
        }
      });
    }

    ReactDOM.render(React.createElement(ProgressBar, _extends({}, props, {
      ref: getBar
    })), containerElement);
  },
  start: function start() {
    var _this2 = this;

    if (this.bar) {
      this.bar.element.update(0);
    } else {
      this.initial(defaultProps, function (bar) {
        _this2.bar = bar;
      });
    }
  },
  config: function config(newConfig) {
    Object.assign(defaultProps, newConfig);
    this.destroy();
    this.start();
  },
  destroy: function destroy() {
    if (this.bar) {
      this.bar.destroy();
      delete this.bar;
    }
  },
  update: function update(value) {
    var _this3 = this;

    if (this.bar) {
      this.bar.element.update(value);
    } else {
      this.initial(defaultProps, function (bar) {
        _this3.bar = bar;
      });
      this.bar.element.update(value);
    }
  },
  finish: function finish() {
    var _this4 = this;

    if (this.bar) {
      this.bar.element.update(100);
    } else {
      this.initial(defaultProps, function (bar) {
        _this4.bar = bar;

        _this4.bar.element.update(100);

        setTimeout(function () {
          return _this4.destroy();
        }, 2000);
      });
    } // loadingBar.destroy();

  }
};
export default loadingBar;