const driverRegistry = new Map();
/**
 * Returns the existing driver for the given key, or calls the factory to create
 * and register a new one. `agentsKey` is the fingerprint of the agents the
 * driver is being built for; it is recorded only when a driver is created (a
 * reused driver keeps the fingerprint it was originally built with).
 */
export function getOrCreateDriver(key, agentsKey, factory) {
    const existing = driverRegistry.get(key);
    if (existing)
        return existing.driver;
    const driver = factory();
    driverRegistry.set(key, { driver, agentsKey });
    return driver;
}
/**
 * Returns the existing driver for the given key, or `undefined`.
 */
export function getDriver(key) {
    var _a;
    return (_a = driverRegistry.get(key)) === null || _a === void 0 ? void 0 : _a.driver;
}
/**
 * The agents fingerprint the registry driver for `key` was built with, or
 * `undefined` if no driver is registered. Used by `agentsChanged` to decide
 * whether a sibling has already rebuilt the shared driver to the desired agents.
 */
export function getDriverAgentsKey(key) {
    var _a;
    return (_a = driverRegistry.get(key)) === null || _a === void 0 ? void 0 : _a.agentsKey;
}
/**
 * Removes and returns the driver for the given key.
 * Used when agents change (driver must be rebuilt) or on explicit session teardown.
 */
export function deleteDriver(key) {
    const entry = driverRegistry.get(key);
    driverRegistry.delete(key);
    return entry === null || entry === void 0 ? void 0 : entry.driver;
}
/**
 * Removes all entries. Exposed for test isolation only — not part of the public API.
 *
 * @internal
 */
export function clearDriverRegistry() {
    driverRegistry.clear();
}
