import { __esmMin } from "../../../_virtual/_rolldown/runtime.js";
import { formatSessionArchiveTimestamp, init_artifacts } from "./artifacts.js";
import { init_session_id, validateSessionId } from "./session-id.js";
import fs from "node:fs";
import { dirname, relative, resolve } from "node:path";
//#region src/session/parity/transcript-paths.ts
function resolveSessionsDir(opts) {
	const sessionsDir = opts?.sessionsDir?.trim();
	if (!sessionsDir) throw new Error("sessionsDir is required for session transcript path resolution");
	return resolve(sessionsDir);
}
function resolvePathWithinSessionsDir(sessionsDir, candidate) {
	const trimmed = candidate.trim();
	if (!trimmed) throw new Error("Session file path must not be empty");
	const resolvedBase = resolve(sessionsDir);
	const normalized = resolve(resolvedBase, trimmed);
	const rel = relative(resolvedBase, normalized);
	if (rel.startsWith("..") || rel === "") throw new Error("Session file path must be within sessions directory");
	return normalized;
}
function resolveSessionTranscriptPathInDir(sessionId, sessionsDir, topicId) {
	const safeSessionId = validateSessionId(sessionId);
	const safeTopicId = typeof topicId === "string" ? encodeURIComponent(topicId) : typeof topicId === "number" ? String(topicId) : void 0;
	return resolvePathWithinSessionsDir(sessionsDir, safeTopicId !== void 0 ? `${safeSessionId}-topic-${safeTopicId}.jsonl` : `${safeSessionId}.jsonl`);
}
function resolveSessionFilePath(sessionId, entry, opts) {
	const sessionsDir = resolveSessionsDir(opts);
	const candidate = entry?.sessionFile?.trim();
	if (candidate) try {
		return resolvePathWithinSessionsDir(sessionsDir, candidate);
	} catch {}
	return resolveSessionTranscriptPathInDir(sessionId, sessionsDir);
}
/**
* Candidate transcript paths for a session (xopc state only — no external legacy dirs).
*/
function resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, _agentId) {
	const candidates = [];
	const push = (p) => {
		if (!candidates.includes(p)) candidates.push(p);
	};
	if (storePath) {
		const sessionsDir = dirname(storePath);
		if (sessionFile?.trim()) try {
			push(resolveSessionFilePath(sessionId, { sessionFile }, { sessionsDir }));
		} catch {}
		push(resolveSessionTranscriptPathInDir(sessionId, sessionsDir));
	} else if (sessionFile?.trim()) push(resolve(sessionFile.trim()));
	return candidates;
}
function archiveFileOnDisk(filePath, reason) {
	const archived = `${filePath}.${reason}.${formatSessionArchiveTimestamp()}`;
	fs.renameSync(filePath, archived);
	return archived;
}
var init_transcript_paths = __esmMin((() => {
	init_artifacts();
	init_session_id();
}));
//#endregion
init_transcript_paths();
export { archiveFileOnDisk, init_transcript_paths, resolveSessionFilePath, resolveSessionTranscriptCandidates, resolveSessionTranscriptPathInDir };

//# sourceMappingURL=transcript-paths.js.map