from typing import Dict
from typing import Tuple


def encode_and_pad(
    input_docs: list, word_map: Dict[str, int], sentence_limit: int, word_limit: int
) -> Tuple[list, list, list]:
    """
    Encode sentences, and pad them to fit word_limit.

    Parameters
    ----------
    input_docs : list
        Documents ([ [word1a, ... ], ..., [wordna, ... ] ])

    word_map : Dict[str, int]
        Word2ix map

    sentence_limit : int
        Max number of sentences in a document

    word_limit : int
        Max number of words in a sentence

    Returns
    -------
    encoded_docs : list
        Encoded and padded document

    sentences_per_document : list
        Number of sentences per document

    words_per_sentence : list
        Number of words per sentence
    """
    encoded_docs = list(
        map(lambda doc: list(
            map(lambda s: list(
                map(lambda w: word_map.get(w, word_map['<unk>']), s)
            ) + [0] * (word_limit - len(s)), doc)
        ) + [[0] * word_limit] * (sentence_limit - len(doc)), input_docs)
    )
    sentences_per_document = list(map(lambda doc: len(doc), input_docs))
    words_per_sentence = list(
        map(lambda doc: list(
            map(lambda s: len(s), doc)
        ) + [0] * (sentence_limit - len(doc)), input_docs)
    )
    return encoded_docs, sentences_per_document, words_per_sentence