"use strict";
/**
 * Bulk operations on accounts
 * Bulk pause/resume and solo mode functionality
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.soloAccount = exports.bulkResumeAccounts = exports.bulkPauseAccounts = void 0;
const registry_1 = require("./registry");
const query_1 = require("./query");
/** Provider-level locks for preventing race conditions in solo mode */
const providerLocks = new Map();
/**
 * Bulk pause multiple accounts
 * Pauses each account, collecting successes and failures (no fail-fast)
 */
function bulkPauseAccounts(provider, accountIds) {
    const result = { succeeded: [], failed: [] };
    for (const id of accountIds) {
        const success = (0, registry_1.pauseAccount)(provider, id);
        if (success) {
            result.succeeded.push(id);
        }
        else {
            result.failed.push({ id, reason: 'Account not found' });
        }
    }
    return result;
}
exports.bulkPauseAccounts = bulkPauseAccounts;
/**
 * Bulk resume multiple accounts
 * Resumes each account, collecting successes and failures (no fail-fast)
 */
function bulkResumeAccounts(provider, accountIds) {
    const result = { succeeded: [], failed: [] };
    for (const id of accountIds) {
        const success = (0, registry_1.resumeAccount)(provider, id);
        if (success) {
            result.succeeded.push(id);
        }
        else {
            result.failed.push({ id, reason: 'Account not found' });
        }
    }
    return result;
}
exports.bulkResumeAccounts = bulkResumeAccounts;
/**
 * Solo mode: activate one account, pause all others in same provider
 * Per validation: auto-resumes target if paused, keeps default unchanged
 */
async function soloAccount(provider, accountId) {
    // Wait for any pending operation on this provider
    const pending = providerLocks.get(provider);
    if (pending)
        await pending;
    const operation = (async () => {
        const accounts = (0, query_1.getProviderAccounts)(provider);
        const targetAccount = accounts.find((a) => a.id === accountId);
        if (!targetAccount) {
            return null;
        }
        const result = { activated: accountId, paused: [] };
        // Resume target account if paused (per validation decision)
        if (targetAccount.paused) {
            (0, registry_1.resumeAccount)(provider, accountId);
        }
        // Pause all other accounts
        for (const account of accounts) {
            if (account.id !== accountId && !account.paused) {
                const success = (0, registry_1.pauseAccount)(provider, account.id);
                if (success) {
                    result.paused.push(account.id);
                }
            }
        }
        return result;
    })();
    providerLocks.set(provider, operation);
    try {
        return await operation;
    }
    finally {
        providerLocks.delete(provider);
    }
}
exports.soloAccount = soloAccount;
//# sourceMappingURL=bulk-ops.js.map