"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Path = __importStar(require("path"));
const FS = __importStar(require("fs"));
const pixelmatch_1 = __importDefault(require("pixelmatch"));
const Pngjs = __importStar(require("pngjs"));
const Utils = __importStar(require("../../../util/src"));
function check(casePortrait, baselinePortrait, acase, abaseline) {
    return __awaiter(this, void 0, void 0, function* () {
        const caseImagePath = Path.join(Path.dirname(acase.path), casePortrait.value.name);
        const baselineImagePath = Path.join(Path.dirname(abaseline.path), baselinePortrait.value.name);
        const caseImage = Pngjs.PNG.sync.read(FS.readFileSync(caseImagePath));
        const baselineImage = Pngjs.PNG.sync.read(FS.readFileSync(baselineImagePath));
        const { width, height } = caseImage;
        const diffImage = new Pngjs.PNG({ width, height });
        const mismatched = pixelmatch_1.default(caseImage.data, baselineImage.data, diffImage.data, width, height, { threshold: 0.1 });
        if (mismatched > 1000) {
            const diffImagePath = Path.join(Path.dirname(acase.path), `${casePortrait.value.name}_diff.png`);
            FS.writeFileSync(diffImagePath, Pngjs.PNG.sync.write(diffImage));
            const caseImageUrl = yield Utils.Basement.upload(caseImagePath, `${casePortrait.value.name}_${new Date().getTime()}`);
            const baselineImageUrl = yield Utils.Basement.upload(baselineImagePath, `${baselinePortrait.value.name}_${new Date().getTime()}`);
            const diffImageUrl = yield Utils.Basement.upload(diffImagePath, `(<Model.Portrait.IImageValue>casePortrait.value).name}_diff_${new Date().getTime()}`);
            FS.unlinkSync(diffImagePath);
            return Promise.resolve({
                success: false,
                message: `不匹配的像素规模: ${mismatched}`,
                images: { caseImageUrl, baselineImageUrl, diffImageUrl }
            });
        }
        else {
            return Promise.resolve({
                success: true,
                message: ''
            });
        }
    });
}
exports.check = check;
//# sourceMappingURL=snapshot.js.map