import React from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import LoadingBar from './loading-bar';
import raf from 'raf';
var rafId = null;
var loadingBar = null;
var outerDiv = null;
var width = 0;
var reset = function () {
    if (outerDiv) {
        unmountComponentAtNode(outerDiv);
        document.body.removeChild(outerDiv);
    }
    loadingBar = null;
    outerDiv = null;
    width = 0;
};
var move = function () {
    if (width < 55) {
        width += .4;
        rafId = raf(move);
    }
    else if (width < 90) {
        width += .1;
        rafId = raf(move);
    }
    loadingBar && (loadingBar.style.width = width + "%");
};
var createComponent = function () {
    outerDiv = document.createElement('div');
    document.body.appendChild(outerDiv);
    var component = React.createElement(LoadingBar, {
        didMount: function () {
            loadingBar = document.getElementById('ty-loading-bar');
            rafId = raf(move);
        },
    });
    render(component, outerDiv);
};
var unmountDom = function () {
    setTimeout(function () {
        loadingBar && (loadingBar.style.opacity = '0');
    }, 300);
    setTimeout(function () {
        reset();
    }, 700);
};
var start = function () {
    if (!outerDiv) {
        reset();
        createComponent();
    }
};
var succeed = function () {
    rafId && raf.cancel(rafId);
    if (outerDiv && loadingBar) {
        loadingBar.style.width = '100%';
        unmountDom();
    }
};
var fail = function () {
    rafId && raf.cancel(rafId);
    if (outerDiv && loadingBar) {
        loadingBar.style.width = '100%';
        loadingBar.style.backgroundColor = '#f30';
        unmountDom();
    }
};
export default {
    start: start,
    succeed: succeed,
    fail: fail,
};
//# sourceMappingURL=index.js.map