import "./jsonl-transcript-io.js";
import { existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
//#region src/session/parity/transcript-pagination.ts
function classifyLine(line) {
	const trimmed = line.trim();
	if (!trimmed) return "other";
	if (trimmed.includes("\"type\":\"session\"") || trimmed.includes("\"type\": \"session\"")) return "session";
	if (trimmed.includes("\"type\":\"message\"") || trimmed.includes("\"type\": \"message\"")) return "message";
	if (trimmed.includes(`"xopc:transcript-row"`) || trimmed.includes("\"type\":\"custom\"")) return "context";
	return "other";
}
function parseStoredRow(line, kind) {
	try {
		const parsed = JSON.parse(line);
		if (kind === "message" && parsed.message) return parsed.message;
		if (kind === "context" && parsed.type === "custom") {
			const data = parsed.data;
			if (!data || data.kind !== "context") return null;
			return {
				kind: "context",
				id: typeof data.id === "string" ? data.id : void 0,
				text: typeof data.text === "string" ? data.text : void 0,
				data: data.data,
				createdAt: typeof data.createdAt === "string" ? data.createdAt : void 0
			};
		}
	} catch {
		return null;
	}
	return null;
}
/**
* Count persisted pi `type: message` rows (excludes context custom rows).
*/
async function countTranscriptMessageRows(absPath) {
	if (!existsSync(absPath)) return 0;
	const content = await readFile(absPath, "utf8");
	let count = 0;
	for (const line of content.split("\n")) if (classifyLine(line) === "message") count += 1;
	return count;
}
function isCompactionSummaryLine(line) {
	return line.includes("[Previous conversation summary]");
}
/**
* Read a page of display messages from transcript tail (newest page when cursors omitted).
*/
async function readDisplayMessagePageFromTranscriptFile(absPath, options) {
	if (!existsSync(absPath)) return {
		rows: [],
		messages: [],
		total: 0,
		startIndex: 0,
		endIndex: 0
	};
	const limit = Math.max(1, Math.trunc(options.limit));
	const content = await readFile(absPath, "utf8");
	const displayLines = [];
	for (const line of content.split("\n")) {
		const kind = classifyLine(line);
		if (kind !== "message") continue;
		if (isCompactionSummaryLine(line)) continue;
		displayLines.push({
			line,
			kind
		});
	}
	const total = displayLines.length;
	const offset = Math.max(0, Math.trunc(options.offset ?? 0));
	const endExclusive = options.beforeIndex !== void 0 ? Math.min(total, Math.max(0, Math.trunc(options.beforeIndex))) : Math.max(0, total - offset);
	const startInclusive = Math.max(0, endExclusive - limit);
	const slice = displayLines.slice(startInclusive, endExclusive);
	const rows = [];
	const messages = [];
	for (const item of slice) {
		const row = parseStoredRow(item.line, item.kind);
		if (row && typeof row.role === "string") {
			rows.push(row);
			messages.push(row);
		}
	}
	return {
		rows,
		messages,
		total,
		startIndex: startInclusive,
		endIndex: endExclusive
	};
}
/**
* Read a page of transcript rows from the tail (newest page when `beforeIndex` omitted).
*/
async function readTranscriptRowsPageFromFile(absPath, options) {
	if (!existsSync(absPath)) return {
		rows: [],
		total: 0,
		startIndex: 0,
		endIndex: 0
	};
	const limit = Math.max(1, Math.trunc(options.limit));
	const content = await readFile(absPath, "utf8");
	const parsedLines = [];
	for (const line of content.split("\n")) {
		const kind = classifyLine(line);
		if (kind === "session" || kind === "other") continue;
		parsedLines.push({
			line,
			kind
		});
	}
	const total = parsedLines.length;
	const offset = Math.max(0, Math.trunc(options.offset ?? 0));
	const endExclusive = options.beforeIndex !== void 0 ? Math.min(total, Math.max(0, Math.trunc(options.beforeIndex))) : Math.max(0, total - offset);
	const startInclusive = Math.max(0, endExclusive - limit);
	const slice = parsedLines.slice(startInclusive, endExclusive);
	const rows = [];
	for (const item of slice) {
		const row = parseStoredRow(item.line, item.kind);
		if (row) rows.push(row);
	}
	return {
		rows,
		total,
		startIndex: startInclusive,
		endIndex: endExclusive
	};
}
//#endregion
export { countTranscriptMessageRows, readDisplayMessagePageFromTranscriptFile, readTranscriptRowsPageFromFile };

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