"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PENDING_AUTHS = void 0;
exports.coordinateAuth = coordinateAuth;
const logger_1 = __importDefault(require("../logger"));
exports.PENDING_AUTHS = new Map();
function buildCoordinatorKey(host, username, authType) {
    return `${host}:${username}:${authType}`;
}
async function coordinateAuth(host, username, authType, authFn) {
    const key = buildCoordinatorKey(host, username, authType);
    const existing = exports.PENDING_AUTHS.get(key);
    if (existing) {
        (0, logger_1.default)().debug('AuthCoordinator: auth already in progress for key %s, waiting for result', key);
        return existing;
    }
    const promise = authFn();
    promise.catch(() => { });
    exports.PENDING_AUTHS.set(key, promise);
    (0, logger_1.default)().debug('AuthCoordinator: first caller for key %s, running auth flow', key);
    try {
        return await promise;
    }
    finally {
        exports.PENDING_AUTHS.delete(key);
    }
}
//# sourceMappingURL=auth_coordinator.js.map