"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsdom_1 = require("jsdom");
const fs_extra_1 = __importDefault(require("fs-extra"));
const path = __importStar(require("path"));
function getMaterialsMeta(paths) {
    const script = fs_extra_1.default.readFileSync(paths.metaTempPath, { encoding: 'utf8' });
    const html = `<body><script>${script}</script></body>`;
    const { window } = new jsdom_1.JSDOM(html, {
        runScripts: 'dangerously',
        pretendToBeVisual: true,
        url: 'http://vision.oa.com/',
        contentType: 'text/html',
    });
    const key = `@impage-materials-meta-${paths.libName}`;
    return window[key];
}
exports.getMaterialsMeta = getMaterialsMeta;
function generateMeta(paths, { components, plugins, actions }) {
    const { manifestPath, outputPath } = paths;
    const manifest = JSON.parse(fs_extra_1.default.readFileSync(manifestPath, { encoding: 'utf8' }));
    manifest.styleSchema = 'styleSchema.js';
    const getMeta = ([, { name, info }]) => ({
        path: name,
        info,
    });
    const result = {
        manifest,
        components: Object.entries(components).map(getMeta),
        plugins: Object.entries(plugins).map(getMeta),
        actions: Object.entries(actions).map(getMeta),
    };
    fs_extra_1.default.writeFileSync(path.join(outputPath, './meta.json'), JSON.stringify(result, undefined, 2));
    fs_extra_1.default.unlinkSync(manifestPath);
}
exports.generateMeta = generateMeta;
function generateStyleSchema(paths, { components }) {
    const schemaMap = Object.entries(components).reduce((accu, [, { name, injectStyle, styleForm }]) => {
        if (injectStyle && typeof styleForm === 'object') {
            accu[name] = styleForm;
        }
        return accu;
    }, {});
    const styleSchemaFile = JSON.stringify(schemaMap, (k, v) => {
        if (typeof v === 'function') {
            if (k === 'format') {
                return v.toString();
            }
            return null;
        }
        return v;
    }, '\t');
    fs_extra_1.default.writeFileSync(path.join(paths.outputPath, './styleSchema.js'), `module.exports = ${styleSchemaFile}`, { encoding: 'utf8' });
}
exports.generateStyleSchema = generateStyleSchema;
//# sourceMappingURL=generateMeta.js.map