"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const webpack_merge_1 = __importDefault(require("webpack-merge"));
class WebpackBuilder {
    ///////////////////////////
    ////// Path Builders //////
    ///////////////////////////
    constructor(buildOptions, webpackConfig = {}) {
        this.webpackConfig = webpackConfig;
        const { appJsPath, assetsDirPath, buildDirPath, configDirPath, configs, bluebaseJsPath } = buildOptions;
        if (!configs) {
            throw Error(`No bundle configuration given to WebpackBuilder.`);
        }
        this.configs = Object.assign({}, configs);
        const isProd = cli_core_1.Utils.isProduction() ? 'production' : 'development';
        this.configs.mode = configs.mode || isProd;
        this.configs.target = configs.target || 'web';
        const ifElse = cli_core_1.Utils.ifElse;
        // Init
        this.appJsPath = appJsPath;
        this.assetsDirPath = assetsDirPath;
        this.bluebaseJsPath = bluebaseJsPath;
        this.buildDirPath = buildDirPath;
        this.configs = configs;
        this.configDirPath = configDirPath;
        // Path helpers
        this.isProd = this.configs.mode === 'production' ? true : false;
        this.isDev = !this.isProd;
        this.isClient = this.configs.target !== 'node';
        this.isServer = !this.isClient;
        this.isNode = !this.isClient;
        // this.isElectron = this.configs.target === 'electron';
        // Conditionals
        this.ifDev = ifElse(this.isDev);
        this.ifProd = ifElse(this.isProd);
        this.ifNode = ifElse(this.isNode);
        this.ifClient = ifElse(this.isClient);
        this.ifDevClient = ifElse(this.isDev && this.isClient);
        this.ifProdClient = ifElse(this.isProd && this.isClient);
    }
    /**
     * Build a webpack config
     * @param webpackConfigInput
     */
    build() {
        return this.webpackConfig;
    }
    /**
     * Use a middleware. A middleware is a function that returns a merged
     * webpack config
     * @param middleware
     */
    use(middleware) {
        this.webpackConfig = middleware(this.webpackConfig, this);
        return this;
    }
    /**
     * Merge a webpack config
     * @param config
     */
    merge(config) {
        this.webpackConfig = webpack_merge_1.default(this.webpackConfig, config);
        return this;
    }
}
exports.default = WebpackBuilder;
//# sourceMappingURL=WebpackBuilder.js.map