'use strict'

const {watch, src, dest, series, parallel} = require('gulp')
const path = require('path')
const concat = require('gulp-concat')
const rename = require('gulp-rename')
const sourcemaps = require('gulp-sourcemaps')
const sass = require('gulp-dart-sass')
const babel = require('gulp-babel')
const uglify = require('gulp-uglify')
const bro = require('gulp-bro')
//const htmlmin = require('gulp-htmlmin')
//const changed = require('gulp-changed')
//const image = require('gulp-image')

let source_dir = './resources/assets'
let output_dir = './public/assets'

function js(done) {
  return src(path.join(source_dir, 'js', '**', '*.js'))
    //.pipe(bro())
    .pipe(concat('main.js'))
    .pipe(dest(path.join(output_dir, 'scripts')))
    .pipe(babel({
			presets: ['@babel/preset-env']
		}))
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(uglify())
    .pipe(rename('main.min.js'))
    .pipe(sourcemaps.write('./maps'))
    .pipe(dest(path.join(output_dir, 'scripts')))
}

function css(done) {
  return src(path.join(source_dir, 'sass', 'main.scss'))
    .pipe(sass({includePaths: ['node_modules']}).on('error', sass.logError))
    .pipe(rename('main.css'))
    .pipe(dest(path.join(output_dir, 'styles')))
    .pipe(sourcemaps.init({loadMaps: true}))
    .pipe(sass({outputStyle: 'compressed'}))
    .pipe(sourcemaps.write('./maps'))
    .pipe(rename('main.min.css'))
    .pipe(dest(path.join(output_dir, 'styles')))
}

/* function html(done) {
  return src(path.join(source_dir, 'html', '**', '*.html'))
    .pipe(concat('index.html'))
    .pipe(htmlmin({collapseWhitespace: true}))
    .pipe(rename('index.html'))
    .pipe(dest(path.join(output_dir, '../')))
}*/

/*function images(done) {
  return src(path.join(source_dir, 'images', '*.*'))
    .pipe(changed(path.join(output_dir, 'images')))
    .pipe(image())
    .pipe(dest(path.join(output_dir, 'images')))
}*/

//exports.watch = watch(source_dir, parallel(js, css/*, html*/))

exports.default = parallel(js, css/*, html*/)
