"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toRelativeTestFile = toRelativeTestFile;
exports.isValidTestLocation = isValidTestLocation;
exports.normalizeTestContext = normalizeTestContext;
exports.setGlobalTestContext = setGlobalTestContext;
exports.getGlobalTestContext = getGlobalTestContext;
const os_1 = __importDefault(require("os"));
const createDebugger_1 = __importDefault(require("../createDebugger"));
const debugLogger = (0, createDebugger_1.default)('testContext');
function toRelativeTestFile(testFile) {
    if (!testFile || typeof testFile !== 'string') {
        return null;
    }
    const home = os_1.default.homedir().replaceAll('\\', '/');
    const normalized = testFile.replaceAll('\\', '/');
    if (normalized.startsWith(home + '/')) {
        return normalized.slice(home.length + 1);
    }
    return normalized;
}
function isValidTestLocation(loc) {
    return (Array.isArray(loc) &&
        loc.length > 0 &&
        loc.every(segment => typeof segment === 'string'));
}
function normalizeTestContext(ctx) {
    if (!ctx || typeof ctx !== 'object') {
        if (ctx) {
            debugLogger('dropped ctx: expected a plain TestContext object');
        }
        return null;
    }
    if (ctx.test_file_path && toRelativeTestFile(ctx.test_file_path) === null) {
        debugLogger('dropped test_file_path: expected a non-empty string');
    }
    if (ctx.test_location && !isValidTestLocation(ctx.test_location)) {
        debugLogger('dropped test_location: expected a non-empty array of strings');
    }
    return {
        test_file_path: toRelativeTestFile(ctx.test_file_path),
        test_location: isValidTestLocation(ctx.test_location)
            ? [...ctx.test_location]
            : null
    };
}
let globalTestContext = null;
function setGlobalTestContext(ctx) {
    globalTestContext = normalizeTestContext(ctx);
}
function getGlobalTestContext() {
    return globalTestContext;
}
//# sourceMappingURL=testContext.js.map