import _extends from 'babel-runtime/helpers/extends';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
var _jsxFileName = 'src/loadingBar/LoadingBar.jsx';
/**
 * Created by zhangce on 17/8/10.
 */
import React from 'react';
import ReactDOM from 'react-dom';
import { LinearProgress } from '../progress';
// import { setTimeout } from 'timers';

var DELAY = 200;
function render(props, rootNode) {
    var style = props.style,
        other = _objectWithoutProperties(props, ['style']);

    return ReactDOM.render(React.createElement(LinearProgress, Object.assign({ showInfo: false, strokeWidth: 4 }, other, {
        __source: {
            fileName: _jsxFileName,
            lineNumber: 12
        },
        __self: this
    })), rootNode);
}
export default function LoadingBar() {
    var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
        className: 'ne-loading-bar',
        duration: 2000
    };

    var rootNode = void 0;
    return {
        start: function start() {
            if (!rootNode) {
                rootNode = document.createElement('div');
                document.body.appendChild(rootNode);
            }
            render(_extends({}, props, { value: 0, duration: 0 }), rootNode);
            setTimeout(function () {
                return render(_extends({}, props, { value: 90 }), rootNode);
            }, DELAY);
        },
        update: function update(value) {
            if (!rootNode) {
                rootNode = document.createElement('div');
                document.body.appendChild(rootNode);
            }
            render(_extends({}, props, { value: value, duration: 200 }), rootNode);
        },
        finish: function finish() {
            if (!rootNode) {
                rootNode = document.createElement('div');
                document.body.appendChild(rootNode);
            }
            render(_extends({}, props, { value: 100, duration: DELAY }), rootNode);
            setTimeout(function () {
                var unmountResult = ReactDOM.unmountComponentAtNode(rootNode);
                if (unmountResult && rootNode.parentNode) {
                    rootNode.parentNode.removeChild(rootNode);
                }
                rootNode = null;
                if (props.onClose) {
                    props.onClose();
                }
            }, DELAY);
        }
    };
}