"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeReferenceImage = exports.revertReferenceImage = exports.updateReferenceImages = void 0;
const path_1 = __importDefault(require("path"));
const tmp_1 = __importDefault(require("tmp"));
const lodash_1 = __importDefault(require("lodash"));
const common_utils_1 = require("./common-utils");
const utils = __importStar(require("./server-utils"));
const server_utils_1 = require("./server-utils");
const utils_1 = require("./test-adapter/utils");
const constants_1 = require("./constants");
const mkReferenceHash = (testId, stateName) => (0, common_utils_1.getShortMD5)(`${testId}#${stateName}`);
const updateReferenceImages = async (testResult, reportPath, onReferenceUpdateCb) => {
    const newImagesInfo = await Promise.all(testResult.imagesInfo.map(async (imageInfo) => {
        const newImageInfo = lodash_1.default.clone(imageInfo);
        if (!(0, common_utils_1.isImageInfoWithState)(newImageInfo) || newImageInfo.status !== constants_1.UPDATED) {
            return newImageInfo;
        }
        const { stateName } = newImageInfo;
        const { actualImg } = newImageInfo;
        const src = actualImg?.path
            ? path_1.default.resolve(reportPath, actualImg.path)
            : utils.getCurrentAbsolutePath(testResult, reportPath, stateName);
        const referencePath = newImageInfo.refImg.path;
        if (utils.fileExists(referencePath)) {
            const referenceId = mkReferenceHash(testResult.id, stateName);
            const oldReferencePath = path_1.default.resolve(tmp_1.default.tmpdir, referenceId);
            await utils.copyFileAsync(referencePath, oldReferencePath);
        }
        const reportReferencePath = utils.getReferencePath(testResult, stateName);
        await Promise.all([
            utils.copyFileAsync(src, referencePath),
            utils.copyFileAsync(src, path_1.default.resolve(reportPath, reportReferencePath))
        ]);
        const { expectedImg } = newImageInfo;
        expectedImg.path = reportReferencePath;
        onReferenceUpdateCb(testResult, newImageInfo, stateName);
        return newImageInfo;
    }));
    return (0, utils_1.copyAndUpdate)(testResult, { imagesInfo: newImagesInfo });
};
exports.updateReferenceImages = updateReferenceImages;
const revertReferenceImage = async (removedResult, newResult, stateName) => {
    const referenceId = removedResult.id;
    const referenceHash = mkReferenceHash(referenceId, stateName);
    const oldReferencePath = path_1.default.resolve(tmp_1.default.tmpdir, referenceHash);
    const referencePath = (0, server_utils_1.getImagesInfoByStateName)(newResult.imagesInfo, stateName)?.refImg?.path;
    if (!referencePath) {
        return;
    }
    return utils.copyFileAsync(oldReferencePath, referencePath);
};
exports.revertReferenceImage = revertReferenceImage;
const removeReferenceImage = async (testResult, stateName) => {
    const imagePath = (0, server_utils_1.getImagesInfoByStateName)(testResult.imagesInfo, stateName)?.refImg?.path;
    if (!imagePath) {
        return;
    }
    return utils.deleteFile(imagePath);
};
exports.removeReferenceImage = removeReferenceImage;
//# sourceMappingURL=reporter-helpers.js.map