import renderToString from 'preact-render-to-string'
// import { App } from './App'
import { ServerAsyncCallbacksContext, DatabaseContext, MetadataContext, ServerContext } from './hooks'

import { RenderedPage, ServerAsyncCallback } from '../shared/ssr'
import { App } from './App'
import { DatabaseConnection } from '../shared/database'

export default (url: string, db: DatabaseConnection): RenderedPage => {
    const metadata = {}
    const asyncCallbacks: ServerAsyncCallback[] = []

    const html = renderToString(
        <MetadataContext.Provider value={metadata}>
            <ServerContext.Provider value={true}>
                <DatabaseContext.Provider value={db}>
                    <ServerAsyncCallbacksContext.Provider value={asyncCallbacks}>
                        <App url={url} />
                    </ServerAsyncCallbacksContext.Provider>
                </DatabaseContext.Provider>
            </ServerContext.Provider>
        </MetadataContext.Provider>
    )

    return { html, metadata, asyncCallbacks }
}
