import msgpack
from khmer_keyboard import __version__ as package_version
from khmer_keyboard.model_io import MODEL_FORMAT_VERSION


def export_model(path, unigram, bigram_next, trigram_topk, chargram=None):

    model = {
        "model_format_version": MODEL_FORMAT_VERSION,
        "package_version": package_version,
        "unigram": dict(unigram),
        "bigram_next": bigram_next,
        "trigram_topk": trigram_topk,
        "chargram": chargram or {},
    }

    with open(path, "wb") as f:
        f.write(msgpack.packb(model, use_bin_type=True))
