"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runWebpackCore = void 0;
const common_1 = require("../types/common");
const webpack_1 = require("../service/webpack");
const generate_1 = require("./generate");
const cals_1 = require("@cloudbase/cals");
async function runWebpackCore({ buildContext, cals, mainAppData, subAppDataList, appBuildDir, publicPath, mode = common_1.WebpackModeType.NONE, appKey, buildTypeList = [common_1.BuildType.WEB], assets = [], devTool = 'vite', generateMpType = common_1.GenerateMpType.APP, cdnEndpoints, }) {
    var _a;
    console.time('runWebpackCore');
    console.time('webpackGenerate');
    const allAppDataList = subAppDataList.concat(mainAppData);
    const webWebpackConfigPath = await (0, webpack_1.generateWebpackWebBuildParamsFile)({
        appId: mainAppData.id || appKey,
        allAppDataList,
        appBuildDir,
        publicPath,
        mode,
        buildTypeList,
        assets,
    });
    console.timeEnd('webpackGenerate');
    await (0, generate_1.generateHTML)({
        buildContext,
        appId: mainAppData.id || appKey,
        envId: mainAppData.envId || '',
        description: cals.description || cals.label || '',
        enableLoading: (_a = cals.extra) === null || _a === void 0 ? void 0 : _a.enableLoading,
        appBuildDir,
        externalResources: (cals.externalResources || []).concat(assets.map((url) => ({
            type: cals_1.EExternalResourceType.CSSUrl,
            url,
        }))),
        mode,
        devTool,
        isBuildApp: buildTypeList.includes(common_1.BuildType.APP),
        buildTypeList,
        cdnEndpoints,
        publicPath,
    });
    if ((0, common_1.buildAsWebByBuildType)(buildTypeList)) {
        if (mode !== common_1.WebpackModeType.PRODUCTION) {
            await (0, webpack_1.generateWebpackWebDevServerFile)({
                appBuildDir,
                buildTypeList,
            });
        }
    }
    console.timeEnd('runWebpackCore');
    return webWebpackConfigPath;
}
exports.runWebpackCore = runWebpackCore;
