import re

_RE_PATH = re.compile(r"[.|/\\]")  # allowed separators: [. | / \]


UNKNOWN = 0x00000000
ARRAY = 0x00000001
STRING = 0x00000002
DATA = 0x00000004
INTEGER = 0x00000008
REAL = 0x00000010
DATE = 0x00000020
BOOLEAN = 0x00000040

_TYPES_TAG_MAP = {
    "array": ARRAY,
    "string": STRING,
    "data": DATA,
    "integer": INTEGER,
    "real": REAL,
    "date": DATE,
    "boolean": BOOLEAN,
}
_TYPES_MAP = {
    ARRAY: "array",
    STRING: "string",
    DATA: "data",
    INTEGER: "integer",
    REAL: "real",
    DATE: "date",
    BOOLEAN: "boolean",
}

def _by_path(
    node,
    path,
    index=0,
    is_key=False,
):
    if node is None:
        return None

    path_parts = _RE_PATH.split(path)

    key_name = None
    if is_key:
        key_name = path_parts[-1]
        path_parts = path_parts[:-1]

    group = node
    for path_part in path_parts:
        group = group.group(name=path_part)
        if group is None:
            return None

    if key_name is not None:
        return group.key(key_name)
    else:
        return group


def _by_name(array, name):
    for obj in array:
        if obj.name == name:
            return obj
    return None
