import re
import sys

# Define the source and destination files
input_file = 'Tam Bao.txt'
output_file = 'Tam Bao - Zen.txt'

def translate_line(line):
    # This is where the translation logic for a single line would go.
    # For now, we are keeping the existing translation but removing the English parts
    # and ensuring it follows the required format.
    if line.startswith('→'):
        content = line[1:].strip()
        # Handle cases like "Pali -> Eng -> Vie" or "Eng -> Vie"
        if '→' in content:
            parts = content.split('→')
            # Extract the last part which is Vietnamese
            vietnamese = parts[-1].strip()
            return f"→ {vietnamese}"
        else:
            return f"→ {content}"
    return line

def process_file():
    try:
        with open(input_file, 'r', encoding='utf-8') as f:
            content = f.read()
            
        # Split into pages
        pages = re.split(r'(TRANG \d+)', content)
        
        final_output = []
        
        # Start from index 1 because index 0 is empty before the first TRANG
        for i in range(1, len(pages), 2):
            page_label = pages[i]
            page_text = pages[i+1].strip()
            
            translated_lines = [page_label]
            for line in page_text.split('\n'):
                if line.strip():
                    translated_lines.append(translate_line(line.strip()))
            
            final_output.append('\n'.join(translated_lines))
            
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write('\n\n'.join(final_output))
            
        print(f"Successfully processed {input_file} to {output_file}")
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    process_file()
