"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require('fs-extra');
const builder_1 = __importDefault(require("./builder"));
const tools_1 = __importDefault(require("./tools"));
function preview(req, res, paths, container) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            console.log('>>>>>>Preview start');
            const { componentsMap, containersMap, actionsMap, pluginsMap } = paths, other = __rest(paths, ["componentsMap", "containersMap", "actionsMap", "pluginsMap"]);
            const param = Object.assign(Object.assign({}, req.body), { componentsMap,
                containersMap,
                actionsMap,
                pluginsMap,
                container, loginName: 'debug', paths: other });
            const { url } = yield serve(param);
            console.log('>>>>>>Url is :', url);
            console.log('>>>>>>Preview end');
            res.send({
                retcode: 0,
                data: {
                    url,
                },
            });
        }
        catch (err) {
            console.error(err);
            res.send({
                retcode: 1,
                msg: err && err.message,
            });
        }
    });
}
exports.preview = preview;
function serve(param) {
    return __awaiter(this, void 0, void 0, function* () {
        const opts = getBuildOpts(param);
        console.log('Final build options:', opts);
        const build = new builder_1.default(opts);
        const buildResult = yield build.build();
        console.log('build result:', buildResult);
        return buildResult;
    });
}
function getBuildOpts(param) {
    const { loginName, componentsMap, actionsMap, containersMap, pluginsMap, container, paths } = param;
    let { history_content, history_ext } = param;
    let projects;
    let mainProject;
    const parsedHistoryContent = JSON.parse(param.history_content || '{}');
    const parsedHistoryExt = JSON.parse(param.history_ext || '{}');
    if (parsedHistoryContent.components) {
        parsedHistoryContent.components.forEach(c => {
            if (c.commonStyle) {
                const position = tools_1.default.getPosition(c.commonStyle);
                if (position) {
                    c.position = position;
                }
                c.commonStyle = tools_1.default.mergeCommonStyle(c.commonStyle);
            }
            if (c.wrapperStyle) {
                c.wrapperStyle = tools_1.default.mergeCommonStyle(c.wrapperStyle);
            }
        });
    }
    return {
        activity: {
            act_key: 'debug',
            dataValues: {
                act_id: '0',
                act_name: 'debug',
                act_description: 'debug',
                act_key: 'debug',
                act_start_time: Date.now(),
                act_end_time: Date.now(),
                act_is_base: 0,
            }
        },
        container,
        containersMap,
        componentsMap,
        actionsMap,
        pluginsMap,
        // projects,
        // mainProject: {
        //   proj_key: paths.libName,
        // },
        historyContent: parsedHistoryContent,
        historyExt: parsedHistoryExt,
        loginName,
        paths,
    };
}
//# sourceMappingURL=index.js.map