'use strict';

/**
 * Load required Gulp plugins.
 */
const autoprefixer = require('autoprefixer');
const concat = require('gulp-concat');
const filter = require('gulp-filter');
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const sass = require('gulp-sass')(require('node-sass'));
const sourcemaps = require('gulp-sourcemaps');

/**
 * Define paths and filenames.
 */
let browserSupport = 'last 1 version',
    publicPath = 'assets/',
    fileName = 'app',
    paths = {
        scss: [
            publicPath + 'sass/**/*.scss',
            publicPath + 'sass/' + fileName + '.scss',
            publicPath + 'sass/print.scss'

        ],
        adminScss: [
            publicPath + 'sass/' + fileName + '-admin.scss'
        ],
        js: [
            './node_modules/bootstrap/dist/js/bootstrap.js',
            publicPath + 'javascript/**/*.js',
            publicPath + 'javascript/' + fileName + '.js',
            '!' + publicPath + 'javascript/' + fileName + '.min.js',
        ]
    };

/**
 * Front end CSS.
 */
function css() {

    return gulp.src(paths.scss)
        .pipe(filter(['**']))
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sass({
            importer: resolveNodeModulesImporter,
            outputStyle: 'expanded'
        })).on('error', sass.logError)
        .pipe(postcss([
            autoprefixer({
                overrideBrowserslist: [browserSupport]
            }),
        ]))
        .pipe(sourcemaps.write())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(publicPath + 'sass'));
}

/**
 * Back end CSS.
 */
function cssAdmin() {
    return gulp.src(paths.adminScss)
        .pipe(filter(['**']))
        .pipe(plumber())
        .pipe(sourcemaps.init())
        .pipe(sass({
            outputStyle: 'expanded'
        })).on('error', sass.logError)
        .pipe(postcss([
            autoprefixer({overrideBrowserslist: [browserSupport]}),
        ]))
        .pipe(sourcemaps.write())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(publicPath + 'sass'));
}

/**
 * Front end scripts.
 */
function scripts() {
    return gulp.src(paths.js)
        .pipe(plumber())
        .pipe(concat(fileName + '.min.js'))
        .pipe(gulp.dest(publicPath + 'javascript'));
}

/**
 * Watch files.
 */
function watchFiles() {
    gulp.watch(paths.scss, gulp.series(css));
    gulp.watch(paths.adminScss, gulp.series(cssAdmin));
    gulp.watch(paths.js, gulp.series(scripts));
}

function resolveAssetPath(file) {
    return (file[ 0 ] === '~') ?
        `node_modules/${file.substr(1)}`
        : `${file}`;
}

function resolveNodeModulesImporter(file) {
    return (file[ 0 ] === '~') ? {
        file: `node_modules/${file.substr(1)}`
    } : file;
}


/**
 * Arguments when running Gulp.
 */
exports.build   = gulp.series(gulp.parallel(css, cssAdmin, scripts));
exports.default = gulp.series(gulp.parallel(css, cssAdmin, scripts, watchFiles));