/**
 * Minimal neo4j driver wrapper for the writer-craft MCP server.
 *
 * Mirrors the public surface of `platform/plugins/memory/mcp/src/lib/neo4j.ts`:
 * `getSession()` returns a session, `closeDriver()` releases the singleton.
 * Password resolution follows the same `PLATFORM_ROOT/config/.neo4j-password`
 * convention, with an explicit `NEO4J_URI` requirement to avoid silent
 * cross-account orphaning.
 */
import neo4j from "neo4j-driver";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
let driver = null;
function readPassword() {
    if (process.env.NEO4J_PASSWORD)
        return process.env.NEO4J_PASSWORD;
    const passwordFile = resolve(process.env.PLATFORM_ROOT ?? resolve(import.meta.dirname, "../../../../../platform"), "config/.neo4j-password");
    try {
        return readFileSync(passwordFile, "utf-8").trim();
    }
    catch {
        throw new Error(`Neo4j password not found. Expected at ${passwordFile} or in NEO4J_PASSWORD env var.`);
    }
}
export function getDriver() {
    if (!driver) {
        const uri = process.env.NEO4J_URI;
        if (!uri) {
            throw new Error("[writer-craft] NEO4J_URI unset — refusing to default to bolt://localhost:7687");
        }
        const user = process.env.NEO4J_USER ?? "neo4j";
        const password = readPassword();
        process.stderr.write(`[writer-craft] resolved neo4j_uri=${uri}\n`);
        driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
    }
    return driver;
}
export function getSession() {
    return getDriver().session();
}
export async function closeDriver() {
    if (driver) {
        await driver.close();
        driver = null;
    }
}
//# sourceMappingURL=neo4j.js.map