"""
Name: Helpers.py
Author: Radovan Klembara

Implements helping functions.
"""

from itertools import permutations
import re
from Error import Error


def check_empty_intersection(list_a: [str], list_b: [str]) -> bool:
    """
    Checks whether two lists have empty intersection.

    :param list_a: First list.
    :param list_b: Second list.
    :return: True if there is not a common element in lists.
    """
    if len(set(list_a).intersection(set(list_b))) > 0:
        return False
    return True


def list_to_str(array: [str]) -> str:
    """
    Converts list to string where elements are divided by single space.

    :param array: List of elements.
    :return: String representation of list.
    """
    result = ""
    for element in array:
        result = result + element + " "
    return result[:len(result) - 1]


def check_string(pattern: str, string: str, error_msg: str):
    """
    Checks string with regular expression.

    :param pattern: Regular expression.
    :param string: String to be checked.
    :param error_msg: Error message.
    """
    if not re.match(pattern, string):
        print(error_msg)
        exit(Error.INPUT)


def check_string_over_set(elements, string: str) -> bool:
    """
    Checks whether given string has been created by given elements.

    :param elements: The source elements.
    :param string: The string that should be created by the permutation of elements.
    :return: True if string is created from elements.
    """
    reduced_elements = list(filter(lambda x: len(x) <= len(string), elements))
    combins = list(permutations(reduced_elements))
    combins = sorted(combins, key=lambda x: len(x))
    for combination in combins:
        tmpstring = string
        for element in combination:
            res = tmpstring.split(element)
            tmpstring = ""
            for r in res:
                if r != element:
                    tmpstring += r
            if len(tmpstring) == 0:
                return True
    return False
