<commit_before>import DocumentTitle from 'react-document-title'
import Html from './html'
import Promise from 'bluebird'
import React from 'react'
import Router from 'react-router'
import config from './config'
import initialState from './initialstate'
import routes from '../client/routes'
import {state} from '../client/state'

export default function render(req, res, locale) {
  const path = req.path
  return loadData(path, locale)
    .then((appState) => renderPage(res, appState, path))
}

function loadData(path, locale) {
  // TODO: Preload and merge user specific state.
  const appState = initialState
  return new Promise((resolve, reject) => {
    resolve(appState)
  })
}

// TODO: Refactor.
function renderPage(res, appState, path) {
  return new Promise((resolve, reject) => {
    const router = Router.create({
      routes,
      location: path,
      onError: reject,
      onAbort: (abortReason) => {
        if (abortReason.constructor.name === 'Redirect') {
          const {to, params, query} = abortReason
          const path = router.makePath(to, params, query)
          res.redirect(path)
          resolve()
          return
        }
        reject(abortReason)
      }
    })
    router.run((Handler, routerState) => {
      state.load(appState)
      const html = getPageHtml(Handler, appState)
      const notFound = routerState.routes.some(route => route.name === 'not-found')
      const status = notFound ? 404 : 200
      res.status(status).send(html)
      resolve()
    })
  })
}

function getPageHtml(Handler, appState) {
  const appHtml = `<div id="app">${React.renderToString(<Handler />)}</div>`
  const appScriptSrc = config.isProduction
    ? '/build/app.js?v=' + config.version
    : '//localhost:8888/build/app.js'

  let scriptHtml = `
    <script>
      (function() {
        window._appState = ${JSON.stringify(appState)};
        var app = document.createElement('script'); app.type = 'text/javascript'; app.async = true;
        var src = '${appScriptSrc}';
        // IE<11 and Safari need Intl polyfill.
        if (!window.Intl) src = src.replace('.js', 'intl.js');
        app.src = src;
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(app, s);
      })();
    </script>`

  if (config.googleAnalyticsId !== 'UA-XXXXXXX-X')
    scriptHtml += `
      <script>
        (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
        function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
        e=o.createElement(i);r=o.getElementsByTagName(i)[0];
        e.src='//www.google-analytics.com/analytics.js';
        r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
        ga('create','${config.googleAnalyticsId}');ga('send','pageview');
      </script>`

  const title = DocumentTitle.rewind()

  return '<!DOCTYPE html>' + React.renderToStaticMarkup(
    <Html<commit_msg>only load google analytics in production<commit_after>import DocumentTitle from 'react-document-title'
import Html from './html'
import Promise from 'bluebird'
import React from 'react'
import Router from 'react-router'
import config from './config'
import initialState from './initialstate'
import routes from '../client/routes'
import {state} from '../client/state'

export default function render(req, res, locale) {
  const path = req.path
  return loadData(path, locale)
    .then((appState) => renderPage(res, appState, path))
}

function loadData(path, locale) {
  // TODO: Preload and merge user specific state.
  const appState = initialState
  return new Promise((resolve, reject) => {
    resolve(appState)
  })
}

// TODO: Refactor.
function renderPage(res, appState, path) {
  return new Promise((resolve, reject) => {
    const router = Router.create({
      routes,
      location: path,
      onError: reject,
      onAbort: (abortReason) => {
        if (abortReason.constructor.name === 'Redirect') {
          const {to, params, query} = abortReason
          const path = router.makePath(to, params, query)
          res.redirect(path)
          resolve()
          return
        }
        reject(abortReason)
      }
    })
    router.run((Handler, routerState) => {
      state.load(appState)
      const html = getPageHtml(Handler, appState)
      const notFound = routerState.routes.some(route => route.name === 'not-found')
      const status = notFound ? 404 : 200
      res.status(status).send(html)
      resolve()
    })
  })
}

function getPageHtml(Handler, appState) {
  const appHtml = `<div id="app">${React.renderToString(<Handler />)}</div>`
  const appScriptSrc = config.isProduction
    ? '/build/app.js?v=' + config.version
    : '//localhost:8888/build/app.js'

  let scriptHtml = `
    <script>
      (function() {
        window._appState = ${JSON.stringify(appState)};
        var app = document.createElement('script'); app.type = 'text/javascript'; app.async = true;
        var src = '${appScriptSrc}';
        // IE<11 and Safari need Intl polyfill.
        if (!window.Intl) src = src.replace('.js', 'intl.js');
        app.src = src;
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(app, s);
      })();
    </script>`

  if (config.isProduction && config.googleAnalyticsId !== 'UA-XXXXXXX-X')
    scriptHtml += `
      <script>
        (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
        function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
        e=o.createElement(i);r=o.getElementsByTagName(i)[0];
        e.src='//www.google-analytics.com/analytics.js';
        r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
        ga('create','${config.googleAnalyticsId}');ga('send','pageview');
      </script>`

  const title = DocumentTitle.rewind()

  return '<!DOCTYPE html>' + React.renderToStaticMarkup(
    <Html
{'fraction_non_alphanumeric': 0.1591997264021888, 'fraction_numerical': 0.0047879616963064295, 'mean_word_length': 3.1452870304748406, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7279958', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1828, 'n_words': 397}