from typing import Counter


def remove_duplicate_docs(docs: list):
    """
    Removes Recoll docs that have the same URL but actually refer to different files, for example an epub file
    which contains HTML files will have multiple docs for each but they all refer to the same epub file.
    :param docs: the original list of documents
    :return: the same docs param but with the docs removed that share the same URL attribute
    """
    urls = [x.url for x in docs]
    url_count = Counter(urls)

    duplicates = [k for k in url_count.keys() if url_count[k] > 1]
    # Merge duplicate results, this might happen becase it actually consists of more than 1 file, like an epub
    # We adopt the relevancy rating of the max one
    for dup in duplicates:
        # Just take the one with the highest relevancy
        best_doc = None
        best_rating = -1
        for doc in [x for x in docs if x.url == dup]:
            rating = float(doc.relevancyrating.replace("%", ""))
            if rating > best_rating:
                best_doc = doc
                best_rating = rating

        docs = [x for x in docs if x.url != dup]
        docs.append(best_doc)
    return docs