"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyMaterialLibraries = exports.runCopy = void 0;
const path = __importStar(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const chalk_1 = __importDefault(require("chalk"));
const config_1 = require("../config");
const generateFiles_1 = require("../util/generateFiles");
const junk = __importStar(require("../util/junk"));
// 模版文件只复制一次
async function runCopy(appBuildDir, webRuntimeAppData, i18nConfig) {
    const timeTag = '-------------------- runCopy ';
    console.time(timeTag);
    // const srcFiles = ['webpack', 'webpack/vite.config.js'];
    const srcFiles = ['webpack'];
    console.log(chalk_1.default.blue.bold('Copying files:'));
    for (const entry of srcFiles) {
        const dstFile = path.join(appBuildDir, entry);
        await fs_extra_1.default.copy(path.join(config_1.appTemplateDir, entry), dstFile);
    }
    await (0, generateFiles_1.writeFile)(path.join(appBuildDir, 'mainAppData.json'), JSON.stringify(webRuntimeAppData, null, 2));
    const fileDir = path.join(appBuildDir, 'src/i18n/');
    if (!fs_extra_1.default.existsSync(fileDir)) {
        fs_extra_1.default.mkdirSync(fileDir, { recursive: true });
    }
    // i8n数据
    await (0, generateFiles_1.writeFile)(path.join(appBuildDir, 'src/i18n/index.js'), `export default ${JSON.stringify((i18nConfig === null || i18nConfig === void 0 ? void 0 : i18nConfig.i18nData) || {}, null, 2)}`);
    console.timeEnd(timeTag);
}
exports.runCopy = runCopy;
async function copyMaterialLibraries(dependencies = [], materialsDir, appBuildDir) {
    return Promise.all(dependencies.map(async (componentLib) => {
        const { name, version } = componentLib;
        const materialNameVersion = `${name}@${version}`;
        const materialDir = path.join(materialsDir, materialNameVersion);
        const srcDir = 'src';
        let targetDir = path.join(materialDir, srcDir);
        const librariesDir = path.join(appBuildDir, 'src/libraries', materialNameVersion);
        const metaJosnPath = path.join(materialDir, 'meta.json');
        if (fs_extra_1.default.existsSync(metaJosnPath)) {
            await fs_extra_1.default.copy(metaJosnPath, path.join(librariesDir, 'meta.json'));
        }
        await fs_extra_1.default.copy(targetDir, path.join(librariesDir, srcDir), {
            filter: (src, dest) => {
                let path = src.split('/');
                return !junk.is(path[path.length - 1]);
            },
        });
        const nodeModulesPath = path.join(materialDir, 'node_modules');
        const targetNodeModulesPath = path.join(librariesDir, 'node_modules');
        if (fs_extra_1.default.existsSync(nodeModulesPath) && !fs_extra_1.default.existsSync(targetNodeModulesPath)) {
            await fs_extra_1.default.symlink(nodeModulesPath, targetNodeModulesPath, 'dir');
        }
        const packageJsonPath = path.join(materialDir, 'package.json');
        if (fs_extra_1.default.existsSync(packageJsonPath)) {
            await fs_extra_1.default.copy(packageJsonPath, path.join(librariesDir, 'package.json'));
        }
    }));
}
exports.copyMaterialLibraries = copyMaterialLibraries;
