// pure peer-brand detection. Extracted from index.ts so the
// "should we leave the system neo4j.service alone?" decision can be unit-
// tested with concrete `.env` fixtures, no fs reads. Mirrors the apt-resolve
// / port-resolution pattern: inputs in, decision out, no I/O.
//
// The installer's `setupDedicatedNeo4j()` wraps this with the fs reads of
// `~/.<peer>/.env` and the `console.error` warning on read failure. This
// module owns only the parsing + matching rule.
/**
 * Decide whether any peer brand on the same host depends on the apt-installed
 * `neo4j.service` (port `defaultNeo4jPort`, conventionally 7687). Returns the
 * first matching peer brand's hostname, or `null` if no peer pins that port.
 *
 * `peerEnvContents` is an iterable of `[hostname, envContent | null]` pairs.
 * `null` content means the peer's `.env` is absent or unreadable — treated
 * the same as "no evidence of dependency". The wrapper logs the read failure;
 * this module stays decision-only.
 *
 * Matching rule: a peer pins the default port iff its `.env` contains a line
 * matching `NEO4J_URI=bolt://localhost:<defaultNeo4jPort>` exactly. Any other
 * scheme, host, or port → not a match. Self-references (peer hostname equal
 * to the current install's hostname) are silently skipped so the caller can
 * pass the full known-brand list without filtering first.
 */
export function findPeerBrandOnDefaultNeo4jPort(args) {
    const { currentBrandHostname, defaultNeo4jPort, peerEnvContents } = args;
    const expected = `bolt://localhost:${defaultNeo4jPort}`;
    for (const [hostname, content] of peerEnvContents) {
        if (hostname === currentBrandHostname)
            continue;
        if (content === null)
            continue;
        const match = content.match(/^NEO4J_URI=(.+)$/m);
        if (match && match[1].trim() === expected) {
            return hostname;
        }
    }
    return null;
}
