"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const util_1 = require("../../util");
function writeTempFile(paths) {
    fs_extra_1.default.writeFileSync(paths.styleSchemaTempPath, getSchemaDeclare(paths));
}
exports.writeTempFile = writeTempFile;
function getSchemaDeclare(paths) {
    const { componentsMap } = paths;
    const schemas = Object.entries(componentsMap)
        .map(([name, entry]) => `'${name}': (() => {
        const component = require('${util_1.handlePathEscape(path.resolve(entry, './config.js'))}').default;
        return (component.injectStyle && typeof component.styleForm === 'object') ? component.styleForm : null;
      })()`)
        .join(',\n');
    return `module.exports = {\n${schemas}\n}`;
}
function generateStyleSchema(paths) {
    const { outputPath, styleSchemaPath } = paths;
    if (!fs_extra_1.default.existsSync(styleSchemaPath)) {
        return;
    }
    const styleSchemaFile = `__require = require;\nrequire = () => ({});\n\n${fs_extra_1.default.readFileSync(styleSchemaPath, {
        encoding: 'utf8',
    })}\n\nrequire = __require;`;
    fs_extra_1.default.writeFileSync(path.join(outputPath, './styleSchema.js'), styleSchemaFile, {
        encoding: 'utf8',
    });
}
exports.generateStyleSchema = generateStyleSchema;
//# sourceMappingURL=generateStyleSchema.js.map