"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
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 FS = __importStar(require("fs"));
const Path = __importStar(require("path"));
const Utils = __importStar(require("../../../util/src"));
const V1 = __importStar(require("./v1"));
const V2 = __importStar(require("./v2"));
const Capnp = __importStar(require("./capnp"));
function build(path, output, process) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
            const cost = new Utils.Cost();
            const pickTplDistPath = (root) => {
                const manifest = JSON.parse(FS.readFileSync(Path.join(root, 'manifest.json')).toString());
                if (manifest.name) {
                    const distpath = Path.join(output, `${manifest.name}`);
                    if (!FS.existsSync(distpath)) {
                        FS.mkdirSync(distpath);
                    }
                    return distpath;
                }
                else {
                    return '';
                }
            };
            //  i18n / l10n
            const pickI18nInfo = (tplRoot) => {
                let i18n;
                Utils.FS.walk(tplRoot, (root, name, stats) => {
                    if (stats.isFile() && Path.extname(name) === '.json' && name.indexOf('i18n') === 0) {
                        const data = JSON.parse(FS.readFileSync(Path.join(root, name)).toString());
                        if (data) {
                            if (i18n) {
                                i18n = Object.assign(Object.assign({}, i18n), data);
                            }
                            else {
                                i18n = data;
                            }
                        }
                    }
                });
                return i18n ? new Capnp.AI18nPhrases(i18n) : undefined;
            };
            Utils.Console.info(`清理构建产物目录 [${output}]`);
            Utils.FS.rmdirs(output);
            Utils.FS.mkdirs(output);
            const builders = new Array();
            const capnpBuilds = new Array();
            Utils.FS.walk(path, (root, name, stats) => {
                if (stats.isFile() && name === 'package.json') {
                    Utils.Console.info(`提取工程配置 [${Path.join(root, name)}]`);
                    FS.writeFileSync(Path.join(output, 'app.manifest'), FS.readFileSync(Path.join(root, name)).toString());
                }
                else if (stats.isFile() && name === 'main.vue') {
                    Utils.Console.info(`提取模板文件 [${Path.join(root, name)}]`);
                    const manifest = JSON.parse(FS.readFileSync(Path.join(root, 'manifest.json')).toString());
                    if (manifest.compilerType === 1) {
                        builders.push(V2.parse(Path.join(root, name)).then(value => {
                            const distpath = pickTplDistPath(root);
                            value.meta = FS.readFileSync(Path.join(root, 'manifest.json')).toString();
                            const i18n = pickI18nInfo(root);
                            value.i18nPhrases = i18n;
                            const jsonpath = Path.join(distpath, `main.json`);
                            const binpath = Path.join(distpath, `main.bin`);
                            FS.writeFileSync(jsonpath, JSON.stringify(value, undefined, 2), { encoding: 'utf8' });
                            capnpBuilds.push(Capnp.build(jsonpath, binpath).then(value => { }).catch(reason => { }));
                        }));
                    }
                    else {
                        builders.push(V1.parse(Path.join(root, name)).then(value => {
                            const distpath = pickTplDistPath(root);
                            value.meta = FS.readFileSync(Path.join(root, 'manifest.json')).toString();
                            const i18n = pickI18nInfo(root);
                            value.i18nPhrases = i18n;
                            const jsonpath = Path.join(distpath, `main.json`);
                            const binpath = Path.join(distpath, `main.bin`);
                            FS.writeFileSync(jsonpath, JSON.stringify(value, undefined, 2), { encoding: 'utf8' });
                            capnpBuilds.push(Capnp.build(jsonpath, binpath).then(value => { }).catch(reason => { }));
                        }));
                    }
                }
                else if (stats.isFile() && name.endsWith('.json') && name.indexOf('mock') === 0) {
                    Utils.Console.info(`提取mock数据 [${Path.join(root, name)}]`);
                    const distpath = pickTplDistPath(root);
                    const mockpath = Path.join(distpath, 'main.mock');
                    let mock = new Array();
                    if (FS.existsSync(mockpath)) {
                        mock = JSON.parse(FS.readFileSync(mockpath).toString());
                    }
                    mock.push({
                        data: FS.readFileSync(Path.join(root, name)).toString()
                    });
                    FS.writeFileSync(mockpath, JSON.stringify(mock, undefined, 2), { encoding: 'utf8' });
                }
            });
            yield Promise.all(builders);
            yield Promise.all(capnpBuilds);
            cost.mark();
            resolve();
        }));
    });
}
exports.build = build;
//# sourceMappingURL=index.js.map