import Janus from 'janus-gateway';

const HOST = 'localhost';

class JanusInstance {

    private readonly janus = Janus;
    constructor(private readonly debug: boolean) {}

    public initiate():Promise<Janus>{
        return new Promise<Janus>((resolve: (value: Janus) => void) => {
            this.janus.init({
                debug: this.debug,
                dependencies: this.janus.useDefaultDependencies(),
                callback: async() => {
                    resolve(await this.createSession());
                }
             });
        });
    }

    private createSession(){
        return new Promise<Janus>((resolve: (value: Janus) => void, reject: (reason: string) => void) => {
            const session = new this.janus({
                server: [`ws://${HOST}:8188/`, `http://${HOST}:8088/janus`],
                success: () => resolve(session),
                error: (err: string) => reject(err),
            });
        });
    }

};

export default JanusInstance;