"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processCals2WeappsData = void 0;
const cals_1 = require("@cloudbase/cals");
function processPkgUrlCals2WeappData(cals) {
    const { extra = {}, datasources, dataSources, ...restCals } = cals;
    return {
        ...extra,
        ...restCals,
        datasources: datasources || dataSources || [],
    };
}
function processCals2WeappsData(cals, dependencies) {
    var _a, _b, _c;
    let weappsData = {};
    if (!cals.mpPkgUrl) {
        weappsData = (0, cals_1.deserializePlatformApp)(cals, {
            dependencies,
        });
        if ((_a = cals.extra) === null || _a === void 0 ? void 0 : _a.miniprogramPlugins) {
            weappsData['miniprogramPlugins'] = cals.extra.miniprogramPlugins.map((plugin) => {
                var _a;
                return {
                    ...plugin,
                    componentConfigs: ((_a = plugin.componentConfigs) === null || _a === void 0 ? void 0 : _a.map((component) => {
                        const { data, events, meta = {}, ...restComponent } = component;
                        const processedCompoennt = {
                            ...restComponent,
                            meta: {
                                desc: (meta === null || meta === void 0 ? void 0 : meta.desc) || (meta === null || meta === void 0 ? void 0 : meta.description),
                            },
                        };
                        if (data) {
                            processedCompoennt['dataForm'] = data.properties;
                        }
                        if (events) {
                            processedCompoennt['emitEvents'] = events === null || events === void 0 ? void 0 : events.map((event) => ({
                                eventName: event.name,
                                name: event.title,
                            }));
                        }
                        return processedCompoennt;
                    })) || [],
                };
            });
        }
        if ((_b = cals.extra) === null || _b === void 0 ? void 0 : _b.envId) {
            weappsData['envId'] = (_c = cals.extra) === null || _c === void 0 ? void 0 : _c.envId;
        }
    }
    else {
        weappsData = processPkgUrlCals2WeappData(cals);
    }
    return weappsData;
}
exports.processCals2WeappsData = processCals2WeappsData;
