"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;
};
Object.defineProperty(exports, "__esModule", { value: true });
const Path = __importStar(require("path"));
const ChildProcess = __importStar(require("child_process"));
const ACUtils = __importStar(require("../../../util/src"));
const zstd = require('@antcube/node-zstandard');
/*
 *  各种应用场景的具体数据模型
*/
class AI18nItem {
    constructor(meta) {
        for (const key in meta) {
            if (meta.hasOwnProperty(key)) {
                if (!this.entries) {
                    this.entries = new Array();
                }
                const entry = new Object();
                entry.key = key;
                const plurals = this.pickPlurals(meta[key]);
                if (plurals) {
                    entry.plurals = plurals;
                }
                else {
                    entry.value = meta[key];
                }
                this.entries.push(entry);
            }
        }
    }
    pickPlurals(value) {
        return (value && value.indexOf('||||') !== -1) ? value.split('||||') : undefined;
    }
}
exports.AI18nItem = AI18nItem;
class AI18nPhrases {
    constructor(meta) {
        for (const key in meta) {
            if (meta.hasOwnProperty(key)) {
                if (!this.entries) {
                    this.entries = new Array();
                }
                const entry = new Object();
                entry.key = key;
                entry.value = new AI18nItem(meta[key]);
                this.entries.push(entry);
            }
        }
    }
}
exports.AI18nPhrases = AI18nPhrases;
/*
    编译接口（前提：brew install capnp）
*/
function build(jsonpath, outpath) {
    return new Promise((resolve, reject) => {
        const cmd = Path.join(ACUtils.ACUtils.respath, 'capnp/falcon_convert');
        const schema = Path.join(ACUtils.ACUtils.respath, 'capnp/cube.capnp');
        const result = ChildProcess.spawnSync(cmd, [schema, jsonpath, outpath]);
        const zstpath = Path.join(Path.dirname(outpath), 'main.zst');
        if (result.status !== 0) {
            reject(new Error(result.stderr));
        }
        else {
            zstd.compress(outpath, zstpath, 3, (err, rst) => {
                if (err) {
                    reject(Error(`compress : ${err}`));
                }
                else {
                    resolve(result.stdout);
                }
            });
        }
    });
}
exports.build = build;
//# sourceMappingURL=capnp.js.map