import tokenize
from io import BytesIO
import re
import unicodedata
from .dictionaries.tamil_dic import dictionary

class TamilCompiler:
    def __init__(self):
        self.translation_map = {}
        for k, v in dictionary.items():
            # Strip invisible characters from dictionary
            clean_k = re.sub(r'[\u200b\u200c\u200d\ufeff]', '', k)
            self.translation_map[unicodedata.normalize('NFC', clean_k)] = v

    def translate(self, code):
        # Strip invisible characters from user code
        code = re.sub(r'[\u200b\u200c\u200d\ufeff]', '', code)
        code = unicodedata.normalize('NFC', code)
        
        tokens = list(tokenize.tokenize(BytesIO(code.encode('utf-8')).readline))
        new_tokens = []
        
        for token in tokens:
            if token.string in ('அமை', 'amai', 'set'):
                continue
            if token.type == tokenize.NAME:
                exact_str = token.string
                lower_str = token.string.lower()
                if exact_str in self.translation_map:
                    translated = self.translation_map[exact_str]
                elif lower_str in self.translation_map:
                    translated = self.translation_map[lower_str]
                else:
                    translated = exact_str
            else:
                translated = token.string
                
            new_tokens.append(tokenize.TokenInfo(token.type, translated, token.start, token.end, token.line))

        return tokenize.untokenize(new_tokens).decode('utf-8')