"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestplaneTestAdapter = exports.getStatus = void 0;
const path_1 = __importDefault(require("path"));
const lodash_1 = __importDefault(require("lodash"));
const history_utils_1 = require("../history-utils");
const constants_1 = require("../constants");
const common_utils_1 = require("../common-utils");
const plugin_utils_1 = require("../plugin-utils");
const utils_1 = require("./utils");
const getStatus = (eventName, events, testResult) => {
    if (eventName === events.TEST_PASS) {
        return constants_1.TestStatus.SUCCESS;
    }
    else if (eventName === events.TEST_PENDING) {
        return constants_1.TestStatus.SKIPPED;
    }
    else if (eventName === events.RETRY || eventName === events.TEST_FAIL) {
        return (0, common_utils_1.hasFailedImages)(testResult.assertViewResults) ? constants_1.TestStatus.FAIL : constants_1.TestStatus.ERROR;
    }
    else if (eventName === events.TEST_BEGIN) {
        return constants_1.TestStatus.RUNNING;
    }
    return constants_1.TestStatus.IDLE;
};
exports.getStatus = getStatus;
const getSkipComment = (suite) => {
    return suite.skipReason || suite.parent && getSkipComment(suite.parent);
};
const wrapSkipComment = (skipComment) => {
    return skipComment ? (0, common_utils_1.wrapLinkByTag)(skipComment) : 'Unknown reason';
};
class TestplaneTestAdapter {
    static create(testResult, options) {
        return new this(testResult, options);
    }
    constructor(testResult, { attempt, status }) {
        this._testResult = testResult;
        this._errorDetails = null;
        this._timestamp = this._testResult.timestamp ??
            this._testResult.startTime ?? Date.now();
        this._status = status;
        const browserVersion = lodash_1.default.get(this._testResult, 'meta.browserVersion', this._testResult.browserVersion);
        lodash_1.default.set(this._testResult, 'meta.browserVersion', browserVersion);
        this._attempt = attempt;
    }
    get fullName() {
        return this._testResult.fullTitle();
    }
    get skipReason() {
        return wrapSkipComment(getSkipComment(this._testResult));
    }
    get status() {
        return this._status;
    }
    get sessionId() {
        return this._testResult.sessionId || constants_1.UNKNOWN_SESSION_ID;
    }
    get browserId() {
        return this._testResult.browserId;
    }
    get imagesInfo() {
        const { assertViewResults = [] } = this._testResult;
        const imagesInfo = assertViewResults.map((assertResult) => {
            if ((0, common_utils_1.isImageDiffError)(assertResult)) {
                const diffBufferImg = assertResult.diffBuffer ? { buffer: assertResult.diffBuffer } : undefined;
                const diffImg = assertResult.diffImg ?? diffBufferImg;
                return {
                    status: constants_1.FAIL,
                    stateName: assertResult.stateName,
                    refImg: assertResult.refImg,
                    actualImg: assertResult.currImg,
                    ...(diffImg ? { diffImg } : {}),
                    expectedImg: lodash_1.default.clone(assertResult.refImg),
                    diffClusters: assertResult.diffClusters,
                    diffOptions: assertResult.diffOpts,
                    differentPixels: assertResult.differentPixels,
                    diffRatio: assertResult.diffRatio
                };
            }
            else if ((0, common_utils_1.isNoRefImageError)(assertResult)) {
                return {
                    status: constants_1.ERROR,
                    stateName: assertResult.stateName,
                    error: lodash_1.default.pick(assertResult, ['message', 'name', 'stack']),
                    refImg: assertResult.refImg,
                    actualImg: assertResult.currImg
                };
            }
            else if (assertResult.isUpdated) {
                return {
                    status: constants_1.UPDATED,
                    stateName: assertResult.stateName,
                    refImg: assertResult.refImg,
                    expectedImg: lodash_1.default.clone(assertResult.refImg),
                    actualImg: assertResult.currImg
                };
            }
            else {
                const { currImg } = assertResult;
                return {
                    status: constants_1.SUCCESS,
                    stateName: assertResult.stateName,
                    refImg: assertResult.refImg,
                    expectedImg: lodash_1.default.clone(assertResult.refImg),
                    ...(currImg ? { actualImg: currImg } : {})
                };
            }
        });
        if (this.screenshot) {
            imagesInfo.push({
                status: lodash_1.default.isEmpty((0, common_utils_1.getError)(this.error)) ? constants_1.SUCCESS : constants_1.ERROR,
                actualImg: this.screenshot
            });
        }
        return imagesInfo;
    }
    get attempt() {
        return this._attempt;
    }
    get history() {
        return (0, history_utils_1.getCommandsHistory)(this._testResult.history);
    }
    get error() {
        return this._testResult.err;
    }
    get imageDir() {
        // TODO: remove toString after publish major version
        return this._testResult.id.toString();
    }
    get state() {
        return { name: this._testResult.title };
    }
    get testPath() {
        return (0, plugin_utils_1.getSuitePath)(this._testResult.parent).concat(this._testResult.title);
    }
    get id() {
        return this.testPath.concat(this.browserId, this.attempt.toString()).join(' ');
    }
    get screenshot() {
        return lodash_1.default.get(this._testResult, 'err.screenshot');
    }
    get description() {
        return this._testResult.description;
    }
    get meta() {
        return this._testResult.meta ?? {};
    }
    get errorDetails() {
        if (!lodash_1.default.isNil(this._errorDetails)) {
            return this._errorDetails;
        }
        this._errorDetails = (0, utils_1.extractErrorDetails)(this);
        return this._errorDetails;
    }
    get file() {
        return path_1.default.relative(process.cwd(), this._testResult.file);
    }
    get url() {
        return this._testResult.meta?.url;
    }
    get multipleTabs() {
        return true;
    }
    get timestamp() {
        return this._timestamp;
    }
    set timestamp(timestamp) {
        if (!lodash_1.default.isNumber(this._timestamp)) {
            this._timestamp = timestamp;
        }
    }
}
exports.TestplaneTestAdapter = TestplaneTestAdapter;
//# sourceMappingURL=testplane.js.map