const { series, parallel, watch } = require('gulp');
const bs = require('browser-sync');

const config = require('./config');

const clean = require('./tasks/clean');
const sprite = require('./tasks/sprite');
const scss = require('./tasks/scss');
const html = require('./tasks/html');
const images = require('./tasks/images');
const assets = require('./tasks/assets');
const report = require('./tasks/report');
const webpack = require('./tasks/webpack');
const browserSync = require('./tasks/browserSync');

const tasks = [scss, html, images, assets];

function setBuild(cb) {
  global.env = 'build';
  cb();
}

function setProd(cb) {
  global.env = 'prod';
  cb();
}

function reload(cb) {
  bs.reload();
  cb();
}

function watchFiles(cb) {
  global.env = 'watch';

  watch(config.sprite.src, series(sprite, reload));
  watch(config.scss.src, scss);
  watch(config.images.src, series(images, reload));
  watch(config.html.src, series(html, reload));
  cb();
}

const _default = series(clean, setBuild, sprite, parallel(...tasks, webpack));
const _prod = series(
  clean,
  setProd,
  sprite,
  parallel(...tasks, webpack),
  report
);
const _watch = series(
  clean,
  watchFiles,
  sprite,
  parallel(...tasks),
  browserSync
);

module.exports = {
  default: _default,
  production: _prod,
  watch: _watch,
};

@@ -654,32 +654,91 @@
 od';%0A  cb();%0A%7D%0A%0A
+function setWatch(cb) %7B%0A  global.env = 'watch';%0A  cb();%0A%7D%0A%0A
 function reload(
@@ -799,33 +799,8 @@
 ) %7B%0A
-  global.env = 'watch';%0A%0A
   wa
@@ -1198,26 +1198,24 @@
 lean,%0A  
-watchFiles
+setWatch
 ,%0A  spri
@@ -1240,16 +1240,30 @@
 tasks),%0A
+  watchFiles,%0A
   browse

reorder watch task to prevent double task calling