"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Utils = require("../../util/src");
const Path = require("path");
const FS = require("fs");
const Mustache = require("mustache");
function report(path) {
    if (!FS.existsSync(path)) {
        return Promise.reject(Error(`无效的运行结果文件`));
    }
    const status = FS.statSync(path);
    if (!status.isFile()) {
        return Promise.reject(Error(`必须指定执行结果文件`));
    }
    const content = FS.readFileSync(path).toString();
    let results = content.split(`[RUBIK] [CASE:RUN]`);
    if (results.length > 1) {
        results = results.slice(1);
        const store = new Array();
        for (const aresult of results) {
            let matched = aresult.match(/\[([\s\S]*?)\]/);
            let status = matched ? matched[1] : '';
            status = status === 'SUCCESS' ? Status.Success : Status.Failure;
            matched = aresult.match(/caseID\{([\s\S]*?)\}/); //  非贪婪匹配
            const id = matched ? matched[1] : '';
            matched = aresult.match(/reason\{([\s\S]*?)\}/); //  非贪婪匹配
            const reason = matched ? matched[1] : undefined;
            store.push({
                id, status, message: reason
            });
        }
        const view = FS.readFileSync(Path.join(Utils.ACUtils.respath, 'case/report.md')).toString();
        const data = {
            date: `${(new Date()).toLocaleTimeString()} ${(new Date()).toLocaleDateString()}`,
            path,
            success: store.filter(r => r.status === Status.Success),
            failure: store.filter(r => r.status === Status.Failure),
        };
        FS.writeFileSync(Path.join(process.env.HOME ? process.env.HOME : '', 'Desktop/RubikCaseReport.md'), Mustache.render(view, data));
        return Promise.resolve();
    }
    return Promise.reject(Error(`解析运行结果失败`));
}
exports.report = report;
var Status;
(function (Status) {
    Status[Status["Unknown"] = 0] = "Unknown";
    Status[Status["Success"] = 1] = "Success";
    Status[Status["Failure"] = 2] = "Failure";
})(Status || (Status = {}));
//# sourceMappingURL=report.js.map