import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Styletron from 'styletron-client';
import { StyletronProvider } from 'styletron-react';
import { App } from './App';
import { documentReady } from './ready';

async function setup() {
    await documentReady();
    await renderToApp(
        <StyletronProvider styletron={new Styletron()}>
            <App />
        </StyletronProvider>,
    );
}

function renderToApp(component: JSX.Element) {
    return new Promise((resolve, reject) => {
        try {
            ReactDOM.render(component, document.querySelector('app'), () => {
                resolve();
            });
        } catch (ex) {
            reject(ex);
        }
    });
}

setup();
