"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildBlock = void 0;
const cals_1 = require("@cloudbase/cals");
const util_1 = require("../util");
const types_1 = require("../../types");
const material_1 = require("./material");
const config_1 = require("../config");
const generate_1 = require("./generate");
async function buildBlock(buildDir, dependencies, runtime = types_1.RUNTIME.NONE) {
    var _a, _b;
    const { materialsDir } = (0, util_1.getCompileDirs)();
    let usedComps;
    const composite = dependencies.find((lib) => lib.isComposite);
    if (composite) {
        let id = 0;
        const cals = {
            items: [
                {
                    id: '$page',
                    type: 'PAGE',
                    items: ((_b = (_a = composite.components) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, (component) => {
                        id += 1;
                        return {
                            id: `id${id}`,
                            module: composite.name,
                            component: component.name,
                            attributes: {},
                        };
                    })) || [],
                },
            ],
        };
        usedComps = (0, cals_1.getUsedComps)([cals], dependencies, { streamline: true, withAction: true });
    }
    let processedDependencies = await (0, material_1.runHandleMaterial)(buildDir, dependencies.filter((lib) => {
        var _a, _b;
        // 注意需要包涵官方的(gsd)，部分平台方法需要由其补充
        return lib.name !== config_1.OFFICIAL_LIB_KEY && !!(((_a = usedComps.component) === null || _a === void 0 ? void 0 : _a[lib.name]) || ((_b = usedComps.action) === null || _b === void 0 ? void 0 : _b[lib.name]));
    }), 
    // [dependence, ...dependencies],
    materialsDir, runtime);
    const officialDependence = dependencies.find((lib) => lib.name === config_1.OFFICIAL_LIB_KEY);
    const componentDeps = (0, config_1.generateLibDependencies)('h5', officialDependence === null || officialDependence === void 0 ? void 0 : officialDependence.version);
    processedDependencies.push(officialDependence);
    let componentsList;
    try {
        componentsList = await (0, generate_1.generateBlockFiles)({
            dependencies: processedDependencies,
            appBuildDir: buildDir,
            componentDeps,
        });
    }
    catch (error) {
        throw error;
    }
    return { buildDir, componentsList };
}
exports.buildBlock = buildBlock;
