import re

# ANSI Escape Sequences (Standard and Xterm)
ANSI_ESCAPE = re.compile(r'\x1b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')

# Shell Prompt Detection patterns
PROMPT_PATTERNS = [
    r"[\r\n].*[@\w\.\-]+:.*[#$]\s*$",  # bash/zsh: user@host:~$
    r">>>\s*$",                        # python
    r"\.\.\.\s*$",                      # python multi-line
    r">\s*$",                           # node/js
    r"PS\s+.*>\s*$",                   # powershell
    r"[A-Z]:\\.*>\s*$",                 # Windows CMD: C:\Users>
    r"\(.*\) [A-Z]:\\.*>\s*$",          # Windows CMD with venv: (venv) C:\Users>
]

# Compiled prompt patterns for performance
COMPILED_PROMPT_PATTERNS = [re.compile(p) for p in PROMPT_PATTERNS]

# Protocol Extraction (OSC 1337 / Bracketed)
TASK_PROTOCOL_FENCE = re.compile(r'1337;Task(Start|End);id=([a-zA-Z0-9-]*)')
# This matches digits optionally followed by brackets (for split-processing resilience)
EXIT_CODE_PATTERN = re.compile(r'^(\d+)')

# Echo Suppression Patterns (M9/M7)
ECHO_START_PATTERN = re.compile(r'echo \s*\[\[1337;Task\^Start;id=[a-zA-Z0-9-]*\]\]\s*&\s*')
ECHO_END_PATTERN = re.compile(r'\s*&\s*echo \s*\[\[1337;Task\^End;id=[a-zA-Z0-9-]*;exit=\d+\]\]')
ECHO_CLEANUP_ANSI = re.compile(r'echo \x1b]1337;TaskEnd;.*')
ECHO_CLEANUP_BRACKET = re.compile(r'echo \[\[1337;TaskEnd;.*')

# UI Stealth Filtering
PROTOCOL_HINT_PATTERN = re.compile(r"1337;Task|`e]|\\033]")
STRIP_START_FENCE = re.compile(r'\x1b]1337;Task(Start|End);id=.*?\x07')
STRIP_BRACKET_FENCE = re.compile(r'\[\[1337;Task(Start|End);id=.*?\]\]')

# Windows ConPTY stream noise filters
# Matches Windows ConHost title-set sequences: "0;...<title>..." lines
WIN_TITLE_RE  = re.compile(r'\x1b]0;.*?\x07', re.MULTILINE)
# Matches the '@call "...bat"' invocation line echoed by cmd.exe (accounting for optional @ and merges)
BAT_CALL_RE   = re.compile(r'@?call\s+".+?\.bat".*?(?=\s*&|$)', re.IGNORECASE)
# Matches the '@del /Q ...' cleanup echo (accounting for optional @ and merges)
DEL_RE        = re.compile(r'@?del\s+/Q\s+".+?\.bat".*?$', re.IGNORECASE)
