(function (appConfig) {

  'use strict'

  // *** main dependencies *** //
  //const cookieParser = require('cookie-parser')
  const bodyParser = require('body-parser')
  const morgan = require('morgan')
  const helmet = require('helmet')
  const path = require('path')


  // *** load environment variables *** //
  require('dotenv').config()

  appConfig.init = function (app, express) {
    // *** app middleware *** //
    app.use(helmet())
    if (process.env.NODE_ENV !== 'test') {
      app.use(morgan('dev'))
    }
    //app.use(cookieParser())
    app.use(bodyParser.json())
    app.use(bodyParser.urlencoded({ extended: false }))
    // // uncomment if using express-session
    // app.use(session({
    //   secret: process.env.SECRET_KEY,
    //   resave: false,
    //   saveUninitialized: true
    // }));
    //app.use(flash())
    if (process.env.NODE_ENV !== 'production') {
      app.disable('etag')
    }
    if (process.env.NODE_ENV === 'development') {
      app.use(express.static(path.join(__dirname, '..', '..', 'assets')))

      const webpack = require('webpack')
      const webpackConfig = require('../../webpack.config')
      const webpackDevMiddleware = require('webpack-dev-middleware')
      const webpackHotMiddleware = require('webpack-hot-middleware')

      let compiler = webpack(webpackConfig)
      app.use(webpackDevMiddleware(compiler, {
        noInfo: true,
        publicPath: webpackConfig.output.publicPath,
        watchOptions: { // used to enable polling, needed in vagrant for watcher
          aggregateTimeout: 300,
          poll: 1000
        },
        historyApiFallback: true
      }))
      app.use(webpackHotMiddleware(compiler))
    }


  }

})(module.exports)
