'use strict';

import path from 'path';
import gulp from 'gulp';
import del from 'del';
import runSequence from 'run-sequence';
import gulpLoadPlugins from 'gulp-load-plugins';
import pkg from './package.json';

const sourceDir = 'src';
const distDir = 'dist';

var $ = gulpLoadPlugins();


gulp.task('styles', () => {
  const AUTOPREFIXER_BROWSERS = [
    'ie >= 9',
    'ie_mob >= 10',
    'ff >= 30',
    'chrome >= 34',
    'safari >= 7',
    'opera >= 23',
    'ios >= 7',
    'android >= 4.4',
    'bb >= 10',
    '> 0.01%'
  ];

  return gulp.src([
    sourceDir + '/styles/main.scss'
  ])
    // .pipe($.sourcemaps.init())
    .pipe($.sass({
      precision: 10
    }).on('error', $.sass.logError))
    .pipe($.autoprefixer(AUTOPREFIXER_BROWSERS))
    .pipe($.if('*.css', $.cssnano()))
    .pipe($.size({title: 'styles'}))
    // .pipe($.sourcemaps.write('./'))
    .pipe(gulp.dest(distDir + '/styles'))
});

gulp.task('lint', () =>
  gulp.src([sourceDir + '/scripts/**/*.js'])
    .pipe($.eslint())
    .pipe($.eslint.format())
    .pipe($.eslint.failAfterError())
);
gulp.task('scripts', ['lint'], () =>
  gulp.src([
    sourceDir + '/scripts/main.js'
  ])
    .pipe($.plumber())
    // .pipe($.sourcemaps.init())
    // .pipe($.babel({presets: ['es2015']}))
    .pipe($.concat('main.min.js'))
    .pipe($.uglify())
    .pipe($.size({title: 'scripts'}))
    // .pipe($.sourcemaps.write('.'))
    .pipe(gulp.dest(distDir + '/scripts'))
);

// Clean output directory
gulp.task('clean', () => del([
    distDir
  ],
  {dot: true}
));


gulp.task('serve', function () {
  gulp.watch(sourceDir + '/styles/**/*.scss', ['styles']);
  gulp.watch(sourceDir + '/scripts/**/*.js', ['scripts']);
});

// Build production files, the default task
gulp.task('default', ['clean'], cb =>
  runSequence(
    'styles',
    'scripts',
    cb
  )
);
