import { normalizeLimit, openReadOnlyDebugDatabase, withStorePath } from "./sql.js";
export function inspectDebugEvents(piboSessionId, store, options = {}) {
    if (!store.exists)
        throw new Error(`Debug store "chat" not found at ${store.defaultPath}`);
    const limit = normalizeLimit(options.limit);
    const db = openReadOnlyDebugDatabase(store);
    try {
        if (!tableExists(db, "web_chat_events"))
            return { piboSessionId, events: [], limited: false };
        const clauses = ["pibo_session_id = ?"];
        const values = [piboSessionId];
        if (options.type) {
            clauses.push("type = ?");
            values.push(options.type);
        }
        const rows = db
            .prepare(`
					SELECT id, event_id, type, created_at, payload_json
					FROM web_chat_events
					WHERE ${clauses.join(" AND ")}
					ORDER BY rowid DESC
					LIMIT ?
				`)
            .all(...values, limit + 1);
        const limited = rows.length > limit;
        return {
            piboSessionId,
            events: rows.slice(0, limit).map((row) => formatEventRow(row, options.fields ?? [])),
            limited,
        };
    }
    catch (error) {
        throw withStorePath(error, store);
    }
    finally {
        db.close();
    }
}
export function formatDebugEvents(result) {
    if (result.events.length === 0)
        return "events: 0";
    const columns = Object.keys(result.events[0] ?? {});
    const lines = [columns.join("\t")];
    for (const event of result.events) {
        lines.push(columns.map((column) => formatValue(event[column])).join("\t"));
    }
    lines.push(`events: ${result.events.length}${result.limited ? " (limited)" : ""}`);
    return lines.join("\n");
}
function formatEventRow(row, fields) {
    const payload = parseObject(row.payload_json);
    const result = {
        created_at: row.created_at,
        type: row.type,
        event_id: row.event_id,
    };
    for (const field of fields) {
        result[field] = getPath(payload, field);
    }
    return result;
}
function getPath(value, path) {
    let current = value;
    for (const part of path.split(".").filter(Boolean)) {
        if (!current || typeof current !== "object" || Array.isArray(current))
            return undefined;
        current = current[part];
    }
    return current;
}
function tableExists(db, table) {
    const row = db.prepare("SELECT name FROM sqlite_schema WHERE type = 'table' AND name = ?").get(table);
    return row !== undefined;
}
function parseObject(value) {
    try {
        const parsed = JSON.parse(value);
        if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
            return {};
        return parsed;
    }
    catch {
        return {};
    }
}
function formatValue(value) {
    if (value === null || value === undefined)
        return "";
    if (typeof value === "object")
        return JSON.stringify(value);
    return String(value).replaceAll("\n", "\\n");
}
