export function createState(cfg) {
    return {
        cfg,
        running: new Map(),
        claimed: new Set(),
        retry_attempts: new Map(),
        completed: new Set()
    };
}
export function claim(state, taskId) {
    if (state.claimed.has(taskId) ||
        state.retry_attempts.has(taskId) ||
        state.completed.has(taskId)) {
        return false;
    }
    state.claimed.add(taskId);
    return true;
}
export function release(state, taskId) {
    state.claimed.delete(taskId);
    state.running.delete(taskId);
    state.retry_attempts.delete(taskId);
}
export function markRunning(state, entry) {
    if (state.completed.has(entry.taskId)) {
        return;
    }
    state.claimed.add(entry.taskId);
    state.retry_attempts.delete(entry.taskId);
    state.running.set(entry.taskId, entry);
}
export function markCompleted(state, taskId) {
    state.completed.add(taskId);
    release(state, taskId);
}
export function scheduleRetry(state, entry) {
    if (state.completed.has(entry.taskId)) {
        return;
    }
    state.claimed.delete(entry.taskId);
    state.running.delete(entry.taskId);
    state.retry_attempts.set(entry.taskId, entry);
}
export function cancelRetry(state, taskId) {
    state.retry_attempts.delete(taskId);
}
