import { readFileSync } from "node:fs";
import { join } from "node:path";
import JSON5 from "json5";
import { isRecord } from "./parse-helpers.js";
export function resolveSummarizeConfigPath(env) {
    const home = env.HOME?.trim() || env.USERPROFILE?.trim() || null;
    return home ? join(home, ".summarize", "config.json") : null;
}
function assertNoComments(raw, path) {
    let inString = null;
    let escaped = false;
    let line = 1;
    let col = 1;
    for (let i = 0; i < raw.length; i += 1) {
        const ch = raw[i] ?? "";
        const next = raw[i + 1] ?? "";
        if (inString) {
            if (escaped) {
                escaped = false;
                col += 1;
                continue;
            }
            if (ch === "\\") {
                escaped = true;
                col += 1;
                continue;
            }
            if (ch === inString) {
                inString = null;
            }
            if (ch === "\n") {
                line += 1;
                col = 1;
            }
            else {
                col += 1;
            }
            continue;
        }
        if (ch === '"' || ch === "'") {
            inString = ch;
            escaped = false;
            col += 1;
            continue;
        }
        if (ch === "/" && next === "/") {
            throw new Error(`Invalid config file ${path}: comments are not allowed (found // at ${line}:${col}).`);
        }
        if (ch === "/" && next === "*") {
            throw new Error(`Invalid config file ${path}: comments are not allowed (found /* at ${line}:${col}).`);
        }
        if (ch === "\n") {
            line += 1;
            col = 1;
        }
        else {
            col += 1;
        }
    }
}
export function readParsedConfigFile(path) {
    let raw;
    try {
        raw = readFileSync(path, "utf8");
    }
    catch {
        return null;
    }
    let parsed;
    assertNoComments(raw, path);
    try {
        parsed = JSON5.parse(raw);
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        throw new Error(`Invalid JSON in config file ${path}: ${message}`);
    }
    if (!isRecord(parsed)) {
        throw new Error(`Invalid config file ${path}: expected an object at the top level`);
    }
    return parsed;
}
//# sourceMappingURL=read.js.map