import { init_write_file_atomic, writeTextAtomicSync } from "../../infra/write-file-atomic.js";
import { randomUUID } from "node:crypto";
import { join } from "path";
import { existsSync, mkdirSync, readFileSync } from "fs";
//#region src/channels/outbound/persist-store.ts
/**
* Durable outbound queue (crash recovery): JSON file under agent internal dir.
*/
init_write_file_atomic();
var OutboundPersistStore = class {
	filePath;
	pending = [];
	constructor(agentDir) {
		mkdirSync(agentDir, { recursive: true });
		this.filePath = join(agentDir, "outbound-pending.json");
		this.load();
	}
	load() {
		if (!existsSync(this.filePath)) return;
		try {
			const raw = readFileSync(this.filePath, "utf-8");
			const data = JSON.parse(raw);
			if (Array.isArray(data)) this.pending = data;
		} catch {
			this.pending = [];
		}
	}
	flush() {
		writeTextAtomicSync(this.filePath, JSON.stringify(this.pending));
	}
	enqueue(message) {
		const id = randomUUID();
		this.pending.push({
			id,
			enqueuedAt: Date.now(),
			message
		});
		this.flush();
		return id;
	}
	ack(id) {
		const i = this.pending.findIndex((p) => p.id === id);
		if (i >= 0) {
			this.pending.splice(i, 1);
			this.flush();
		}
	}
	peek() {
		return this.pending;
	}
};
//#endregion
export { OutboundPersistStore };

//# sourceMappingURL=persist-store.js.map