"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SqliteTestAdapter = void 0;
const lodash_1 = __importDefault(require("lodash"));
const constants_1 = require("../constants");
const common_utils_1 = require("../common-utils");
const tryParseJson = (json) => {
    try {
        return JSON.parse(json);
    }
    catch {
        return undefined;
    }
};
class SqliteTestAdapter {
    constructor(testResult, attempt, options) {
        this._testResult = testResult;
        this._parsedTestResult = { attempt };
        this._titleDelimiter = options.titleDelimiter;
    }
    get assertViewResults() {
        // TODO: try to restore assertViewResults from imagesInfo
        return [];
    }
    get attempt() {
        return this._parsedTestResult.attempt;
    }
    get browserId() {
        return this._testResult[constants_1.DB_COLUMN_INDEXES.name];
    }
    get description() {
        return this._testResult[constants_1.DB_COLUMN_INDEXES.description] ?? undefined;
    }
    get error() {
        if (!lodash_1.default.has(this._parsedTestResult, 'error')) {
            this._parsedTestResult.error = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.error]);
        }
        return this._parsedTestResult.error;
    }
    get errorDetails() {
        // TODO: implement returning error details
        return null;
    }
    get file() {
        if (!lodash_1.default.has(this._parsedTestResult, 'meta')) {
            this._parsedTestResult.meta = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.metaInfo]);
        }
        return this._parsedTestResult.meta?.file;
    }
    get fullName() {
        if (!lodash_1.default.has(this._parsedTestResult, 'fullName')) {
            this._parsedTestResult.fullName = this.testPath.join(this._titleDelimiter);
        }
        return this._parsedTestResult.fullName;
    }
    get history() {
        if (!lodash_1.default.has(this._parsedTestResult, 'history')) {
            this._parsedTestResult.history = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.history]);
        }
        return this._parsedTestResult.history;
    }
    get id() {
        return this.testPath.concat(this.browserId, this.attempt.toString()).join(' ');
    }
    get imageDir() {
        return (0, common_utils_1.getTestHash)(this);
    }
    get imagesInfo() {
        if (!lodash_1.default.has(this._parsedTestResult, 'imagesInfo')) {
            this._parsedTestResult.imagesInfo = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.imagesInfo]);
        }
        return this._parsedTestResult.imagesInfo;
    }
    get meta() {
        if (!lodash_1.default.has(this._parsedTestResult, 'meta')) {
            this._parsedTestResult.meta = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.metaInfo]) ?? {};
        }
        return this._parsedTestResult.meta;
    }
    get multipleTabs() {
        return Boolean(this._testResult[constants_1.DB_COLUMN_INDEXES.multipleTabs]);
    }
    get screenshot() {
        return this.error?.screenshot;
    }
    get sessionId() {
        if (!lodash_1.default.has(this._parsedTestResult, 'meta')) {
            this._parsedTestResult.meta = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.metaInfo]);
        }
        return this._parsedTestResult.meta?.sessionId;
    }
    get skipReason() {
        return this._testResult[constants_1.DB_COLUMN_INDEXES.skipReason];
    }
    get state() {
        return { name: this.testPath.at(-1) };
    }
    get status() {
        return this._testResult[constants_1.DB_COLUMN_INDEXES.status];
    }
    get testPath() {
        if (!lodash_1.default.has(this._parsedTestResult, 'testPath')) {
            this._parsedTestResult.testPath = tryParseJson(this._testResult[constants_1.DB_COLUMN_INDEXES.suitePath]);
        }
        return this._parsedTestResult.testPath;
    }
    get timestamp() {
        return Number(this._testResult[constants_1.DB_COLUMN_INDEXES.timestamp]);
    }
    get url() {
        return this._testResult[constants_1.DB_COLUMN_INDEXES.suiteUrl];
    }
}
exports.SqliteTestAdapter = SqliteTestAdapter;
//# sourceMappingURL=sqlite.js.map