/**
 * Browser Sync
 * @description Refresh the Browser after File Change.
 */

import kc from '../../config.json'
import gulp from 'gulp'
import browserSync from 'browser-sync'
import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import webpackSettings from '../../webpack.dev.config.babel'

const bundler = webpack(webpackSettings)

const browserSyncTask = () => {
  // Build a condition when Proxy is active
  let bsProxy
  let bsServer

  // Condition for Proxy
  if(kc.browsersync.proxy) {
    bsProxy = kc.browsersync.proxy
    bsServer = false
  } else {
    bsProxy = false
    bsServer = { baseDir : kc.dist.browserSyncDir }
  }

  // Browser Sync
  browserSync.init({
      debugInfo: true,
      watchTask: true,
      proxy: {
        target: bsProxy,
        ws: true,
        middleware: [
          webpackDevMiddleware(bundler, {
            publicPath: webpackSettings.output.publicPath,
            stats: { colors: true }
          }),
          webpackHotMiddleware(bundler)
        ]
      },

      ghostMode: {
        clicks: true,
        scroll: true,
        links: true,
        forms: true
      },

      logLevel: 'info',

      notify: {
        styles: [
          'padding: 8px 16px;',
          'position: fixed;',
          'font-size: 12px;',
          'font-weight: bold',
          'z-index: 9999;',
          'top: inherit',
          'border-radius: 0',
          'right: 0;',
          'bottom: 0;',
          'color: #f4f8f9;',
          'background-color: #026277;',
          'text-transform: uppercase'
        ]
      },

      server: bsServer,
      open: kc.browsersync.openbrowser,
      files: [
        kc.dist.js + '**/*.js',
        kc.dist.css + '**/*.css',<% if (projectUsage == 'Integrate in CraftCMS' ) { %>
    kc.dist.markup + 'templates/**/*.{php,html,twig}',<% } else if (projectUsage == 'Integrate in Wordpress') { %>
    kc.dist.markup + '**/*.{php,html,png,txt,md}',<% } else { %>
    kc.dist.base + '**/*.{php,html}',<% } %>
  kc.dist.cssimg + '**/*.{jpg,gif,png,svg}'

]
    });
};

gulp.task('browser-sync', browserSyncTask)
module.exports = browserSyncTask

@@ -599,265 +599,46 @@
 y = 
-kc.browsersync.proxy%0A    bsServer = false%0A  %7D else %7B%0A    bsProxy = false%0A    bsServer = %7B baseDir : kc.dist.browserSyncDir %7D%0A  %7D%0A%0A  // Browser Sync%0A  browserSync.init(%7B%0A      debugInfo: true,%0A      watchTask: true,%0A      proxy: %7B%0A        target: bsProxy,%0A  
+%7B%0A      target: kc.browsersync.proxy,%0A
     
@@ -655,18 +655,16 @@
 ,%0A      
-  
 middlewa
@@ -665,26 +665,24 @@
 ddleware: %5B%0A
-  
         webp
@@ -719,18 +719,16 @@
         
-  
 publicPa
@@ -766,18 +766,16 @@
 icPath,%0A
-  
         
@@ -812,16 +812,12 @@
     
-  
 %7D),%0A
-  
     
@@ -851,36 +851,247 @@
 ndler)%0A     
+ %5D%0A
    
-%5D
+ %7D
 %0A    
-  %7D
+bsServer = false%0A  %7D else %7B%0A    bsProxy = false%0A    bsServer = %7B baseDir : kc.dist.browserSyncDir %7D%0A  %7D%0A%0A  // Browser Sync%0A  browserSync.init(%7B%0A      debugInfo: true,%0A      watchTask: true,%0A      proxy: bsProxy
 ,%0A%0A      gho

Fix Browsersync without proxy