"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("../../core");
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const base_1 = __importDefault(require("./base"));
class Preview extends base_1.default {
    constructor() {
        super(core_1.ModuleName.preview);
    }
    gadget(options) {
        var _a, _b;
        return this.manager.execute(core_1.ModuleName.preview, core_1.PreviewCmdEnum.previewGadget, {
            appId: options.appId || '',
            compilePath: options.path,
            mode: options.mode,
            platform: options.platform,
            watch: options.watch,
            startPage: options.startPage,
            enableWebviewSafeDomain: (_a = options.enableWebviewSafeDomain) !== null && _a !== void 0 ? _a : true,
            debug: options.debug,
            useNPM: (_b = options.useNPM) !== null && _b !== void 0 ? _b : false,
        });
    }
    gadgetPlugin(options) {
        return this.manager.execute(core_1.ModuleName.preview, core_1.PreviewCmdEnum.previewGadgetPlugin, {
            appId: options.appId || '',
            compilePath: options.path,
            mode: options.mode,
            platform: options.platform,
            watch: options.watch,
            startPage: options.startPage,
            debug: options.debug,
        });
    }
    block(options) {
        return this.manager.execute(core_1.ModuleName.preview, core_1.PreviewCmdEnum.previewBlock, {
            appId: options.appId || '',
            compilePath: options.path,
            docsUrl: options.docsUrl,
            platform: options.platform,
            host: options.host,
        });
    }
    readBlockInfoByPath({ projectPath, blockPath, }) {
        return fs_extra_1.default.readJSONSync(path_1.default.resolve(path_1.default.join(projectPath, `${blockPath}.json`)));
    }
    readProjectConfigByPath(projectPath) {
        return fs_extra_1.default.readJSONSync(path_1.default.resolve(path_1.default.join(projectPath, 'project.config.json')));
    }
    async getH5BlockPreviewToken(options) {
        const previewResourceMap = {};
        await options.blocks.reduce(async (prev, blockPath) => {
            const { blockTypeID } = this.readBlockInfoByPath({
                projectPath: options.projectPath,
                blockPath,
            });
            const { appid } = this.readProjectConfigByPath(options.projectPath);
            const res = await this.manager.execute(core_1.ModuleName.preview, core_1.PreviewCmdEnum.previewH5Block, {
                compilePath: options.projectPath,
                appId: appid,
                blockTypeID,
            });
            previewResourceMap[blockPath] = {
                blockTypeID,
                previewToken: res.previewToken,
            };
        }, Promise.resolve());
        return previewResourceMap;
    }
}
exports.default = Preview;
