const ENABLE_THIRDPARTY_APPS = process.env.VUE_APP_ENABLE_THIRDPARTY_APPS || 'false'
const ENABLE_THIRDPARTY_APPS_BY_DZYNE = process.env.VUE_APP_ENABLE_THIRDPARTY_APPS_BY_DZYNE || 'false'
const ENABLE_THIRDPARTY_APPS_ONLY = process.env.VUE_APP_ENABLE_THIRDPARTY_APPS_ONLY || 'false'
const ENABLE_EXTERNAL_TREE_VIEWER = process.env.VUE_APP_ENABLE_EXTERNAL_PLACEMENT_TREE_VIEWER || 'false'
const ENABLE_PAYMENT_APP = process.env.VUE_APP_ENABLE_PAYMENT_APP || 'false'
const ENABLE_ELKEN_APP_CENTER = process.env.VUE_APP_ENABLE_ELKEN_APP_CENTER || 'false'
const IS_EVO = process.env.VUE_APP_APPLICATION_NAME === 'EVO'

module.exports = {
  getPages: function() {
    let pages = {}
    if (ENABLE_THIRDPARTY_APPS_ONLY !== 'true') {
      pages.app = {
        entry: 'src/main.js',
        template: 'public/index.html',
        filename: 'index.html',
        title: `Elken ${(IS_EVO && 'EVO') || 'IBS'} Member Portal`,
        chunks: ['chunk-app-vendors', 'app'],
      }
    }
    if (ENABLE_THIRDPARTY_APPS === 'true') {
      if (ENABLE_THIRDPARTY_APPS_BY_DZYNE === 'true') {
        pages['checkout'] = {
          entry: 'src/thirdparty/eshop/checkout.js',
          template: 'public/checkout.html',
          filename: 'app1/index.html',
          title: 'Elken Checkout Page',
          chunks: ['chunk-checkout-vendors', 'checkout'],
        }
        pages['product-list'] = {
          entry: 'src/thirdparty/eshop/product-list.js',
          template: 'public/product-list.html',
          filename: 'app2/index.html',
          title: 'Elken Product List',
          chunks: ['chunk-product-list-vendors', 'product-list'],
        }
        pages['login'] = {
          entry: 'src/thirdparty/eshop/login.js',
          template: 'public/login.html',
          filename: 'app3/index.html',
          title: 'Elken Login Page',
          chunks: ['chunk-login-vendors', 'login'],
        }
        pages['product-viewer'] = {
          entry: 'src/thirdparty/eshop/product-viewer.js',
          template: 'public/product-viewer.html',
          filename: 'app5/index.html',
          title: 'Elken Product Previewer',
          chunks: ['chunk-product-viewer-vendors', 'product-viewer'],
        }
      }
      if (ENABLE_EXTERNAL_TREE_VIEWER === 'true') {
        pages['placement-tree'] = {
          entry: 'src/thirdparty/placement-tree-viewer/main.js',
          template: 'public/placement-tree-viewer.html',
          filename: 'treeapp/index.html',
          title: 'Placement Tree Viewer',
          chunks: ['chunk-placement-tree-vendors', 'placement-tree'],
        }
      }
      if (ENABLE_PAYMENT_APP === 'true') {
        pages['payment'] = {
          entry: 'src/thirdparty/payment/main.js',
          template: 'public/payment.html',
          filename: 'app4/index.html',
          title: 'Elken Payment Page',
          chunks: ['chunk-payment-vendors', 'payment'],
        }
      }
      if (ENABLE_ELKEN_APP_CENTER === 'true') {
        pages['elken-app-center'] = {
          entry: 'src/thirdparty/elken-app-center/main.js', // entry point of your application
          template: 'public/elken-app-center.html', // template file path
          filename: 'elken-app-center/bundle.html', // output path for your index.html // TODO: change done as per hassan request
          title: 'Elken App Center', // the title of your application
          chunks: ['chunk-elken-app-center-vendors', 'elken-app-center'], // js chunks name. note: chunks name must follow your page name
        }
      }
    }

    return pages
  },
  getPagesSplitChunkCacheGroupsOptimizations: function() {
    const IS_VENDOR = /[\\/]node_modules[\\/]/

    return {
      app: {
        name: `chunk-app-vendors`,
        priority: -11,
        chunks: chunk => chunk.name === 'app',
        test: IS_VENDOR,
        enforce: true,
      },
      checkout: {
        name: `chunk-checkout-vendors`,
        priority: -11,
        chunks: chunk => chunk.name === 'checkout',
        test: IS_VENDOR,
        enforce: true,
      },
      'product-list': {
        name: `chunk-product-list-vendors`,
        priority: -11,
        chunks: chunk => chunk.name === 'product-list',
        test: IS_VENDOR,
        enforce: true,
      },
      login: {
        name: `chunk-login-vendors`,
        priority: -11,
        chunks: chunk => chunk.name === 'login',
        test: IS_VENDOR,
        enforce: true,
      },
      'placement-tree': {
        name: `chunk-placement-tree-vendors`,
        priority: -11,
        chunks: chunk => chunk.name === 'placement-tree',
        test: IS_VENDOR,
        enforce: true,
      },
      payment: {
        name: `chunk-payment-vendors`,
        priority: -11,
        chunks: chunks => chunks.name === 'payment',
        test: IS_VENDOR,
        enforce: true,
      },
      'elken-app-center': {
        name: `chunk-elken-app-center-vendors`,
        priority: -11,
        chunks: chunks => chunks.name === 'elken-app-center',
        test: IS_VENDOR,
        enforce: true,
      },
      'product-viewer': {
        name: `chunk-product-viewer-vendors`,
        priority: -11,
        chunks: chunks => chunks.name === 'product-viewer',
        test: IS_VENDOR,
        enforce: true,
      },
    }
  },
}
