"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPluginType = exports.installMpPlugin = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const mp_1 = require("../util/mp");
// 安装原生小程序类的插件
async function installMpPlugin(appBuildDir, plugins) {
    return Promise.all(plugins
        .filter((item) => item.type === 'mp')
        .map(async (plugin) => {
        const pluginModule = plugin.module;
        const pluginNodeModuleDir = path.resolve(appBuildDir, 'node_modules', pluginModule);
        const pluginPkgJson = await fs.readJson(path.join(pluginNodeModuleDir, 'package.json'));
        const { pluginName } = pluginPkgJson;
        const pluginNodeModuleSrcDir = path.resolve(pluginNodeModuleDir, pluginName);
        const mpBuildPluginDir = path.resolve(appBuildDir, pluginName);
        const mpBuildAppJsonPath = path.resolve(appBuildDir, 'app.json');
        const pluginAppJsonPath = path.resolve(pluginNodeModuleDir, 'app.json');
        // 将插件目录复制到项目里
        console.log('安装 mp 插件', pluginModule, mpBuildPluginDir);
        await fs.copy(pluginNodeModuleSrcDir, mpBuildPluginDir, {
            overwrite: true,
        });
        // 合并 app.json
        await (0, mp_1.mergeSubPackages)(mpBuildAppJsonPath, pluginAppJsonPath);
    })).catch((err) => console.error(err));
    // 这里的 mergeAppJson 是不生效的，只能到 wa-watch 里 merge
    // GOTO webpack compile callback
}
exports.installMpPlugin = installMpPlugin;
/**
 * 根据 plugin 获取插件类型
 * @param appBuildDir 编译的目录
 * @param plugins 插件列表
 */
async function getPluginType(appBuildDir, plugins) {
    const pluginsWithType = [];
    for (const plugin of plugins) {
        const pluginName = plugin.name;
        // TODO: @govcloud 是在私域里，后续需要接入配置修改
        const pkgPath = path.resolve(appBuildDir, `node_modules/@govcloud/${pluginName}/package.json`);
        const pkgJson = await fs.readJson(pkgPath);
        pluginsWithType.push({
            ...plugin,
            type: pkgJson.pluginType,
        });
    }
    return pluginsWithType;
}
exports.getPluginType = getPluginType;
