import { isOccAbortError } from '../../errors.js';
import { GitError } from '../../git/client.js';
import { GitPathError } from '../../git/parse.js';
function errorMessage(error) {
    return error instanceof Error ? error.message : String(error);
}
function errorCode(error) {
    return error instanceof Error ? error.code : undefined;
}
function isResourceFailure(error) {
    const code = errorCode(error);
    if (code === 'ENOBUFS' || code === 'ENOMEM' || code === 'EFBIG' || code === 'ERR_FS_FILE_TOO_LARGE')
        return true;
    return /max(?:imum)? buffer|out of memory|allocation failed|buffer (?:is )?too large/i.test(errorMessage(error));
}
/** Preserve the code→git DAG while letting higher layers classify the concrete error. */
export function isGitError(error) {
    return error instanceof GitError;
}
/** One error taxonomy shared by entity diff, at-ref resolution, and blob lineage. */
export function mapEntityGitError(error) {
    if (isOccAbortError(error))
        return { code: 'aborted', message: errorMessage(error) };
    if (error instanceof GitPathError)
        return { code: 'invalid-path', message: error.message };
    if (error instanceof GitError) {
        switch (error.code) {
            case 'bad-ref':
                return { code: 'invalid-ref', message: error.message };
            case 'not-a-repo':
                return { code: 'not-a-repo', message: error.message };
            case 'max-buffer':
                return { code: 'budget-exceeded', message: error.message };
            case 'git-missing':
            case 'merge-base-failed':
            case 'timeout':
            case 'git-failed':
            case 'not-implemented':
                return { code: 'git-failed', message: error.message };
        }
    }
    if (isResourceFailure(error))
        return { code: 'budget-exceeded', message: errorMessage(error) };
    return { code: 'git-failed', message: errorMessage(error) };
}
//# sourceMappingURL=git-error.js.map