const path = require("path");

module.exports = grunt => {
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),
        clean: {
            default: ["dist/"],
        },
        copy: {
            css: {
                files: [{expand: true, cwd: "src/main/", src: "**/*.css", dest: "dist/", flatten: true}]
            },
            html: {
                files: [{expand: true, cwd: "src/main/", src: "**/*.html", dest: "dist/", flatten: true}]
            },
            txt: {
                files: [{expand: true, cwd: "src/main/", src: "**/*.txt", dest: "dist/", flatten: true}]
            },
        },
        focus: {
            dev: {
                include: ["css", "html", "js"],
            },
        },
        replace: {
            dev: {
                src: ["./dist/*.html", "./dist/*.js"],
                replacements: [
                    {
                        from: "%%VERSION_NUMBER%%",
                        to: "<%= pkg.version %>+" + new Date().toISOString().slice(0, 19).replace(/[-:T]/g, "")
                    }
                ],
                overwrite: true
            },
            deploy: {
                src: ["./dist/*.html", "./dist/*.js"],
                replacements: [
                    {
                        from: "%%VERSION_NUMBER%%",
                        to: "<%= pkg.version %>"
                    }
                ],
                overwrite: true
            },
        },
        webpack: {
            options: {
                entry: "./src/main/js/main.js",
                module: {
                    rules: [
                        {
                            test: /\.js$/,
                            exclude: /node_modules/,
                        },
                    ],
                },
                resolve: {
                    extensions: [".js"],
                },
                output: {
                    filename: "bundle.js",
                    path: path.resolve(__dirname, "dist/"),
                },
            },
            dev: {
                mode: "development",
                devtool: "inline-source-map",
            },
            deploy: {
                mode: "production",
            },
        },
        watch: {
            css: {
                files: ["src/main/**/*.css"],
                tasks: ["copy:css"],
            },
            html: {
                files: ["src/main/**/*.html"],
                tasks: ["copy:html"],
            },
            js: {
                files: ["src/main/**/*.js"],
                tasks: ["webpack:dev", "replace:dev"],
            },
        },
    });

    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-contrib-watch");
    grunt.loadNpmTasks("grunt-focus");
    grunt.loadNpmTasks("grunt-text-replace");
    grunt.loadNpmTasks("grunt-webpack");

    grunt.registerTask("dev", [
        // Pre
        "clean",
        // Copy files
        "copy:css",
        "copy:html",
        "copy:txt",
        // Compile
        "webpack:dev",
        // Post
        "replace:dev"
    ]);
    grunt.registerTask("dev:server", ["dev", "focus:dev"]);
    grunt.registerTask("deploy", [
        // Pre
        "clean",
        // Copy files
        "copy:css",
        "copy:html",
        "copy:txt",
        // Compile JS
        "webpack:deploy",
        // Post
        "replace:deploy"
    ]);

    grunt.registerTask("default", ["dev"]);
};
