const logger = require('../../config/logger')
const config = require('../../config')

let webpackManifest = {}
try {
  webpackManifest = require(`${config.buildDir}/manifest.json`)
} catch (err) {
  logger.error('Manifest file is not found. Ensure assets are built.')
}

const globalNavItems = [
  { path: '/companies', label: 'Companies' },
  { path: '/contacts', label: 'Contacts' },
  { path: '/interactions', label: 'Interactions' },
  { path: '/investment-projects', label: 'Investment projects' },
  { path: '/omis', label: 'OMIS Orders' },
]

module.exports = function locals (req, res, next) {
  const baseUrl = `${(req.encrypted ? 'https' : req.protocol)}://${req.get('host')}`
  const breadcrumbItems = res.breadcrumb()

  res.locals = Object.assign({}, res.locals, {
    BASE_URL: baseUrl,
    CANONICAL_URL: baseUrl + req.originalUrl,
    CURRENT_PATH: req.path,
    GOOGLE_TAG_MANAGER_KEY: config.googleTagManagerKey,
    BREADCRUMBS: breadcrumbItems,
    IS_XHR: req.xhr,
    QUERY: req.query,
    GLOBAL_NAV_ITEMS: globalNavItems.map(globalNavItem => {
      const url = globalNavItem.path

      return Object.assign(globalNavItem, {
        url,
        isActive: req.path.startsWith(url),
      })
    }),

    getMessages () {
      return req.flash()
    },

    getPageTitle () {
      const items = breadcrumbItems.map(item => item.name)
      const title = res.locals.title

      if (title) {
        if (items.length === 1) {
          return [title]
        }

        items.pop()
        items.push(title)
      }

      return items.reverse().slice(0, -1)
    },

    getAssetPath (asset) {
      const assetsUrl = config.assetsHost || baseUrl
      const webpackAssetPath = webpackManifest[asset]

      if (webpackAssetPath) {
        return `${assetsUrl}/${webpackAssetPath}`
      }

      return `${assetsUrl}/${asset}`
    },

    getLocal (key) {
      return res.locals[key]
    },

    translate (key) {
      return req.translate(key)
    },
  })
  next()
}

@@ -371,32 +371,72 @@
 : 'Contacts' %7D,%0A
+  %7B path: '/events', label: 'Events' %7D,%0A
   %7B path: '/inte

add events link to global nav