"""Parser for prettier output.

Handles typical Prettier CLI output for --check and --write modes,
including ANSI-colored lines produced in CI environments.
"""

from loguru import logger

from lintro.parsers.base_parser import strip_ansi_codes
from lintro.parsers.prettier.prettier_issue import PrettierIssue


def parse_prettier_output(output: str) -> list[PrettierIssue]:
    """Parse prettier output into a list of PrettierIssue objects.

    Args:
        output: The raw output from prettier

    Returns:
        List of PrettierIssue objects
    """
    issues: list[PrettierIssue] = []

    if not output:
        return issues

    # Prettier output format when issues are found:
    # "Checking formatting..."
    # "[warn] path/to/file.js"
    # "[warn] Code style issues found in the above file. Run Prettier with --write \
    # to fix."
    # Normalize output by stripping ANSI escape sequences to make matching robust
    # across different terminals and CI runners.
    # Example: "[\x1b[33mwarn\x1b[39m] file.js" -> "[warn] file.js"
    normalized_output = strip_ansi_codes(output)

    lines = normalized_output.splitlines()

    for _i, line in enumerate(lines):
        try:
            line = line.strip()
            if not line:
                continue

            # Look for [warn] lines that contain file paths
            if line.startswith("[warn]") and not line.endswith("fix."):
                # Extract the file path from the [warn] line
                file_path = line[6:].strip()  # Remove "[warn] " prefix
                if file_path and not file_path.startswith("Code style issues"):
                    # Create a generic issue for the file
                    issues.append(
                        PrettierIssue(
                            file=file_path,
                            line=1,  # Prettier doesn't provide specific line numbers
                            code="FORMAT",
                            message="Code style issues found",
                            # Prettier doesn't provide specific column numbers
                            column=1,
                        ),
                    )
        except (IndexError, AttributeError, TypeError) as e:
            logger.debug(f"Failed to parse prettier line '{line}': {e}")
            continue

    return issues
