import { createSubsystemLogger } from "../logging/subsystem.js";
const log = createSubsystemLogger("memory");
export function parseQmdQueryJson(stdout, stderr) {
    const trimmedStdout = stdout.trim();
    const trimmedStderr = stderr.trim();
    const stdoutIsMarker = trimmedStdout.length > 0 && isQmdNoResultsOutput(trimmedStdout);
    const stderrIsMarker = trimmedStderr.length > 0 && isQmdNoResultsOutput(trimmedStderr);
    if (stdoutIsMarker || (!trimmedStdout && stderrIsMarker)) {
        return [];
    }
    if (!trimmedStdout) {
        const context = trimmedStderr ? ` (stderr: ${summarizeQmdStderr(trimmedStderr)})` : "";
        const message = `stdout empty${context}`;
        log.warn(`qmd query returned invalid JSON: ${message}`);
        throw new Error(`qmd query returned invalid JSON: ${message}`);
    }
    try {
        const parsed = JSON.parse(trimmedStdout);
        if (!Array.isArray(parsed)) {
            throw new Error("qmd query JSON response was not an array");
        }
        return parsed;
    }
    catch (err) {
        const message = err instanceof Error ? err.message : String(err);
        log.warn(`qmd query returned invalid JSON: ${message}`);
        throw new Error(`qmd query returned invalid JSON: ${message}`, { cause: err });
    }
}
function isQmdNoResultsOutput(raw) {
    const normalized = raw.trim().toLowerCase().replace(/\s+/g, " ");
    return normalized === "no results found" || normalized === "no results found.";
}
function summarizeQmdStderr(raw) {
    return raw.length <= 120 ? raw : `${raw.slice(0, 117)}...`;
}
