import { existsSync, readFileSync, statSync } from 'node:fs';
import { isAbsolute, join, resolve } from 'node:path';
const CONFLICT_PORCELAIN_PREFIXES = ['UU', 'AA', 'DD', 'AU', 'UA', 'DU', 'UD'];
function readGitDir(worktreePath) {
    const gitPointerPath = join(worktreePath, '.git');
    if (!existsSync(gitPointerPath))
        return null;
    try {
        const stat = statSync(gitPointerPath);
        if (stat.isDirectory())
            return gitPointerPath;
        const content = readFileSync(gitPointerPath, 'utf-8').trim();
        const match = content.match(/^gitdir:\s*(.+)$/);
        if (!match)
            return null;
        const target = match[1];
        if (target === undefined)
            return null;
        return isAbsolute(target) ? target : resolve(worktreePath, target);
    }
    catch {
        return null;
    }
}
function detectOrphanLock(gitDir, now) {
    for (const lockName of ['index.lock', 'HEAD.lock']) {
        const lockPath = join(gitDir, lockName);
        if (!existsSync(lockPath))
            continue;
        try {
            const stat = statSync(lockPath);
            const ageMs = Math.max(0, now.getTime() - stat.mtime.getTime());
            return { present: true, path: lockPath, ageMs };
        }
        catch {
            return null;
        }
    }
    return null;
}
function detectConflict(stdout) {
    const lines = stdout.split('\n');
    for (const line of lines) {
        const head = line.slice(0, 2);
        if (CONFLICT_PORCELAIN_PREFIXES.includes(head))
            return true;
    }
    return false;
}
function readMtime(worktreePath, fallback) {
    try {
        return statSync(worktreePath).mtime;
    }
    catch {
        return fallback;
    }
}
export class WorktreeHealthProbeFileSystemGateway {
    executor;
    now;
    constructor(deps) {
        this.executor = deps.executor;
        this.now = deps.now ?? (() => new Date());
    }
    async probe(entry) {
        const now = this.now();
        const mtime = readMtime(entry.path, entry.mtime);
        const gitDir = readGitDir(entry.path);
        const orphanLock = gitDir === null ? null : detectOrphanLock(gitDir, now);
        let unresolvedConflict = false;
        try {
            const statusResult = await this.executor.execute({
                kind: 'status-porcelain',
                args: ['status', '--porcelain=v1'],
                cwd: entry.path,
            });
            if (statusResult.exitCode === 0) {
                unresolvedConflict = detectConflict(statusResult.stdout);
            }
        }
        catch {
            unresolvedConflict = false;
        }
        return {
            mtime,
            orphanLock,
            unresolvedConflict,
        };
    }
}
//# sourceMappingURL=worktreeHealthProbe.fileSystem.gateway.js.map