"""Live discard line action orchestration."""

from __future__ import annotations

import sys

from ...data.file_review.action_scope import finish_review_scoped_line_action
from ...data.file_review.records import FileReviewState
from ...data.selected_change.paths import get_selected_change_file_path
from ...data.undo.checkpoints import undo_checkpoint
from ...i18n import _
from . import discard_line_selection as _discard_line_selection
from .selected_hunk_refresh import refresh_selected_hunk_after_line_action


def discard_live_line_selection(
    line_id_specification: str,
    file: str | None = None,
    *,
    review_state: FileReviewState | None,
    auto_advance: bool | None = None,
) -> None:
    """Discard selected lines from the live working-tree view."""
    operation_parts = ["discard", "--line", line_id_specification]
    if file is not None:
        operation_parts.extend(["--file", file])

    target_file = file if file not in (None, "") else get_selected_change_file_path()
    with undo_checkpoint(
        " ".join(operation_parts),
        worktree_paths=[target_file] if target_file is not None else [],
        rollback_on_error=True,
    ):
        file_path = _discard_line_selection.discard_worktree_line_selection(
            line_id_specification,
            file=file,
        )
        print(
            _("✓ Discarded selection {lines} from {file}").format(
                lines=line_id_specification,
                file=file_path,
            ),
            file=sys.stderr,
        )
        refresh_selected_hunk_after_line_action(
            file_path,
            auto_advance=auto_advance,
        )
        finish_review_scoped_line_action(review_state)
