import { DurableTaskQueue } from "./queue.js";
export class DurableTaskWorker {
    state;
    claimant;
    queue;
    constructor(state, claimant = `worker_${process.pid}`) {
        this.state = state;
        this.claimant = claimant;
        this.queue = new DurableTaskQueue(state);
    }
    async runOne(runId, handler) {
        const task = this.queue.claimNext(runId, this.claimant);
        if (!task) {
            return { status: "idle", message: "no runnable task" };
        }
        try {
            const detail = await handler(task);
            this.queue.complete(task.id, detail || "completed");
            this.state.appendEvent(runId, "worker_step_completed", {
                task_id: task.id,
                claimant: this.claimant,
            });
            return { claimed: task, status: "completed", message: detail || "completed" };
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            this.queue.fail(task.id, message);
            this.state.appendEvent(runId, "worker_step_failed", {
                task_id: task.id,
                claimant: this.claimant,
                message,
            });
            return { claimed: task, status: "failed", message };
        }
    }
}
