import { lstatSync, readFileSync } from "node:fs";
import path from "node:path";
import { hasOwnErrorCode } from "./error-codes.js";
function isHookHandler(value) {
    return typeof value === "object" && value !== null && !Array.isArray(value);
}
function readSettingsFile(filePath) {
    let content;
    try {
        if (lstatSync(filePath).isSymbolicLink()) {
            throw new Error(`Hook settings path must not be a symbolic link: ${filePath}`);
        }
        content = readFileSync(filePath, "utf8");
    }
    catch (error) {
        if (hasOwnErrorCode(error, "ENOENT")) {
            return undefined;
        }
        throw error;
    }
    try {
        return JSON.parse(content);
    }
    catch (error) {
        throw new Error(`Malformed JSON in ${filePath}`, { cause: error });
    }
}
export function readClaudeHooks(cwd, homeDir, opts) {
    const projectPath = path.resolve(cwd, ".claude/settings.json");
    const userPath = path.resolve(homeDir, ".claude/settings.json");
    const scope = opts?.scope ?? "merged";
    const sourcePaths = scope === "project" ? [projectPath] : scope === "user" ? [userPath] : [userPath, projectPath];
    const result = { entries: [], readPaths: [] };
    for (const sourcePath of sourcePaths) {
        const settings = readSettingsFile(sourcePath);
        if (settings === undefined) {
            continue;
        }
        result.readPaths.push(sourcePath);
        for (const [event, groups] of Object.entries(settings.hooks ?? {})) {
            if (!Array.isArray(groups)) {
                throw new Error(`Malformed hooks in ${sourcePath}`);
            }
            for (const group of groups) {
                if (!group || !Array.isArray(group.hooks)) {
                    throw new Error(`Malformed hooks in ${sourcePath}`);
                }
                for (const handler of group.hooks) {
                    if (!isHookHandler(handler)) {
                        throw new Error(`Malformed hooks in ${sourcePath}`);
                    }
                    result.entries.push({ event, matcher: group.matcher, handler });
                }
            }
        }
    }
    return result;
}
