import {Inject, Maybe, Request, Singleton, Unit, ViewEngine} from '@extollo/lib'
import {Cobalt} from './Cobalt.service'

@Singleton()
export class CobaltUnit extends Unit {
    @Inject()
    protected readonly viewEngine!: ViewEngine

    up(): Promise<void> | void {
        this.viewEngine.registerGlobalFactory('cobaltSession', (req: Maybe<Request>) => {
            if ( !req ) {
                return
            }

            const cobalt = req.make<Cobalt>(Cobalt)
            return cobalt.getSession()
        })

        this.viewEngine.registerGlobalFactory('cobaltState', (req: Maybe<Request>) => {
            if ( !req ) {
                return
            }

            const cobalt = req.make<Cobalt>(Cobalt)
            return cobalt.getState()
        })
    }
}
