"""Debug the actual rc values returned by insert_textbox during real translation."""
import sys
sys.path.insert(0, '.')
import importlib.util
import fitz
from pathlib import Path

spec = importlib.util.spec_from_file_location('pdf_mod', 'translators/pdf_translator.py')
pdf_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pdf_mod)

# Monkey-patch _try_insert to print rc values
original_try_insert = pdf_mod.PDFTranslator._try_insert

def debug_try_insert(self, page, rect, text, fontsize, fontname, fontfile, color, align):
    rc = page.insert_textbox(
        rect, text,
        fontsize=fontsize,
        fontname=fontname,
        fontfile=fontfile,
        color=color,
        align=align,
        overlay=True,
    )
    text_preview = text[:30].replace('\n', '\\n')
    print(f"  TRY: size={fontsize:.1f} rect={rect.width:.0f}x{rect.height:.0f} text={text_preview!r} -> rc={rc}", flush=True)
    return rc

pdf_mod.PDFTranslator._try_insert = debug_try_insert

PDFTranslator = pdf_mod.PDFTranslator
PDFTranslator().translate_file(
    Path('sample_files/test_corpus/test_pdf.pdf'),
    Path('sample_files/test_corpus/test_pdf_translated.pdf'),
    target_language='fr',
    source_language='en',
)
