"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.runHandleMaterial = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const lodash_1 = require("lodash");
const webpack_1 = require("../service/webpack");
const copy_1 = require("./copy");
const types_1 = require("../../types");
const common_1 = require("../util/common");
async function runHandleMaterial(appBuildDir, dependencies = [], materialsDir, runtime = types_1.RUNTIME.NONE, ignoreInstall = false) {
    const allMaterials = [
        await handleNormalMaterial({
            dependencies,
            materialsDir,
            appBuildDir,
            runtime,
            ignoreInstall,
        }),
    ];
    return (0, lodash_1.flatten)(allMaterials);
    // TODO: 确认这里是否需要生成复合组件
    // await handleCompositeComponent({ dependencies, appBuildDir });
}
exports.runHandleMaterial = runHandleMaterial;
async function handleNormalMaterial({ dependencies, materialsDir, appBuildDir, runtime, ignoreInstall = false }) {
    const timeTag = '-------------------- handleNormalMaterial';
    console.time(timeTag);
    const normalDependencies = dependencies.filter((item) => !item.isComposite);
    await (0, webpack_1.downloadAndInstallDependencies)(normalDependencies, materialsDir, {
        runtime,
        ignoreInstall,
    });
    await (0, copy_1.copyMaterialLibraries)(normalDependencies, materialsDir, appBuildDir);
    console.timeEnd(timeTag);
    return normalDependencies.map((metaInfo) => {
        var _a, _b;
        const { name: materialName } = metaInfo;
        const materialItemPath = path
            .resolve(appBuildDir, 'src/libraries', `${metaInfo.name}@${metaInfo.version}`)
            .replace(/packages\/\w+\//, ''); // HACK：去除子包的目录，找根目录的素材地址。后续提供一个方法获取这些关键路径。
        const materialComponentsPath = path.resolve(materialItemPath, `components`);
        const actionsDir = path.join(materialItemPath, 'actions');
        const meta = (0, common_1.readComponentLibMata)(materialItemPath);
        let actions = [];
        let components = [];
        if (meta) {
            const { components: componentsMap, actions: actionsMap = {} } = meta;
            for (let name in componentsMap) {
                components.push({ name });
            }
            for (let name in actionsMap) {
                actions.push({ name: name });
            }
        }
        else {
            // 老格式，需要从子目录下读取
            actions = ((_a = fs.readdirSync(actionsDir)) === null || _a === void 0 ? void 0 : _a.map((dirName) => ({ name: dirName }))) || [];
            components = (_b = fs.readdirSync(materialComponentsPath)) === null || _b === void 0 ? void 0 : _b.map((dirName) => ({ name: dirName }));
        }
        // TODO: 根据mata判断而非readdir
        return {
            ...metaInfo,
            actions,
            components,
            plugins: [],
        };
    });
}
// async function handleCompositeComponent({ dependencies, appBuildDir }) {
//   console.time('handleCompositeComponent');
//   const compositeDependencies: IMaterialItem[] = dependencies.filter(
//     (item) => item.isComposite
//   );
//   const materialGroupInfoMap = {};
//   dependencies.forEach(
//     (item) =>
//       (materialGroupInfoMap[item.name] = {
//         isComposite: item.isComposite,
//         version: item.version,
//         entries: item.entries,
//         schemaVersion: item.schemaVersion,
//       })
//   );
//   const componentsMeta = await getComponentsInfo(
//     path.join(appBuildDir, 'src'),
//     dependencies
//   );
//   await writeLowCodeFilesForCompositeComp(compositeDependencies, appBuildDir);
//   await genCompositeComponentLibraries(
//     compositeDependencies,
//     appBuildDir,
//     materialGroupInfoMap,
//     componentsMeta
//   );
//   console.timeEnd('handleCompositeComponent');
// }
