// @flow
import type { ChooMiddleware } from "./app";

const xhr = require("xhr");
const config = require("./config");

type APICall = (body: Object, cb: Function) => any;
const apiCall: APICall = (body, cb) =>
    xhr(
        {
            uri: config.api.url,
            method: "post",
            headers: {
                "Content-Type": "application/json"
            },
            json: true,
            body
        },
        cb
    );

const apiReducers: ChooMiddleware = (state, emitter) => {
    emitter.on("api:room", () => {
        const query = `
        {
            room {
                apiKey
                sessionId
                token
            }
        }`;
        return apiCall({query}, (err, resp, body) => {
            if (err) {
                emitter.emit('log:error', err)
                return state;
            }
            state.room = body.data.room
            emitter.emit("render")
        })
    }),
    emitter.on("api:signup", user => {
        const query = `
        mutation ($user: UserInput){
            createUser(user: $user) {
                id
                name
                email
            }
        }`;
        const variables = { user };
        emitter.emit("log:info", user);
        return apiCall({ query, variables }, (err, resp, body) => console.log(body));
    });
};

module.exports = {
    apiReducers
};
@@ -111,16 +111,53 @@
 onfig%22);
+%0Aconst opentok = require(%22./opentok%22)
 %0A%0Atype A
@@ -976,16 +976,198 @@
         
+    emitter.emit(%22opentok:initialize%22, state.room)%0A        %7D)%0A    %7D),%0A    emitter.on(%22opentok:initialize%22, (%7BapiKey, sessionId, token%7D) =%3E %7B%0A        opentok(%7BapiKey, sessionId, token
 %7D)%0A    %7D

add event to trigger opentok