const mix = require("laravel-mix");
require("laravel-vue-i18n/mix");

// LOCAL HTTPS SETUP
const fs = require("fs");
const path = require("path");
const homeDir = process.env.HOME;
const host = process.env.APP_URL.split("//")[1];
let key = null;
let cert = null;
console.log(`.config/valet/Certificates/${host}.key`);
if (
    process.env.NODE_ENV === "development" &&
    fs.existsSync(
        path.join(homeDir, `.config/valet/Certificates/${host}.key`)
    ) &&
    fs.existsSync(path.join(homeDir, `.config/valet/Certificates/${host}.crt`))
) {
    key = fs.readFileSync(
        path.resolve(homeDir, `.config/valet/Certificates/${host}.key`)
    );
    cert = fs.readFileSync(
        path.resolve(homeDir, `.config/valet/Certificates/${host}.crt`)
    );
    console.log("Using Valet SSL", host);
} else {
    console.log("Not using SSL, so no key or cert");
}

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel applications. By default, we are compiling the CSS
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js("resources/js/app.js", "public/js")
    .js("resources/js/plain-app.js", "public/js")
    .vue({ version: 3 })
    .i18n()
    .postCss("resources/css/app.css", "public/css", [require("tailwindcss")])
    // .browserSync({
    //     proxy: "https://" + host,
    //     host: host,
    //     open: "external",
    //     // port: "80",
    //     https: {
    //         key:
    //             process.env.HOME +
    //             "/.config/valet/Certificates/" +
    //             host +
    //             ".key",
    //         cert:
    //             process.env.HOME +
    //             "/.config/valet/Certificates/" +
    //             host +
    //             ".crt",
    //     },
    //     notify: false, //Enable or disable notifications
    // })
    .options({
        hmrOptions: {
            host: host,
            port: "8080",
            open: "external",
        },
    })
    .webpackConfig({
        devServer: {
            https: key && cert ? { key, cert } : false,
        },
    })
    .webpackConfig(require("./webpack.config"));

if (mix.inProduction()) {
    mix.version();
}
