"""Iteration-pass helpers for session commands."""

from __future__ import annotations

from ...data.file_tracking import auto_add_untracked_files
from ...data.hunk_tracking import fetch_next_change
from ...data.session import clear_iteration_state, increment_iteration_count
from ...exceptions import NoMoreHunks
from ...i18n import _
from ..selection.selected_change_display import show_selected_change


def restart_iteration_pass(
    *,
    current_iteration: int,
    quiet: bool = False,
) -> None:
    """Clear iteration state and select the first available change."""
    clear_iteration_state()
    increment_iteration_count(current_iteration)
    auto_add_untracked_files(show_progress=not quiet)

    try:
        fetch_next_change()
    except NoMoreHunks:
        if not quiet:
            print(_("No more hunks to process."))
        return

    if not quiet:
        show_selected_change()
