from typing import OrderedDict


def build_graphics_layer(root):
    """
    Build the graphics layer from the provided root of the XML tree.

    :param root: The root element of the XML tree.
    :type root: :class:`etree.Element`

    :return: An OrderedDict containing the locations of markers, suitable for direct
        transformation into JSON for use with the eRegs frontend.
    :rtype: :class:`collections.OrderedDict`:
    """

    layer_dict = OrderedDict()
    paragraphs = root.findall('.//{eregs}paragraph')

    for paragraph in paragraphs:
        content = paragraph.find('{eregs}content')
        graphics = content.findall('{eregs}graphic')
        label = paragraph.get('label')
        if len(graphics) > 0:
            layer_dict[label] = []
        for graphic in graphics:
            text = graphic.find('{eregs}text').text
            alt_text = graphic.find('{eregs}altText').text
            if alt_text is None:
                alt_text = ''
            url = graphic.find('{eregs}url').text
            graphic_dict = OrderedDict()
            graphic_dict['alt'] = alt_text
            graphic_dict['locations'] = [0]
            graphic_dict['text'] = text
            graphic_dict['url'] = url

            layer_dict[label].append(graphic_dict)

    return layer_dict