import path from 'path';
import { match } from 'react-router';
import PrettyError from 'pretty-error';
import createMemoryHistory from 'react-router/lib/createMemoryHistory';

import createStore from '../shared/create';
import configure from '../configure';
import html from './html';
import getTools from './tools';

global.__CLIENT__ = false;
global.__SERVER__ = true;
global.__DISABLE_SSR__ = false;  // <----- DISABLES SERVER SIDE RENDERING FOR ERROR DEBUGGING
global.__DEVELOPMENT__ = process.env.NODE_ENV !== 'production';

export default (projectConfig, projectToolsConfig) => {
  const tools = getTools(projectConfig, projectToolsConfig);
  const config = configure(projectConfig);
  const getRoutes = require(path.resolve(config.routes)).default;
  const rootComponent = require(config.rootComponent ? path.resolve(config.rootComponent) : '../helpers/rootComponent');
  const pretty = new PrettyError();


  const dynamicMiddleware = (originalUrl, headers, send, redirect) => {
    if (__DEVELOPMENT__) {
      // Do not cache webpack stats: the script file would change since
      // hot module replacement is enabled in the development env
      tools.refresh();
    }

    const middleware = config.redux.middleware ? require(path.resolve(config.redux.middleware)).default : [];
    const history = createMemoryHistory();
    const store = createStore(middleware, history);

    if (__DISABLE_SSR__) {
      const content = html(config, tools.assets(), store, headers);
      send(200, content);
    } else {
      return match({ history, routes: getRoutes(store), location: originalUrl }, (error, redirectLocation, renderProps) => {
        if (redirectLocation) {
          redirect(redirectLocation.pathname + redirectLocation.search);
        } else if (error) {
          console.error('ROUTER ERROR:', pretty.render(error));
          send(500);
        } else if (renderProps) {
          return rootComponent.createForServer(store, renderProps)
            .then(({ root }) => {
              const content = html(config, tools.assets(), store, headers, root);
              send(200, content);
            })
            .catch((err) => {
              console.log('ERROR GENERATING ROOT COMPONENT', err, err.stack);
              send(500, err);
            });
        } else {
          send(400, 'Not found');
        }
      });
    }
  };

  function *koaMiddleware() {
    yield dynamicMiddleware(this.request.originalUrl,
      this.request.headers,
      (status, body) => {
        this.status = status;
        this.body = body;
      },
      (url) => this.response.redirect(url));
  }

  switch (config.server.webFramework) {
    case 'koa': {
      return koaMiddleware;
    }
    default:
    case 'express': {
      return (req, res) => {
        dynamicMiddleware(req.originalUrl,
          req._headers,
          (status, body) => res.status(status).send(body),
          (url) => res.redirect(url));
      };
    }
  }
};

@@ -1521,23 +1521,16 @@
 %7B%0A      
-return 
 match(%7B 
@@ -1625,32 +1625,32 @@
 nderProps) =%3E %7B%0A
+
         if (redi
@@ -1895,23 +1895,16 @@
         
-return 
 rootComp
@@ -2385,14 +2385,8 @@
     
-yield 
 dyna
@@ -2805,17 +2805,17 @@
       re
-q
+s
 ._header

Revert "added promise resolving to handle the async callback in match"