'use strict'

const path = require('path')
const fs = require('fs')
const { DefinePlugin, ProvidePlugin } = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = function(production, app) {
  var entry = {
    app: [path.resolve(__dirname, 'expose-react.js'), path.resolve(__dirname, `../targets/${app}/web/main`)]
  }

  var plugins = [
    new DefinePlugin({
      __TARGET__: JSON.stringify('browser')
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, `../targets/${app}/web/index.ejs`),
      title: `Cozy ${app.charAt(0).toUpperCase()}${app.slice(1)}`,
      chunks: ['app'],
      inject: 'head',
      minify: {
        collapseWhitespace: true
      }
    }),
    new ProvidePlugin({
      PouchDB: 'pouchdb',
      pouchdbFind: 'pouchdb-find'
    })
  ]

  if (
    fs.existsSync(path.resolve(__dirname, `../targets/${app}/web/services.jsx`))
  ) {
    entry.services = [path.resolve(__dirname, 'expose-react.js'), path.resolve(__dirname, `../targets/${app}/web/services`)]
    plugins.push(
      new HtmlWebpackPlugin({
        template: path.resolve(__dirname, `../targets/${app}/web/services.ejs`),
        title: `Cozy ${app.charAt(0).toUpperCase()}${app.slice(1)}`,
        filename: 'services.html',
        chunks: ['services'],
        inject: 'head',
        minify: {
          collapseWhitespace: true
        }
      })
    )
  }

  if (
    fs.existsSync(
      path.resolve(__dirname, `../targets/${app}/web/public/main.jsx`)
    )
  ) {
    entry['public/app'] = [path.resolve(__dirname, 'expose-react.js'), path.resolve(
      __dirname,
      `../targets/${app}/web/public/main`
    )]
    plugins.push(
      new HtmlWebpackPlugin({
        template: path.resolve(
          __dirname,
          `../targets/${app}/web/public/index.ejs`
        ),
        title: `Cozy ${app.charAt(0).toUpperCase()}${app.slice(1)}`,
        filename: 'public/index.html',
        chunks: ['public/app'],
        inject: 'head',
        minify: {
          collapseWhitespace: true
        }
      })
    )
  }

  const devBrowserRules = [
    {
      test: require.resolve('cozy-bar/dist/cozy-bar.js'),
      loader: 'imports-loader?css=./cozy-bar.css'
    }
  ]

  return {
    entry: entry,
    module: { rules: production ? devBrowserRules : [] },
    output: {
      path: path.resolve(__dirname, `../build/${app}`),
      filename: '[name].js'
    },
    externals: {
      'cozy-client-js': 'cozy'
    },
    plugins: plugins
  }
}

@@ -249,16 +249,23 @@
   app: %5B
+%0A      
 path.res
@@ -291,32 +291,38 @@
 pose-react.js'),
+%0A     
  path.resolve(__
@@ -359,16 +359,21 @@
 b/main%60)
+%0A    
 %5D%0A  %7D%0A%0A 
@@ -953,24 +953,31 @@
 services = %5B
+%0A      
 path.resolve
@@ -999,32 +999,38 @@
 pose-react.js'),
+%0A     
  path.resolve(__
@@ -1071,16 +1071,21 @@
 rvices%60)
+%0A    
 %5D%0A    pl
@@ -1584,16 +1584,23 @@
 pp'%5D = %5B
+%0A      
 path.res
@@ -1634,16 +1634,22 @@
 ct.js'),
+%0A     
  path.re
@@ -1654,23 +1654,16 @@
 resolve(
-%0A      
 __dirnam
@@ -1664,22 +1664,16 @@
 dirname,
-%0A     
  %60../tar
@@ -1700,22 +1700,22 @@
 ic/main%60
+)
 %0A    
-)
 %5D%0A    pl
@@ -2333,16 +2333,21 @@
 uction ?
+ %5B%5D :
  devBrow
@@ -2358,13 +2358,8 @@
 ules
- : %5B%5D
  %7D,%0A

Load dev config only for dev