// src/server/render-queue/index.ts
import Database from "better-sqlite3";
import { mkdirSync } from "node:fs";
import { dirname } from "node:path";
import { RenderQueueStore, defaultDbPath } from "./store.js";
import { RenderQueueWorker, } from "./worker.js";
export { defaultDbPath };
export class RenderQueue {
    db;
    store;
    worker;
    // Phase H (#35) — in-memory side channel for runtime-only options that
    // don't belong in the persistent SQLite row (captionTracks is a UI-driven
    // ephemeral choice — not a property of the job itself). We forward it to
    // the worker via this map so the existing store schema stays untouched.
    // Entries are cleared when the worker terminates a job. Survives only
    // until process restart; that's intentional — replays after restart fall
    // back to the legacy single-track render path.
    extras = new Map();
    constructor(opts) {
        const path = opts.dbPath ?? defaultDbPath();
        if (path !== ":memory:") {
            try {
                mkdirSync(dirname(path), { recursive: true });
            }
            catch {
                /* ok */
            }
        }
        this.db = new Database(path);
        this.store = new RenderQueueStore(this.db);
        this.worker = new RenderQueueWorker({
            store: this.store,
            runRenderPipeline: opts.runRenderPipeline,
            loadComposition: opts.loadComposition,
            outDirFor: opts.outDirFor,
            concurrency: opts.concurrency,
            extrasFor: (jobId) => this.extras.get(jobId),
            clearExtras: (jobId) => this.extras.delete(jobId),
        });
        this.worker.start();
    }
    enqueue(opts) {
        const job = this.store.insert(opts);
        // Only remember extras when the caller actually populated runtime-only
        // fields — empty map is the cheap path for the legacy single-track UI.
        if (opts.captionTracks || opts.burnSubtitles || opts.loudnessTargetLufs) {
            this.extras.set(job.id, opts);
        }
        this.worker.notify();
        return job;
    }
    cancel(jobId) {
        this.worker.cancel(jobId);
    }
    get(jobId) {
        return this.store.get(jobId);
    }
    list(workId) {
        return this.store.list(workId);
    }
    on(jobId, fn) {
        return this.worker.on(jobId, fn);
    }
    /** Test helper / shutdown — stop the worker drain loop and close db. */
    shutdown() {
        this.worker.stop();
        this.db.close();
    }
}
//# sourceMappingURL=index.js.map