/**
 * Git error parsing utility.
 * Maps git stderr patterns to typed errors with user-friendly messages and hints.
 */
export var GitErrorType;
(function (GitErrorType) {
    GitErrorType["AUTH_FAILED"] = "auth_failed";
    GitErrorType["NETWORK_ERROR"] = "network_error";
    GitErrorType["MERGE_CONFLICT"] = "merge_conflict";
    GitErrorType["UNCOMMITTED_CHANGES"] = "uncommitted_changes";
    GitErrorType["BRANCH_EXISTS"] = "branch_exists";
    GitErrorType["BRANCH_NOT_FOUND"] = "branch_not_found";
    GitErrorType["REMOTE_REJECTED"] = "remote_rejected";
    GitErrorType["PROTECTED_BRANCH"] = "protected_branch";
    GitErrorType["DETACHED_HEAD"] = "detached_head";
    GitErrorType["NOTHING_TO_COMMIT"] = "nothing_to_commit";
    GitErrorType["UNKNOWN"] = "unknown";
})(GitErrorType || (GitErrorType = {}));
const GIT_ERROR_PATTERNS = [
    { pattern: /Authentication failed/i, type: GitErrorType.AUTH_FAILED },
    { pattern: /Could not resolve host/i, type: GitErrorType.NETWORK_ERROR },
    { pattern: /Connection refused/i, type: GitErrorType.NETWORK_ERROR },
    { pattern: /CONFLICT/i, type: GitErrorType.MERGE_CONFLICT },
    { pattern: /uncommitted changes/i, type: GitErrorType.UNCOMMITTED_CHANGES },
    { pattern: /already exists/i, type: GitErrorType.BRANCH_EXISTS },
    { pattern: /did not match any/i, type: GitErrorType.BRANCH_NOT_FOUND },
    { pattern: /rejected.*protected branch/i, type: GitErrorType.PROTECTED_BRANCH },
    { pattern: /\[rejected\]/i, type: GitErrorType.REMOTE_REJECTED },
    { pattern: /nothing to commit/i, type: GitErrorType.NOTHING_TO_COMMIT },
];
function getErrorMessage(type) {
    switch (type) {
        case GitErrorType.AUTH_FAILED:
            return "Authentication failed";
        case GitErrorType.NETWORK_ERROR:
            return "Network error - unable to reach remote";
        case GitErrorType.MERGE_CONFLICT:
            return "Merge conflict detected";
        case GitErrorType.UNCOMMITTED_CHANGES:
            return "You have uncommitted changes";
        case GitErrorType.BRANCH_EXISTS:
            return "Branch already exists";
        case GitErrorType.BRANCH_NOT_FOUND:
            return "Branch not found";
        case GitErrorType.REMOTE_REJECTED:
            return "Push was rejected by remote";
        case GitErrorType.PROTECTED_BRANCH:
            return "Cannot push to protected branch";
        case GitErrorType.DETACHED_HEAD:
            return "HEAD is detached";
        case GitErrorType.NOTHING_TO_COMMIT:
            return "Nothing to commit";
        case GitErrorType.UNKNOWN:
            return "An unknown git error occurred";
    }
}
function getErrorHint(type) {
    switch (type) {
        case GitErrorType.AUTH_FAILED:
            return "Check your credentials or SSH key configuration";
        case GitErrorType.NETWORK_ERROR:
            return "Check your internet connection and remote URL";
        case GitErrorType.MERGE_CONFLICT:
            return "Resolve conflicts manually, then stage and commit";
        case GitErrorType.UNCOMMITTED_CHANGES:
            return "Commit or stash your changes before proceeding";
        case GitErrorType.BRANCH_EXISTS:
            return "Use a different branch name or delete the existing branch first";
        case GitErrorType.BRANCH_NOT_FOUND:
            return "Check the branch name and try again";
        case GitErrorType.REMOTE_REJECTED:
            return "Pull the latest changes and try again";
        case GitErrorType.PROTECTED_BRANCH:
            return "Create a feature branch and submit a pull request instead";
        case GitErrorType.DETACHED_HEAD:
            return "Create a new branch to save your work";
        case GitErrorType.NOTHING_TO_COMMIT:
            return "Stage changes with git add before committing";
        default:
            return undefined;
    }
}
/**
 * Parse git stderr output into a typed error with user-friendly message and hint.
 */
export function parseGitError(stderr) {
    for (const { pattern, type } of GIT_ERROR_PATTERNS) {
        if (pattern.test(stderr)) {
            return {
                type,
                message: getErrorMessage(type),
                hint: getErrorHint(type),
            };
        }
    }
    return { type: GitErrorType.UNKNOWN, message: stderr };
}
//# sourceMappingURL=error-parser.js.map