/**
 * Unified queue backend entry point — selects memory or persistent (SQLite) backend based on runtime mode.
 *
 * Defaults to memory backend (zero dependencies); switches to SQLite when user configures queue.mode = "persistent".
 *
 * Usage:
 *   import { queueBackend, setQueueMode } from './queue-backend.js';
 *   await setQueueMode('persistent');  // Called in preaction.ts based on config
 *   queueBackend().insertTask(...)
 */
import * as memoryBackend from "./queue-memory.js";
let currentMode = "memory";
let persistentBackend = null;
/**
 * Set the queue mode. Should be called in preaction.ts (one-time setup at startup).
 * Dynamically loads the better-sqlite3 module when switching to persistent mode.
 *
 * Note: This function must be called before any enqueue operations.
 */
export async function setQueueMode(mode, dbPath) {
    currentMode = mode;
    if (mode === "persistent" && !persistentBackend) {
        try {
            // Use ESM dynamic import to load the SQLite backend
            const db = await import("./queue-db.js");
            if (dbPath) {
                db.initQueueDB(dbPath);
            }
            else {
                db.initQueueDB();
            }
            persistentBackend = db;
        }
        catch (err) {
            // If better-sqlite3 is unavailable, fall back to memory mode
            currentMode = "memory";
            console.warn(`[queue-backend] Failed to initialize persistent queue, falling back to memory: ${String(err)}`);
        }
    }
}
export function getQueueMode() {
    return currentMode;
}
/**
 * Get the current active queue backend instance.
 */
export function queueBackend() {
    if (currentMode === "persistent" && persistentBackend) {
        return persistentBackend;
    }
    return memoryBackend;
}
/**
 * Reset backend state (used for test isolation only).
 */
export function _resetBackendForTests() {
    currentMode = "memory";
    persistentBackend = null;
}
