'use strict';

const cache = require('gulp-cached');
const filter = require('gulp-filter');
const less = require('gulp-less');
const rename = require('gulp-rename');
const gutil = require('gulp-util');

// Only copy LESS from FE src to target, and webapp. This will eventually be replaced with the LESS transpile,
// keeping it separate now makes that easier later
module.exports = function (gulp, paths) {
    return function() {
        return gulp.src(paths.FRONTEND_SRC + paths.LESS_GLOB)
            .pipe(cache('less'))
            .pipe(less({
                paths: [paths.FRONTEND_BASE]
            }))
            .on('error', function (err) {
                gutil.log(gutil.colors.bgRed('LESS Error') + ' ' + gutil.colors.red(err.message));
                gutil.log(gutil.colors.yellow('Context:\n') + gutil.colors.cyan(err.extract.join('\n')));
                this.emit('end');
            })
            .pipe(filter(function(file) {
                // don't copy empty files, eg: variables.less
                return file.stat && file.contents.length;
            }))
            .pipe(rename({
                extname: '.less.css'
            }))
            .pipe(gulp.dest(paths.FRONTEND_TARGET))

    }
};