const path = require('path')

const webpack = require('webpack')
const BabiliPlugin = require('babili-webpack-plugin')
const clone = require('lodash/cloneDeep')
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')

const PROD = process.env.NODE_ENV === 'production'

const plugins = [
  new webpack.optimize.OccurrenceOrderPlugin(),
  new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
  }),
  new LodashModuleReplacementPlugin({
    caching: true,
    cloning: true
  })
]

if (PROD) {
  plugins.push(
    new BabiliPlugin()
  )
  plugins.push(
    new webpack.LoaderOptionsPlugin({
      minimize: true,
      debug: false
    })
  )
}

const baseFileName = `contentful-management`

const baseBundleConfig = {
  context: path.join(__dirname, 'lib'),
  entry: ['./contentful-management.js'],
  output: {
    path: path.join(__dirname, 'dist'),
    libraryTarget: 'umd',
    library: 'contentfulManagement'
  },
  module: {
    loaders: []
  },
  devtool: PROD ? false : 'source-map',
  plugins,
  node: {
    os: 'empty'
  },
  // Show minimal information, but all errors and warnings
  // Except for log generation which have to contain all information
  stats: process.env.WEBPACK_MODE === 'log' ? 'detailed' : {
    assets: true,
    cached: false,
    children: false,
    chunks: true,
    chunkModules: true,
    colors: true,
    errors: true,
    errorDetails: true,
    exclude: [],
    hash: false,
    maxModules: 0,
    modules: false,
    moduleTrace: false,
    performance: true,
    providedExports: true,
    publicPath: false,
    reasons: false,
    source: false,
    timings: true,
    usedExports: false,
    version: true,
    warnings: true
  }
}

const defaultBabelLoader = {
  test: /\.js?$/,
  include: [
    path.resolve(__dirname, 'lib'),
    path.resolve(__dirname, 'test')
  ],
  loader: 'babel-loader',
  options: {}
}

// Browsers
const browserBundle = clone(baseBundleConfig)
browserBundle.module.loaders = [
  Object.assign({}, defaultBabelLoader, {
    options: Object.assign({}, defaultBabelLoader.options, {
      forceEnv: 'browser'
    })
  })
]
browserBundle.output.filename = `${baseFileName}.browser${PROD ? '.min' : ''}.js`

// Legacy browsers like IE11
const legacyBundle = clone(baseBundleConfig)
legacyBundle.module.loaders = [
  Object.assign({}, defaultBabelLoader, {
    options: Object.assign({}, defaultBabelLoader.options, {
      forceEnv: 'legacy'
    })
  })
]
// To be replaced with babel-polyfill with babel-preset-env 2.0:
// https://github.com/babel/babel-preset-env#usebuiltins
// https://github.com/babel/babel-preset-env/pull/241
legacyBundle.entry = [
  'core-js/fn/promise',
  'core-js/fn/object/assign',
  'core-js/fn/array/from'
].concat(legacyBundle.entry)

legacyBundle.output.filename = `${baseFileName}.legacy${PROD ? '.min' : ''}.js`

// Node
const nodeBundle = clone(baseBundleConfig)
nodeBundle.module.loaders = [
  Object.assign({}, defaultBabelLoader, {
    options: Object.assign({}, defaultBabelLoader.options, {
      forceEnv: 'node'
    })
  })
]
nodeBundle.target = 'node'
nodeBundle.output.libraryTarget = 'commonjs2'
nodeBundle.output.filename = `${baseFileName}.node${PROD ? '.min' : ''}.js`
delete nodeBundle.node

module.exports = [
  browserBundle,
  legacyBundle,
  nodeBundle
]

@@ -1258,475 +1258,27 @@
  ? '
-detailed' : %7B%0A    assets: true,%0A    cached: false,%0A    children: false,%0A    chunks: true,%0A    chunkModules: true,%0A    colors: true,%0A    errors: true,%0A    errorDetails: true,%0A    exclude: %5B%5D,%0A    hash: false,%0A    maxModules: 0,%0A    modules: false,%0A    moduleTrace: false,%0A    performance: true,%0A    providedExports: true,%0A    publicPath: false,%0A    reasons: false,%0A    source: false,%0A    timings: true,%0A    usedExports: false,%0A    version: true,%0A    warnings: true%0A  %7D
+verbose' : 'normal'
 %0A%7D%0A%0A

fix stats property to match webpack 3.8.0 schema