"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 });
/**
 * @desc 自动 import 素材到单个 bundle
 */
const path = __importStar(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const util_1 = require("../../util");
function writeTempFile(paths, container) {
    fs_extra_1.default.writeFileSync(paths.autoRequireMetaTempPath, getMetaExportsDeclare(paths, container));
    fs_extra_1.default.writeFileSync(paths.autoRequireTempPath, getExportsDeclare(paths));
}
exports.writeTempFile = writeTempFile;
function getExportsDeclare(paths) {
    const { componentsMap, pluginsMap, actionsMap } = paths;
    const components = Object.entries(componentsMap)
        .map(([name, entry]) => {
        return `'${name}': require('${util_1.handlePathEscape(path.resolve(entry, './index.js'))}').default`;
    })
        .join(',\n');
    const plugins = Object.entries(pluginsMap)
        .map(([name, entry]) => {
        return `'${name}': require('${util_1.handlePathEscape(path.resolve(entry, './index.js'))}').default`;
    })
        .join(',\n');
    const actions = Object.entries(actionsMap)
        .map(([name, entry]) => {
        return `'${name}': require('${util_1.handlePathEscape(path.resolve(entry, './index.js'))}').default`;
    })
        .join(',\n');
    return `module.exports = {
    components: { ${components} },
    plugins: { ${plugins} },
    actions: { ${actions} },
  }`;
}
function getMetaExportsDeclare(paths, container) {
    const { componentsMap, pluginsMap, actionsMap } = paths;
    const componentsMeta = Object.entries(componentsMap)
        .map(([name, entry]) => {
        const thumbPath = findThumb(entry);
        return `{ name: '${name}', thumb: require('${util_1.handlePathEscape(thumbPath)}'), ...require('${util_1.handlePathEscape(path.resolve(entry, './config.js'))}').default }`;
    })
        .join(',\n');
    const pluginsMeta = Object.entries(pluginsMap)
        .map(([name, entry]) => {
        const thumbPath = findThumb(entry);
        return `{ name: '${name}', thumb: require('${util_1.handlePathEscape(thumbPath)}'), ...require('${util_1.handlePathEscape(path.resolve(entry, './config.js'))}').default }`;
    })
        .join(',\n');
    const actionsMeta = Object.entries(actionsMap)
        .map(([name, entry]) => {
        return `{ name: '${name}', ...require('${util_1.handlePathEscape(path.resolve(entry, './config.js'))}').default }`;
    })
        .join(',\n');
    let containerContent = null;
    if (container) {
        const containerPath = paths.containersMap[container];
        containerContent = `{
        content: require('${util_1.handlePathEscape(path.resolve(containerPath, 'index.html.ejs'))}').default,
        config: require('${util_1.handlePathEscape(path.resolve(containerPath, 'config.json'))}'),
    }`;
    }
    return `module.exports = {
    components: [${componentsMeta}],
    plugins: [${pluginsMeta}],
    actions: [${actionsMeta}],${containerContent ? `\n    container: ${containerContent}` : ''}
  }`;
}
function findThumb(entry) {
    const svg = path.join(entry, './thumb.svg');
    if (fs_extra_1.default.existsSync(svg))
        return svg;
    const png = path.join(entry, './thumb.png');
    if (fs_extra_1.default.existsSync(png))
        return png;
    const jpg = path.join(entry, './thumb.jpg');
    if (fs_extra_1.default.existsSync(jpg))
        return jpg;
    const jpeg = path.join(entry, './thumb.jpeg');
    if (fs_extra_1.default.existsSync(jpeg))
        return jpeg;
    throw util_1.error(`No thumb file at ${entry}, accept thumb.svg thumb.png thumb.jp(e)g.`);
}
//# sourceMappingURL=autoRequire.js.map