/* eslint-disable */
require('events').EventEmitter.defaultMaxListeners = 50;
const path = require('path');
const publicPath = process.env.VUE_APP_PROJECT_PATH || '';
// const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const { ContextReplacementPlugin } = require('webpack');
const ModuleFederationForWebpack4Plugin = require("mf-webpack4");

process.on('uncaughtException', (e) => {
  console.log(e.message);
});

module.exports = {
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.plugin('html').tap((args) => {
      args[0].title = 'Единый реестр воинского учёта';
      return args;
    });
    config.module
      .rule('i18n')
      .resourceQuery(/blockType=i18n/)
      .type('javascript/auto')
      .use('i18n')
      .loader('@intlify/vue-i18n-loader')
      .end();
    return config;
  },

  pwa: {
    iconPaths: {
      favicon32: 'favicon.ico',
      favicon16: 'favicon.ico',
      appleTouchIcon: 'favicon.ico',
      maskIcon: 'favicon.ico',
      msTileImage: 'favicon.ico',
    },
  },

  lintOnSave: false,
  devServer: {
    overlay: {
      warnings: false,
      errors: false,
    },  
    https: true,
    host: process.env.VUE_APP_HOST,
    disableHostCheck: true, // devServer ругается 'Invalid Host Header' если через Nginx с докером конфигурацию мутить
    proxy: {
      // '/mfe/drafts': {
      //   target: 'http://mfe-drafts:8081',
      //   pathRewrite: {'^/mfe/drafts' : ''},
      // },
      // TODO: от этого мока нужно избавляться
      '/epby-mock': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-ui-action-service': {
        target: `https://${process.env.API_BASE_URL}`,
        //ws: true,
        changeOrigin: true,
      },
      '/service/ervu-import-service': {
        target: `https://${process.env.API_BASE_URL}`,
        //ws: true,
        changeOrigin: true,
      },
      '/service/stat-report-scheduler': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-person-registry': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-summon-list-registry': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/personal-template-prefill-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/sso': {
        target: process.env.VUE_APP_SSO_AUTH,
        changeOrigin: true,
      },
      '/service/ervu-data-update-controller': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-subpoena-registry': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-journal-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-nsi-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-doc-merge-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-decision-document-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-appeal-document-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-extract-from-registry-provider': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-incidents-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-mz-monitor-service': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/service/ervu-person-draft': {
        target: `https://${process.env.API_BASE_URL}`,
        changeOrigin: true,
      },
      '/nexus-transfer': {
        target: `https://${process.env.API_BASE_URL}/nexus-transfer`,
        pathRewrite: { '/nexus-transfer': '' },
        changeOrigin: true
      },
      '/service/cnsi/service': {
        target: `https://${process.env.API_BASE_URL}/service/cnsi/service`,
        pathRewrite: { '/service/cnsi/service': '' },
        changeOrigin: true,
      },
      '/service/registry/data/document': {
        target: `https://${process.env.API_BASE_URL}/service/registry/data/document`,
        pathRewrite: { '/service/registry/data/document': '' },
        changeOrigin: true,
      },
      '/service/registry/data/exp/compensation': {
        target: `https://${process.env.API_BASE_URL}/service/registry/data/exp/compensation`,
        pathRewrite: { '/service/registry/data/exp/compensation': '' },
        changeOrigin: true,
        logLevel: 'debug'
      },
      '/api': {
        target: `https://${process.env.VUE_APP_API_GATEWAY}`,
        pathRewrite: { '/api': '' },
        changeOrigin: true,
      },
      '/stat-chart-component': {
        target: `https://${process.env.API_BASE_URL}/stat-chart-component`,
        pathRewrite: { '/stat-chart-component': '' },
        changeOrigin: true,
      },
      '/stat-query-rest': {
        target: `https://${process.env.API_BASE_URL}/stat-query-rest`,
        pathRewrite: { '/stat-query-rest': '' },
        changeOrigin: true,
      },
      '/service/idm': {
        target: `https://${process.env.API_BASE_URL}/service/idm`,
        pathRewrite: { '/service/idm': '' },
        changeOrigin: true,
      },
      '/service/wf/service': {
        target: `https://${process.env.API_BASE_URL}/service/wf/service`,
        pathRewrite: { '/service/wf/service': '' },
        changeOrigin: true,
      },
      '/service/notifier/registry': {
        target: `https://${process.env.API_BASE_URL}/service/notifier/registry`,
        pathRewrite: { '/service/notifier/registry': '' },
        changeOrigin: true,
      },
      '/service/notifier/gateway': {
        target: `wss://${process.env.API_BASE_URL}/service/notifier/gateway`,
        pathRewrite: { '/service/notifier/gateway': '' },
        ws: true,
        changeOrigin: true,
      },
      '/service/address': {
        target: `https://${process.env.API_BASE_URL}/service/address`,
        pathRewrite: { '/service/address': '' },
        changeOrigin: true,
      },
      '/service/fias': {
        target: `https://${process.env.API_BASE_URL}/service/fias`,
        pathRewrite: { '/service/fias': '' },
        changeOrigin: true,
      },
      '/service/search': {
        target: `https://${process.env.API_BASE_URL}/service/search`,
        pathRewrite: { '/service/search': '' },
        changeOrigin: true,
      },
      '/service/document-data': {
        target: `https://${process.env.API_BASE_URL}/service/document-data`,
        pathRewrite: { '/service/document-data': '' },
        changeOrigin: true,
      },
      '/service/document/registry': {
        target: `https://${process.env.API_BASE_URL}/service/document/registry`,
        pathRewrite: { '/service/document/registry': '' },
        changeOrigin: true,
      },
      '/service/registry/data': {
        target: `https://${process.env.VUE_APP_REGISTRY_DATA_SERVICE}/service/registry/data`,
        pathRewrite: { '/service/registry/data': '' },
        changeOrigin: true,
      },
      '/service/report': {
        target: `https://${process.env.API_BASE_URL}/service/report`,
        pathRewrite: { '/service/report': '' },
        changeOrigin: true,
      },
      '/mz/history/petitions': {
        target: `https://${process.env.API_PGS_BASE_URL}/mz/history/petitions`,
        pathRewrite: { '/mz/history/petitions': '' },
        changeOrigin: true,
      },
      '/mz/history': {
        target: `https://${process.env.API_BASE_URL}/mz/history`,
        pathRewrite: { '/mz/history': '' },
        changeOrigin: true,
      },
      '/mz/inner-petitions': {
        target: `https://${process.env.API_PGS_BASE_URL}/mz/inner-petitions`,
        pathRewrite: { '/mz/inner-petitions': '' },
        changeOrigin: true,
      },
      '/mz/payment': {
        target: `https://${process.env.API_BASE_URL}/mz/payment`,
        pathRewrite: { '/mz/payment': '' },
        changeOrigin: true,
      },
      '/mz/resend-service': {
        target: `https://${process.env.API_PGS_BASE_URL}/mz/resend-service`,
        pathRewrite: { '/mz/resend-service': '' },
        changeOrigin: true,
      },
      '/service/mz-discovery': {
        target: `https://${process.env.API_BASE_URL}/service/mz-discovery`,
        pathRewrite: { '/service/mz-discovery': '' },
        changeOrigin: true,
      },
      '/mz/gateway': {
        target: `${process.env.VUE_APP_MZ_SERVICE}/mz/gateway`,
        pathRewrite: { '/mz/gateway': '' },
        changeOrigin: true,
      },
      '/service/s3urlsigner': {
        target: `https://${process.env.API_BASE_URL}/service/s3urlsigner`,
        pathRewrite: { '/service/s3urlsigner': '' },
        changeOrigin: true,
      },
      '/service/registry/service': {
        target: `https://${process.env.API_BASE_URL}/service/registry/service`,
        pathRewrite: { '/service/registry/service': '' },
        changeOrigin: true,
      },
      '/service/dmn': {
        target: `https://${process.env.API_BASE_URL}/service/dmn`,
        pathRewrite: { '/service/dmn': '' },
        changeOrigin: true,
      },
      '/service/dictionary/registry/nsi': {
        target: `https://${process.env.API_BASE_URL}/service/dictionary/registry/nsi`,
        pathRewrite: { '/service/dictionary/registry/nsi': '' },
        changeOrigin: true,
      },
      '/service/printer': {
        target: `https://${process.env.API_BASE_URL}/service/printer`,
        pathRewrite: { '/service/printer': '' },
        changeOrigin: true,
      },
      '/service/stpbroker/gateway': {
        target: `https://${process.env.API_BASE_URL}/service/stpbroker/gateway`,
        pathRewrite: { '/service/stpbroker/gateway': '' },
        changeOrigin: true,
      },
      '/service/importer/registry': {
        target: `https://${process.env.API_BASE_URL}/service/importer/registry`,
        pathRewrite: { '/service/importer/registry': '' },
        changeOrigin: true,
      },
      '/service/personal-account-service': {
        target: `https://${process.env.API_BASE_URL}/service/personal-account-service`,
        pathRewrite: { '/service/personal-account-service': '' },
        changeOrigin: true,
      },
      '/service/reference-information': {
        target: `https://${process.env.API_BASE_URL}/service/reference-information`,
        pathRewrite: { '/service/reference-information': '' },
        changeOrigin: true,
      },
      '/kcr-core/container': {
        target: `${process.env.VUE_APP_KCR_SERVICE}kcr-core/container`,
        pathRewrite: { '/kcr-core/container': '' },
        changeOrigin: true,
      },
      '/service/prepare-statement-print-template': {
        target: `http://localhost:3000/`,
        changeOrigin: true,
      },
      '/service/preorder/service': {
        target: `https://${process.env.API_BASE_URL}/service/preorder/service`,
        pathRewrite: { '/service/preorder/service': '' },
        changeOrigin: true,
        followRedirects: true,
      },
      '/service/constructor-mz-mock-adapter/': {
        target: `https://${process.env.API_BASE_URL}/service/constructor-mz-mock-adapter/`,
        pathRewrite: { '/service/constructor-mz-mock-adapter/': '' },
        changeOrigin: true,
      },
      '/service/constructor-database': {
        target: `https://${process.env.API_BASE_URL}/service/constructor-database`,
        pathRewrite: { '/service/constructor-database': '' },
        changeOrigin: true,
      },
      '/service/document-converter': {
        target: `https://${process.env.API_BASE_URL}/service/document-converter`,
        pathRewrite: { '/service/document-converter': '' },
        changeOrigin: true,
      },
      '/service/nsi-registry': {
        target: `https://${process.env.API_BASE_URL}/service/nsi-registry`,
        pathRewrite: { '/service/nsi-registry': '' },
        changeOrigin: true,
      },
      '/service/refugee-compensation': {
        target: `https://${process.env.API_BASE_URL}/service/refugee-compensation`,
        pathRewrite: { '/service/refugee-compensation': '' },
        changeOrigin: true,
      },
      '/service/cwf/service': {
        target: `https://${process.env.API_BASE_URL}/service/cwf/service`,
        pathRewrite: { '/service/cwf/service': '' },
        changeOrigin: true,
      },
      'service/constructor-bpmn-generation-service': {
        target: `https://${process.env.API_BASE_URL}/service/constructor-bpmn-generation-service`,
        pathRewrite: { 'service/constructor-bpmn-generation-service': '' },
        changeOrigin: true,
      },
    },
  },

  runtimeCompiler: false,

  pluginOptions: {
    quasar: {
      importStrategy: 'kebab',
      rtlSupport: false,
    },
  },

  transpileDependencies: ['quasar'],

  configureWebpack: {
    resolve: {
      alias: {
        quasar: path.resolve(__dirname, 'node_modules/quasar'),
        'vue-property-decorator': path.resolve(
          __dirname,
          'node_modules/vue-property-decorator'
        ),
        'portal-vue': path.resolve(__dirname, 'node_modules/portal-vue'),
        '@': path.resolve(__dirname, 'src/'),
        '@components': path.resolve(__dirname, 'src/components/'),
        '@layout': path.resolve(__dirname, 'src/components/common/layout/'),
        '@openapi': path.resolve(__dirname, 'src/openapi/'),
        '@plugins': path.resolve(__dirname, 'src/plugins/'),
        '@router': path.resolve(__dirname, 'src/router/'),
        '@services': path.resolve(__dirname, 'src/services/'),
        '@state': path.resolve(__dirname, 'src/state/'),
        '@views': path.resolve(__dirname, 'src/router/views/'),
        '@utils': path.resolve(__dirname, 'src/utils/'),
        '@interfaces': path.resolve(__dirname, 'src/interfaces/'),
        '@models': path.resolve(__dirname, 'src/models/'),
        '@mixins': path.resolve(__dirname, 'src/mixins/'),
        '@constants': path.resolve(__dirname, 'src/constants/'),
      },
      symlinks: false,
    },
    plugins: [
      new (require('circular-dependency-plugin'))({ exclude: /node_modules/ }),
      new ContextReplacementPlugin(/moment[/\\]locale$/, /ru|en/),
      // new BundleAnalyzerPlugin(),
      // new ModuleFederationForWebpack4Plugin({
      //   name: 'microws',
      //   remotes: {
      //     drafts: "drafts@/mfe/drafts/remoteEntry.js",
      //   },
      // }),
    ],
  },
  publicPath,

  // отключить паралельную сборку потому что иначе не собирается с mf-webpack4 плагином
  parallel: ((process.env.NODE_ENV === 'production') ? false : undefined),
};
