from django.utils.translation import gettext


def repeat(n: int, unit: str) -> str:
    """Creates a CSS repeat element, that makes the `unit` be repeated `n` times.

    Example:
        `repeat(5, Style.px(20))` results in the string `'repeat(5, 20px)'`

    Attributes:
        n (int): The amount of times the unit should be repeated
        unit (str): The unit that is going to be repeated. Should be generated by the unit helper methods, e.g. `Style.px(20)`

    Returns:
        A valid css string containing the information about repeated an element `n` times.
    """

    return f"repeat({n}, {unit})"


def repeat_field(n: int, field_name: str) -> str:
    """Creates a string repeating the `field_name` `n` times

    Example:
        `repeat_field(5, Style.px(20))` results in the string `'20px 20px 20px 20px 20px'`

    Args:
        n: An integer specifying how often the the `field_name` should be repeated
        field_name: A string, which should be repeated `n` times

    Returns:
        A string where the value of `field_name` is duplicated `n` times
    """
    return " ".join([field_name] * n)


def grid_definition(*units: str) -> str:
    """Concatanates the arguments specified as positional arguments with a space to create a valid css string to define a grid

    Arguments:
        *units: Positional string arguments that will be concatanated

    Returns:
        A valid css string of space seperated strings

    """
    return " ".join(units)


def split_list_into_grid_template_area_sublists(elem_list: list, column_number: int) -> list[list]:
    """Splits a list of elements into sublists of the provided column number length and adds empty grid cells at the end if necessary

    Args:
        elem_list (list): List of elements to split
        column_number (int): Number of columns in the grid. Must be above 0

    Returns:
        list: A list of lists of exactly the specified length

    Raises:
        ValueError: If column_number below 1 is provided
    """

    if column_number < 1:
        raise ValueError(gettext("Column number has to be at least 1!"))

    sublists = [elem_list]
    if column_number <= len(elem_list):
        # If we don't have enough elements in the list to split into the target column number we avoid the IndexError and instead populate the sublist with empty fields
        sublists = [elem_list[x : x + column_number] for x in range(0, len(elem_list), column_number)]

    for _ in range(column_number - len(sublists[-1])):
        sublists[-1].append(".")
    return sublists
