from typing import Sequence, Optional

from ...filth import Filth, MergedFilth
from ..base import PostProcessor


class FilthTypeReplacer(PostProcessor):
    name = 'filth_type_replacer'  # type: str

    @staticmethod
    def filth_label(filth, upper: bool = True, separator: Optional[str] = None) -> str:
        if separator is None:
            separator = '+'
        filths = [filth]
        if isinstance(filth, MergedFilth):
            filths = filth.filths

        replacements = set()
        for f in filths:
            filth_type = getattr(f, 'type', None)
            if filth_type is None:
                continue
            if filth_type == 'known':
                filth_comparison_type = getattr(f, 'comparison_type', None)
                if filth_comparison_type is not None:
                    filth_type += '_' + filth_comparison_type
            replacements.add(filth_type)

        # print(filths, replacements)
        label = separator.join(sorted(replacements))
        if upper:
            label = label.upper()
        return label

    def process_filth(self, filth_list: Sequence[Filth]) -> Sequence[Filth]:
        for filth_item in filth_list:
            filth_item.replacement_string = self.filth_label(filth_item)
        return filth_list


__all__ = ['FilthTypeReplacer']

@@ -71,16 +71,39 @@
 gedFilth
+, TaggedEvaluationFilth
 %0Afrom ..
@@ -128,16 +128,38 @@
 ocessor%0A
+from ... import utils%0A
 %0A%0Aclass 
@@ -239,16 +239,516 @@
 e: str%0A%0A
+    # NOTE: this is not an efficient way to store this in memory. could%0A    # alternatively hash the type and text and do away with the overhead%0A    # bits of storing the tuple in the lookup%0A    lookup = utils.Lookup()%0A%0A    def __init__(self, upper: bool = True, include_count: bool = False, separator: Optional%5Bstr%5D = None, **kawrgs):%0A        super(FilthTypeReplacer, self).__init__(**kawrgs)%0A        self.upper = upper%0A        self.include_count = include_count%0A        self.separator = separator%0A%0A
     @sta
@@ -803,16 +803,45 @@
  = True,
+ include_count: bool = False,
  separat
@@ -1232,15 +1232,34 @@
  == 
-'known'
+TaggedEvaluationFilth.type
 :%0A  
@@ -1448,16 +1448,275 @@
 on_type%0A
+%0A            filth_type = filth_type.replace(' ', '_')%0A%0A            if include_count:%0A                filth_type = '%7B%7D-%7B:d%7D'.format(%0A                    filth_type,%0A                    FilthTypeReplacer.lookup%5B(filth_type, f.text.lower())%5D%0A                )%0A%0A
         
@@ -2090,26 +2090,189 @@
 h_label(
-filth_item
+%0A                filth=filth_item,%0A                upper=self.upper,%0A                include_count=self.include_count,%0A                separator=self.separator,%0A            
 )%0A      

allow numeric replacement in the FilthTypeReplacer