"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());
    });
};
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
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 Model = __importStar(require("../model"));
const Snapshot = __importStar(require("./snapshot"));
const handlers = Object.create(null);
handlers[`portrait_type_${Model.Portrait.Type.Image}`] = Snapshot.check;
function check(casePath, baselinePath) {
    return __awaiter(this, void 0, void 0, function* () {
        const cost = new Utils.Cost();
        const reports = new Array();
        Utils.Console.info(`开始提取待校验用例运行结果...`);
        const cases = yield pick(casePath);
        Utils.Console.info(`开始提取用例基线...`);
        const baselines = yield pick(baselinePath);
        for (const acase of cases) {
            const report = {
                path: acase.path,
                info: acase.content,
                portraits: []
            };
            Utils.Console.info(`校验用例 ${acase.content.case.ID} [${acase.path}]`);
            const abaseline = findBaseline(baselines, acase.content.case.ID);
            if (abaseline) {
                if (acase.content.portraits.length === abaseline.content.portraits.length) {
                    const count = acase.content.portraits.length;
                    for (let index = 0; index < count; index++) {
                        const casePortrait = acase.content.portraits[index];
                        const baselinePortrait = abaseline.content.portraits[index];
                        const func = handlers[`portrait_type_${casePortrait.type}`];
                        if (func) {
                            const result = yield func(casePortrait, baselinePortrait, acase, abaseline);
                            report.portraits.push(result);
                        }
                        else {
                            report.portraits.push({
                                success: false,
                                message: `未找到可用的校验模块(${casePortrait.type})`
                            });
                        }
                    }
                }
                else {
                    report.portraits = [{ success: false, message: `基线特征数量不匹配` }];
                }
            }
            else {
                report.portraits = [{ success: false, message: `未找到匹配基线` }];
            }
            reports.push(report);
        }
        cost.mark();
        return Promise.resolve(reports);
    });
}
exports.check = check;
function pick(path) {
    const results = new Array();
    Utils.FS.walk(path, (root, name, stats) => {
        if (stats.isFile() && Path.extname(name) === '.rcr') {
            Utils.Console.info(`[${results.length}] ${Path.join(root, name)}`);
            results.push({
                path: Path.join(root, name),
                content: JSON.parse(FS.readFileSync(Path.join(root, name)).toString())
            });
        }
    });
    return Promise.resolve(results);
}
function findBaseline(baselines, ID) {
    var _a;
    let matched = undefined;
    if (ID) {
        for (const abaseline of baselines) {
            const content = abaseline.content;
            if (content && ((_a = content.case) === null || _a === void 0 ? void 0 : _a.ID) === ID) {
                matched = abaseline;
                break;
            }
        }
    }
    return matched;
}
__export(require("./report"));
//# sourceMappingURL=index.js.map