import fse from 'fs-extra';
import path from 'path';
import gulp from "gulp";
import ts from "gulp-typescript";
//import * as dartSass from 'sass';
//import gulpSass from 'gulp-sass';
//import webpack from 'webpack';
//import webpackStream from 'webpack-stream';
import { deleteAsync } from 'del';

//const sass = gulpSass(dartSass);

var tsProject = ts.createProject("tsconfig.json");

// Task which would delete the old dist directory if present
gulp.task("build-clean", function () {
    return deleteAsync(["./dist"]);
});

// Task which would transpile typescript to javascript
gulp.task("typescript", function () {
    return tsProject.src().pipe(tsProject()).js.pipe(gulp.dest("dist"));
});

/*let webpackConfig = {
    mode: 'development', //(PRODUCTION ? 'production' : 'development'),
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [ "@babel/preset-env" ],
                        compact: false
                    }
                }
            }
        ]
    }//,
    //devtool: !PRODUCTION && 'source-map'
}*/

// Task which will copy the assets from the static JavaScript directory to the dist directory
/*gulp.task("compile-foundation-js", function () {
    return gulp.src('./src/static/js/foundation/** /*.js').pipe(webpackStream(webpackConfig, webpack)).pipe(gulp.dest("./dist/static/js/foundation"));
});*/

// Task to compile the sass files to css files
/*gulp.task("sass", function () {
    let includePaths = [];
    
    if(fse.existsSync('node_modules/foundation-sites/scss') && fse.existsSync('node_modules/motion-ui/src')) {
        includePaths = [
            'node_modules/foundation-sites/scss', 
            'node_modules/motion-ui/src'
        ];
    }
    else if(fse.existsSync('.yarn/cache')) {
        // We're using Yarn PnP, so we need to find the paths to the foundation-sites and motion-ui packages
        const packages = fse.readdirSync('.yarn/cache');
        
        const foundationSitesYarnPnPPath = path.join('.yarn/cache', packages.find(p => p.startsWith('foundation-sites-npm-') && p.endsWith('.zip')), 'node_modules/foundation-sites/scss');
        const motionUIYarnPnPPath = path.join('.yarn/cache', packages.find(p => p.startsWith('motion-ui-npm-') && p.endsWith('.zip')), 'node_modules/motion-ui/src');
        
        includePaths = [
            foundationSitesYarnPnPPath,
            motionUIYarnPnPPath
        ];
    }
    
    return gulp.src("./src/static/scss/** /*.scss").pipe(sass({ includePaths: includePaths })).pipe(gulp.dest("./dist/static/css"));
});*/

//gulp.task('pre-compile', gulp.parallel('compile-foundation-js', 'sass'));

// Task which would just create a copy of the current views directory in dist directory
gulp.task("views", function () {
    return gulp.src("./src/pages/**/*.ejs").pipe(gulp.dest("./dist/pages"));
});

// Task which will copy the assets from the static JavaScript directory to the dist directory
gulp.task("assets-js", function () {
    return gulp.src(['./src/static/js/**/*.js', '!./src/static/js/foundation/**/*.js']).pipe(gulp.dest("./dist/static/js"));
});

// Task which will copy the assets from the static image directory to the dist directory
gulp.task("assets-img", function () {
    return gulp.src("./src/static/img/**/*", { encoding: false }).pipe(gulp.dest("./dist/static/img"));
});

// Task which will copy the assets from the static CSS directory to the dist directory
gulp.task("assets-css", function () {
    return gulp.src("./src/static/css/*").pipe(gulp.dest("./dist/static/css"));
});

gulp.task("assets", gulp.parallel("assets-js", "assets-img", "assets-css"));

// The default task which runs at start of the gulpfile.js
gulp.task("default", gulp.series("build-clean", "typescript", /*"pre-compile",*/ "views", "assets"), () => {
    console.log("Done");
});