/* eslint-disable global-require */

/**
 * Front-end middleware
 */
const proxyConfig = require('../../config/proxy');
const fs = require('fs');
const path = require('path');
const apiMocker = require('connect-api-mocker');
module.exports = (app, options) => {
  const isProd = process.env.NODE_ENV === 'production';
  const { createProxyMiddleware } = require('http-proxy-middleware');

  if (isProd) {
    const addProdMiddlewares = require('./addProdMiddlewares');
    addProdMiddlewares(app, options);
  } else {
    const webpackConfig = require('../../internals/webpack/webpack.dev.babel');
    const addDevMiddlewares = require('./addDevMiddlewares');

    Object.keys(proxyConfig.dev).forEach(item => {
      app.use(createProxyMiddleware(item, proxyConfig.dev[item]));
    });

    addDevMiddlewares(app, webpackConfig);
  }

  return app;
};
