from typing import OrderedDict


def build_keyterm_layer(root):
    """
    Build the keyterm layer from the provided XML tree.

    :param root: The root element of an XML tree containing paragraphs.
    :type root: :class:`etree.Element`

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

    keyterm_dict = OrderedDict()
    paragraphs = root.findall('.//{eregs}paragraph') \
               + root.findall('.//{eregs}interpParagraph')

    for paragraph in paragraphs:
        title = paragraph.find('{eregs}title')
        if title is not None and title.get('type') == 'keyterm':
            label = paragraph.get('label')
            keyterm_dict[label] = [
                {
                    'key_term': title.text,
                    'locations': [0]
                }
            ]

    return keyterm_dict