from __future__ import unicode_literals, print_function

from .mf_helpers import unordered_list

import re

_mf2_classes_re = re.compile("(p|e|u|dt|h)-((:?[a-z0-9]+-)?[a-z]+(:?-[a-z]+)*)$")
_mf2_roots_re = re.compile("h-(:?[a-z0-9]+-)?[a-z]+(:?-[a-z]+)*$")
_mf2_properties_re = re.compile("(p|e|u|dt)-(:?[a-z0-9]+-)?[a-z]+(:?-[a-z]+)*$")
_mf2_e_properties_re = re.compile("e-(:?[a-z0-9]+-)?[a-z]+(:?-[a-z]+)*$")


def filter_classes(classes, regex=_mf2_classes_re):
    """detect classes that are valid names for mf2, sort in dictionary by prefix"""

    types = {x: set() for x in ('u', 'p', 'dt', 'e', 'h')}
    for c in classes:
        a=regex.match(c)
        if a:
            if c[0] == "h":
                types['h'].add(c)
            else:
                types[a[1]].add(a[2])
    return types


def root(classes):
    return {c for c in classes if _mf2_roots_re.match(c)}

def is_property_class(class_):
    return _mf2_properties_re.match(class_)

def has_embedded_class(classes):
    return any(_mf2_e_properties_re.match(c) for c in classes)
@@ -637,10 +637,16 @@
     
-a=
+match = 
 rege
@@ -667,17 +667,21 @@
      if 
-a
+match
 :%0A      
@@ -780,22 +780,42 @@
 pes%5B
-a%5B1%5D%5D.add(a%5B2%5D
+match.group(1)%5D.add(match.group(2)
 )%0A  

use old re.match.group() syntax