"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCompileDirs = exports.downloadZip = exports.strToBuf = exports.fileToZip = exports.getFiles = exports.version = exports.cleanComponentDir = exports.buildWedaApp = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const path_1 = __importDefault(require("path"));
const util_1 = require("../util");
const common_1 = require("../types/common");
const index_1 = require("../mp/index");
const types_1 = require("../../types");
const mixMode_1 = require("../mp/mixMode");
const index_2 = require("../h5/index");
const common_2 = require("../../utils/common");
const postProcess_1 = require("../../utils/postProcess");
const net_1 = require("../util/net");
const pkg = require('../../../package.json');
async function buildWedaApp({ cals, subAppCalsList = [], dependencies = [], appKey = 'test', runtime = types_1.RUNTIME.NONE, ignoreInstall = false, buildTypeList = [common_1.BuildType.WEB], mode = common_1.WebpackModeType.PRODUCTION, devTool = 'webpack', deployOptions = { mode: types_1.DEPLOY_MODE.PREVIEW }, generateMpType = common_1.GenerateMpType.APP, plugins = [], extraData = {
    isComposite: false,
    compProps: {},
}, resourceAppId = undefined, domain = '', output, isBrowserMpBuilder = false, }, cb) {
    var _a, _b;
    if (!cals) {
        console.error('无效的应用配置');
        return;
    }
    const { mode: deployMode = types_1.DEPLOY_MODE.PREVIEW } = deployOptions;
    let appBuildDir = (output === null || output === void 0 ? void 0 : output.path) || (runtime === types_1.RUNTIME.CI ? (_a = (0, util_1.getCompileDirs)('app')) === null || _a === void 0 ? void 0 : _a.appBuildDir : (_b = (0, util_1.getCompileDirs)(appKey)) === null || _b === void 0 ? void 0 : _b.appBuildDir);
    console.log('domain', domain);
    console.log('应用名', appKey);
    console.log('生成模式', generateMpType);
    console.log('生成路径', appBuildDir);
    try {
        const startTime = Date.now();
        if (buildTypeList.includes(common_1.BuildType.MP)) {
            const mainAppSerializeData = (0, common_2.processCals2WeappsData)(cals, dependencies);
            const subAppSerializeDataList = (subAppCalsList === null || subAppCalsList === void 0 ? void 0 : subAppCalsList.map((item) => (0, common_2.processCals2WeappsData)(item, dependencies))) || [];
            appBuildDir = (output === null || output === void 0 ? void 0 : output.path) || path_1.default.join(appBuildDir, 'mp');
            const isMixMode = generateMpType === common_1.GenerateMpType.SUBPACKAGE;
            const apps = [mainAppSerializeData, ...subAppSerializeDataList];
            if (isBrowserMpBuilder) {
                // 尽早下载物料
                await (0, net_1.downloadBrowserMaterial)(output === null || output === void 0 ? void 0 : output.path);
            }
            const result = await (0, index_1.generateWxMp)({
                weapps: apps,
                projDir: appBuildDir,
                appId: appKey,
                domain: domain,
                materials: dependencies,
                plugins,
                isProduction: mode === common_1.WebpackModeType.PRODUCTION,
                deployMode,
                extraData,
                isMixMode,
                options: {
                    isCrossAccount: resourceAppId !== deployOptions.targetMpAppId,
                    resourceAppId,
                },
                buildTypeList,
                isBrowserMpBuilder,
            });
            // 如果是混合模式，则将特定的目录复制到工程下
            // 针对 app.json / package.json 则采用 merge 的操作
            if (isMixMode) {
                console.log(chalk_1.default.green('【混合模式】'), appBuildDir);
                await (0, mixMode_1.handleMixMode)({
                    apps,
                    generateMpPath: appBuildDir,
                    miniprogramRoot: result.miniprogramRoot,
                    plugins,
                });
            }
            const outDir = appBuildDir;
            let projectJsonPath = path_1.default.resolve(outDir, 'project.config.json');
            await (0, postProcess_1.postprocessProjectConfig)(projectJsonPath, {
                appid: deployOptions === null || deployOptions === void 0 ? void 0 : deployOptions.mpAppId,
                cloudfunctionRoot: undefined,
            });
            // 如果是代开发的模式，则写入ext.json
            await (0, postProcess_1.postprocessDeployExtraJson)(outDir, deployOptions);
            if (!isBrowserMpBuilder && generateMpType === common_1.GenerateMpType.APP) {
                // 模板拷入的 miniprogram_npm 有问题，直接删除使用重新构建的版本
                // 模板需要占位保证 mp 文件夹存在
                fs_extra_1.default.removeSync(path_1.default.resolve(outDir, 'miniprogram_npm'));
            }
            cb === null || cb === void 0 ? void 0 : cb(null, {
                ...result,
                outDir,
                timeElapsed: Date.now() - startTime,
            });
            return outDir;
        }
        else {
            const h5BuildDir = await (0, index_2.buildH5App)({
                appKey,
                buildDir: appBuildDir,
                dependencies,
                cals,
                subAppCalsList,
                extraData,
                buildTypeList,
                mode,
                devTool,
                runtime,
                deployMode,
                ignoreInstall,
                domain,
            });
            cb === null || cb === void 0 ? void 0 : cb(null, {
                outDir: h5BuildDir,
                timeElapsed: Date.now() - startTime,
            });
            return h5BuildDir;
        }
    }
    catch (e) {
        cb === null || cb === void 0 ? void 0 : cb(e);
        throw e;
    }
}
exports.buildWedaApp = buildWedaApp;
async function cleanComponentDir() {
    return fs_extra_1.default.emptyDir((0, util_1.getCompileDirs)().materialsDir);
}
exports.cleanComponentDir = cleanComponentDir;
exports.version = pkg.version;
var generateFiles_1 = require("../util/generateFiles");
Object.defineProperty(exports, "getFiles", { enumerable: true, get: function () { return generateFiles_1.getFiles; } });
Object.defineProperty(exports, "fileToZip", { enumerable: true, get: function () { return generateFiles_1.fileToZip; } });
Object.defineProperty(exports, "strToBuf", { enumerable: true, get: function () { return generateFiles_1.strToBuf; } });
var net_2 = require("../util/net");
Object.defineProperty(exports, "downloadZip", { enumerable: true, get: function () { return net_2.downloadZip; } });
var util_2 = require("../util");
Object.defineProperty(exports, "getCompileDirs", { enumerable: true, get: function () { return util_2.getCompileDirs; } });
exports.default = buildWedaApp;
