const gulp = require("gulp")
const concat = require('gulp-concat')
const watch = require('gulp-watch')
const embedTemplates = require('gulp-angular-embed-templates')
const concatCss = require('gulp-concat-css')
const del = require('del')
const runSequence = require('run-sequence')
const terser = require('gulp-terser')

const _distFolder = './dist'
const allFiles = './src/**/*'
const jsFiles = './src/**/*.js'
const cssFiles = './src/**/*.css'
const bundleJsName = 'table.min.js'
const bundleCssName = 'table.min.css'

const buildJs = () => {
    return gulp.src(jsFiles)
        .pipe(embedTemplates())
        .pipe(concat(bundleJsName))
        .pipe(terser())
        .pipe(gulp.dest(_distFolder))
}

const buildCss = () => {
    return gulp.src(cssFiles)
        .pipe(concatCss(bundleCssName))
        .pipe(gulp.dest(_distFolder))
}

/* Delete dist files */
gulp.task('clean', () => del(_distFolder + '/**/*', { force: true }))

gulp.task('build-css', buildCss)

gulp.task('build-js', buildJs)

gulp.task('watch-js', () => watch(allFiles, { ignoreInitial: false }, buildJs))

gulp.task('watch-css', () => watch(allFiles, { ignoreInitial: false }, buildCss))

gulp.task('watch', () => runSequence('clean', ['watch-css', 'watch-js']))

gulp.task('default', () => runSequence('clean', ['build-css', 'build-js']))