// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
/*
 * decaffeinate suggestions:
 * DS103: Rewrite code to no longer use __guard__
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
const i18n = require('i18next')
const _ = require('underscore')
const path = require('path')

module.exports = {
  setup(options) {
    const subdomainLang =
      (options != null ? options.subdomainLang : undefined) || {}
    const availableLngs = _.pluck(_.values(subdomainLang), 'lngCode')
    i18n.init({
      resGetPath: path.resolve(__dirname, '../../', 'locales/__lng__.json'),
      saveMissing: true,
      resSetPath: path.resolve(
        __dirname,
        '../../',
        'locales/missing-__lng__.json'
      ),
      sendMissingTo: 'fallback',
      fallbackLng: (options != null ? options.defaultLng : undefined) || 'en',
      detectLngFromHeaders: true,
      useCookie: false,
      preload: availableLngs,
      supportedLngs: availableLngs
    })
    const setLangBasedOnDomainMiddlewear = function(req, res, next) {
      const { host } = req.headers
      if (host == null) {
        return next()
      }
      const parts = host.split(/[.-]/)
      const subdomain = parts[0]
      const lang = __guard__(
        __guard__(
          options != null ? options.subdomainLang : undefined,
          x1 => x1[subdomain]
        ),
        x => x.lngCode
      )
      if (req.originalUrl.indexOf('setLng') === -1 && lang != null) {
        req.i18n.setLng(lang)
      }
      if (req.language !== req.lng) {
        req.showUserOtherLng = req.language
      }
      next()
    }

    return {
      expressMiddlewear: i18n.handle,
      setLangBasedOnDomainMiddlewear,
      i18n
    }
  }
}

function __guard__(value, transform) {
  return typeof value !== 'undefined' && value !== null
    ? transform(value)
    : undefined
}

@@ -138,58 +138,8 @@
 ns:%0A
- * DS103: Rewrite code to no longer use __guard__%0A
  * D
@@ -1290,59 +1290,35 @@
 %5B0%5D%0A
+%0A
       
-cons
+le
 t lang
- = __guard__(%0A        __guard__(%0A  
+%0A      if (%0A
     
@@ -1325,33 +1325,34 @@
     options 
-!= null ?
+&&%0A       
  options.sub
@@ -1366,39 +1366,40 @@
 ang 
-: undefined,%0A          x1 =%3E x1
+&&%0A        options.subdomainLang
 %5Bsub
@@ -1408,28 +1408,27 @@
 main%5D%0A      
-  ),
+) %7B
 %0A        x =
@@ -1428,14 +1428,47 @@
     
-x =%3E x
+lang = options.subdomainLang%5Bsubdomain%5D
 .lng
@@ -1478,17 +1478,18 @@
 e%0A      
-)
+%7D%0A
 %0A      i
@@ -1815,141 +1815,4 @@
 %7D%0A%7D%0A
-%0Afunction __guard__(value, transform) %7B%0A  return typeof value !== 'undefined' && value !== null%0A    ? transform(value)%0A    : undefined%0A%7D%0A

Decaf clean up: replace __guard__ function