"use strict";
/**
 * Copyright: (c) Myia SAS 2026.
 * This file and its contents are licensed under the AGPLv3 License.
 * Please see the LICENSE file at the root of this repository
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetExecutionIdForTesting = exports.getExecutionIdSource = exports.resolveExecutionId = void 0;
// Resolves the per-process executionId once and caches it.
//
// Source priority:
//   1. process.env.HEAL_EXECUTION_ID (set this in CI to make every
//      shard share a single id)
//   2. randomUUID() — deterministic-enough fallback for local runs
//
// The result is memoized so repeat calls within a process return the
// same value, and so multiple Playwright workers spawned by the same
// runner all see one executionId per call (each worker is a separate
// node process; the runner's env is inherited).
const node_crypto_1 = require("node:crypto");
let cached = null;
const ENV_VAR = 'HEAL_EXECUTION_ID';
const compute = () => {
    const fromEnv = process.env[ENV_VAR];
    if (fromEnv && fromEnv.length > 0) {
        return { id: fromEnv, source: 'env' };
    }
    return { id: (0, node_crypto_1.randomUUID)(), source: 'generated' };
};
const resolveExecutionId = () => {
    if (!cached) {
        cached = compute();
    }
    return cached.id;
};
exports.resolveExecutionId = resolveExecutionId;
const getExecutionIdSource = () => {
    if (!cached) {
        cached = compute();
    }
    return cached.source;
};
exports.getExecutionIdSource = getExecutionIdSource;
/**
 * Reset the memoized executionId. Test-only — production code never
 * calls this. Allows unit tests to exercise both env-var and uuid
 * fallback paths within the same process.
 */
const resetExecutionIdForTesting = () => {
    cached = null;
};
exports.resetExecutionIdForTesting = resetExecutionIdForTesting;
