/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-var-requires */
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const { BlueprintReplacementPlugin } = require("./blueprintReplacementPlugin");
const path = require("path");
const setupEntryFiles = require("./setupEntryFiles");
const setupBlueprintWidgetImport = require("./setupBlueprintWidgetImport");
require("./setupTsconfigPathsFile");
const webpack = require("webpack");
const RemovePlugin = require("remove-files-webpack-plugin");
const semver = require("semver");
const createImportChunk = require("./createImportChunk");
const createAllowedContext = require("./createAllowedContext");

if (semver.lt(process.version, "18.12.1")) {
    throw new Error("Spire requires node 18.12.1+ to function properly.");
}

let hasBeenCalled = false;

exports.setupCommonConfig = (isDevBuild, env, target = "ES2017", includeMobiusOverrides = true) => {
    let blueprint = env && env.BLUEPRINT && `blueprints/${env.BLUEPRINT}`;
    if (!blueprint) {
        if (isDevBuild) {
            const [, , blueprintName] = process.argv;
            if (blueprintName && isNaN(blueprintName) && blueprintName !== "content-library") {
                blueprint = `blueprints/${blueprintName}`;
            }
        } else {
            blueprint = process.env.BLUEPRINT;
        }
    }

    if (!blueprint) {
        blueprint = "content-library";
    }

    console.log(`Blueprint is ${blueprint}.`);

    if (!hasBeenCalled) {
        createImportChunk(blueprint);
        createAllowedContext(blueprint);
        setupBlueprintWidgetImport(isDevBuild, blueprint);
        setupEntryFiles(isDevBuild, blueprint);
        hasBeenCalled = true;
    }

    const removePluginOptions = {
        include: [
            "./modules/client-framework/node_modules",
            "./modules/mobius/node_modules",
            "./modules/server-framework/node_modules",
            "./modules/shell/node_modules",
        ],
        logWarning: false, // otherwise we get extra logging later about these directories not existing
    };

    const plugins = [
        new RemovePlugin({
            watch: removePluginOptions, // npm run start uses watch
            before: removePluginOptions, // npm run build uses before
        }),
        new webpack.DefinePlugin({
            BUILD_DATE: new Date().getTime(),
            BLUEPRINT_NAME: JSON.stringify(blueprint.replace("blueprints/", "")),
        }),
    ];

    plugins.push(new BlueprintReplacementPlugin(blueprint, includeMobiusOverrides));

    const commonConfig = {
        resolve: {
            extensions: [".tsx", ".ts", ".jsx", ".js"],
            plugins: [new TsconfigPathsPlugin({ configFile: "tsconfig.base.json" })],
        },
        plugins,
        stats: {
            builtAt: false,
            colors: true,
            hash: false,
            modules: false,
            entrypoints: false,
            performance: false,
            warningsFilter: [/Critical dependency: the request of a dependency is an expression/],
        },
        performance: {
            hints: false,
        },
        module: {
            rules: [
                {
                    test: /\.tsx?$/,
                    exclude: /node_modules/,
                    use: [
                        {
                            loader: "ts-loader",
                            options: {
                                happyPackMode: true,
                                getCustomTransformers: path.resolve(__dirname, "./transformers.js"),
                                compilerOptions: {
                                    target,
                                },
                            },
                        },
                    ],
                },
                {
                    test: /\.css$/,
                    use: ["style-loader", "css-loader"],
                },
                {
                    test: /\.woff(\?v=\d+\.\d+\.\d+)?$/,
                    use: "url-loader?limit=10000&mimetype=application/font-woff",
                },
                {
                    test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
                    use: "url-loader?limit=10000&mimetype=application/font-woff",
                },
                {
                    test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
                    use: "url-loader?limit=10000&mimetype=application/octet-stream",
                },
                {
                    test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
                    use: "file-loader",
                },
                {
                    test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
                    use: "url-loader?limit=10000&mimetype=image/svg+xml",
                },
            ],
        },
    };

    return commonConfig;
};
