from multiplayer_crosswords.dictionary import Dictionary, Word
import json
from pathlib import Path

def load_dictionary(p: str | Path) -> Dictionary:
    p = Path(p)
    if not p.exists():
        raise FileNotFoundError(f"Dictionary file not found: {p}")
    with p.open("r", encoding="utf-8") as f:
        data = json.load(f)
    
    dict_obj = Dictionary()

    for key, obj in data.items():
        word = obj.get("word", "").strip()
        if not word.isalpha():
            continue
        word = word.lower()
        dict_obj.add_word(Word(word=word, hints=[], difficulty=1))
    return dict_obj

def load_en_dictionary() -> Dictionary:
    return load_dictionary(Path(__file__).parent.parent / "data" / "en.json")

def load_de_dictionary() -> Dictionary:
    return load_dictionary(Path(__file__).parent.parent / "data" / "de.json")