"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 Utils = __importStar(require("../../util/src"));
const Path = __importStar(require("path"));
const FS = __importStar(require("fs"));
const Mustache = __importStar(require("mustache"));
const Pngjs = __importStar(require("pngjs"));
function report(items) {
    var _a;
    const path = Path.join(process.env.HOME ? process.env.HOME : '', 'Desktop/_rubik');
    Utils.FS.rmdirs(path);
    Utils.FS.mkdirs(Path.join(path, 'res'));
    const successItems = new Array();
    const failureItems = new Array();
    let index = 1;
    for (const aitem of items) {
        const item = Object.assign({}, aitem);
        const failstacks = item.stacks ? (_a = item.stacks) === null || _a === void 0 ? void 0 : _a.filter(v => !v.success) : [];
        Utils.Console.info(`填充报告 ${aitem.caseID} [${index}/${items.length}]`);
        if (failstacks.length > 0) {
            for (const astack of failstacks) {
                if (astack.images) {
                    FS.writeFileSync(Path.join(path, `res/baseline_${item.caseID}.png`), Pngjs.PNG.sync.write(astack.images.baseline));
                    FS.writeFileSync(Path.join(path, `res/meta_${item.caseID}.png`), Pngjs.PNG.sync.write(astack.images.meta));
                    FS.writeFileSync(Path.join(path, `res/diff_${item.caseID}.png`), Pngjs.PNG.sync.write(astack.images.diff));
                    astack.images = {
                        meta: `./res/meta_${item.caseID}.png`,
                        baseline: `./res/baseline_${item.caseID}.png`,
                        diff: `./res/diff_${item.caseID}.png`
                    };
                }
            }
            failureItems.push(item);
        }
        else {
            successItems.push(item);
        }
        index++;
    }
    const view = FS.readFileSync((Path.join(Utils.ACUtils.respath, 'case/report.md'))).toString();
    const data = {
        date: `${(new Date()).toLocaleTimeString()} ${(new Date()).toLocaleDateString()}`,
        successItems,
        failureItems
    };
    FS.writeFileSync(Path.join(path, 'report.md'), Mustache.render(view, data));
    Utils.Console.info(`结果报告已生成，详情请参阅 [${path}]`);
    return Promise.resolve();
}
exports.report = report;
//# sourceMappingURL=report.js.map