import 'node:module';
import 'node:fs';
import { resolve, join } from 'node:path';
import { execFile } from 'node:child_process';
import { createHash } from 'node:crypto';
import 'node:os';
import { readFile } from 'node:fs/promises';
import { promisify } from 'node:util';

//#region ../core/dist/nexus/identity.js
/**
* Canonical project identity computation (T9149 — W5 N1 Epsilon-unique insight).
*
* Addresses the 80,969-row pollution from cross-provider mount-path divergence
* (e.g. /mnt/projects/cleocode vs /workspace/cleocode both hashing to different
* base64url(path) IDs for the same repo).
*
* `canonicalProjectId` anchors identity to git-root + realpath so container
* bind-mounts, CI clones, and developer laptops all produce the same ID.
*
* @task T9149
* @module nexus/identity
*/
var execFileAsync = promisify(execFile);
/**
* Find the git root for a given directory using `git rev-parse --show-toplevel`.
*
* Returns `null` if the directory is not inside a git repo (non-fatal: allows
* use outside git repos).
*/
async function findGitRoot(fromPath) {
	try {
		const { stdout } = await execFileAsync("git", ["rev-parse", "--show-toplevel"], { cwd: resolve(fromPath) });
		return resolve(stdout.trim());
	} catch {
		return null;
	}
}
/**
* Find the primary git remote URL (origin fetch URL).
*
* Returns `null` when there are no remotes or git is unavailable.
*/
async function findGitRemoteUrl(fromPath) {
	try {
		const { stdout } = await execFileAsync("git", [
			"remote",
			"get-url",
			"origin"
		], { cwd: resolve(fromPath) });
		return stdout.trim() || null;
	} catch {
		return null;
	}
}
/**
* Read the project name from `.cleo/project-info.json` (if present).
* Non-fatal on any I/O or parse error.
*/
async function readProjectInfoName(repoRoot) {
	try {
		const raw = await readFile(join(repoRoot, ".cleo", "project-info.json"), "utf8");
		const parsed = JSON.parse(raw);
		return (typeof parsed.name === "string" ? parsed.name : void 0) || void 0;
	} catch {
		return;
	}
}
/**
* Compute the canonical project ID for a given repository path.
*
* Algorithm:
*   1. Resolve `repoPath` to its `realpath` (resolves symlinks, normalises mounts).
*   2. Detect the git root via `git rev-parse --show-toplevel` (falls back to realpath).
*   3. Read `.cleo/project-info.json` name (optional).
*   4. Read `git remote get-url origin` (optional).
*   5. SHA-256 of `<gitRoot>|<projectName>|<remoteUrl>`, first 12 hex chars.
*
* This ensures `/mnt/projects/cleocode` and `/workspace/cleocode` (same git root,
* same remote) produce the same ID — resolving the 80,969-row pollution vector.
*
* @param repoPath - Absolute path to the project root (may be a symlink or bind-mount).
* @returns The canonical project ID result with components and hash.
*
* @task T9149
*/
async function canonicalProjectId(repoPath) {
	const realRepoPath = resolve(repoPath);
	const [gitRoot, remoteUrl, projectName] = await Promise.all([
		findGitRoot(realRepoPath),
		findGitRemoteUrl(realRepoPath),
		(async () => {
			const gr = await findGitRoot(realRepoPath);
			return gr ? readProjectInfoName(gr) : readProjectInfoName(realRepoPath);
		})()
	]);
	const effectiveRoot = gitRoot ?? realRepoPath;
	const fingerprint = [
		effectiveRoot,
		projectName ?? "",
		remoteUrl ?? ""
	].join("|");
	return {
		id: createHash("sha256").update(fingerprint).digest("hex").substring(0, 12),
		components: {
			gitRoot: effectiveRoot,
			...projectName !== void 0 && { projectName },
			...remoteUrl != null && { remoteUrl }
		}
	};
}

export { canonicalProjectId as c, findGitRemoteUrl as f };
//# sourceMappingURL=identity2-DeAB5RXE.js.map
