let nextId = 1;
const tasks = new Map();
export function insertTask(lane, taskType, payload) {
    const id = nextId++;
    const now = Date.now();
    tasks.set(id, {
        id,
        lane,
        task_type: taskType,
        payload: JSON.stringify(payload),
        status: "PENDING",
        error_msg: null,
        result: null,
        retry_count: 0,
        created_at: now,
        updated_at: now,
    });
    return id;
}
export function claimNextPendingTask(lane) {
    for (const task of tasks.values()) {
        if (task.lane === lane && task.status === "PENDING") {
            task.status = "RUNNING";
            task.updated_at = Date.now();
            return { ...task };
        }
    }
    return null;
}
export function resolveTask(id, result) {
    const task = tasks.get(id);
    if (task) {
        task.status = "COMPLETED";
        task.updated_at = Date.now();
        task.result = result !== undefined ? JSON.stringify(result) : null;
    }
}
export function rejectTask(id, errorMsg) {
    const task = tasks.get(id);
    if (task) {
        task.status = "FAILED";
        task.error_msg = errorMsg;
        task.updated_at = Date.now();
    }
}
export function countQueueByStatus(lane, status) {
    let count = 0;
    for (const task of tasks.values()) {
        if (lane && task.lane !== lane)
            continue;
        if (status) {
            if (task.status !== status)
                continue;
        }
        else {
            // Default: only count PENDING + RUNNING (consistent with DB backend)
            if (task.status !== "PENDING" && task.status !== "RUNNING")
                continue;
        }
        count++;
    }
    return count;
}
export function countTotalQueue() {
    let count = 0;
    for (const task of tasks.values()) {
        if (task.status === "PENDING" || task.status === "RUNNING")
            count++;
    }
    return count;
}
export function clearLaneTasks(lane) {
    let removed = 0;
    for (const [id, task] of tasks.entries()) {
        if (task.lane === lane && task.status === "PENDING") {
            tasks.delete(id);
            removed++;
        }
    }
    return removed;
}
/**
 * Get the list of PENDING task IDs for a given lane (called before clearLaneTasks to reject in-memory Promises).
 */
export function getPendingTaskIdsForLane(lane) {
    const ids = [];
    for (const task of tasks.values()) {
        if (task.lane === lane && task.status === "PENDING") {
            ids.push(task.id);
        }
    }
    return ids;
}
export function hasActiveTasks() {
    for (const task of tasks.values()) {
        if (task.status === "RUNNING")
            return true;
    }
    return false;
}
export function recoverRunningTasks() {
    const affectedLanes = new Set();
    for (const task of tasks.values()) {
        if (task.status === "RUNNING") {
            affectedLanes.add(task.lane);
            task.status = "PENDING";
            task.updated_at = Date.now();
        }
    }
    return Array.from(affectedLanes);
}
export function getTaskResult(id) {
    const task = tasks.get(id);
    if (!task)
        return null;
    return {
        status: task.status,
        result: task.result ? JSON.parse(task.result) : null,
        error_msg: task.error_msg,
    };
}
export function getPendingLanes() {
    const lanes = new Set();
    for (const task of tasks.values()) {
        if (task.status === "PENDING")
            lanes.add(task.lane);
    }
    return Array.from(lanes);
}
export function markStaleTasks(_reason) {
    // In memory mode, data is lost after process restart; no cleanup needed
    return 0;
}
export function getRecoverableTasks() {
    // In memory mode, cross-process recovery is not possible
    return [];
}
/**
 * Reset all in-memory state (used for test isolation only).
 */
export function reset() {
    tasks.clear();
    nextId = 1;
}
