import requests
import hashlib
import xml.etree.ElementTree as ET


def xml_to_dict(xml_data: str) -> dict:
    # Parse the XML data
    root = ET.fromstring(xml_data)

    # Function to remove namespace from the tag name
    def remove_namespace(tag):
        return tag.split('}')[-1]  # Splits on '}' and returns the last part, the actual tag name without namespace

    # Function to recursively convert XML elements to a dictionary
    def elem_to_dict(elem):
        tag = remove_namespace(elem.tag)
        elem_dict = {tag: {}}

        # If the element has attributes, add them to the dictionary
        elem_dict[tag].update({'@' + remove_namespace(k): v for k, v in elem.attrib.items()})

        # Handle the element's text content, if present and not just whitespace
        text = elem.text.strip() if elem.text and elem.text.strip() else None
        if text:
            elem_dict[tag]['#text'] = text

        # Process child elements
        for child in elem:
            child_dict = elem_to_dict(child)
            child_tag = remove_namespace(child.tag)
            if child_tag not in elem_dict[tag]:
                elem_dict[tag][child_tag] = []
            elem_dict[tag][child_tag].append(child_dict[child_tag])

        # Simplify structure if there's only text or no children and no attributes
        if len(elem_dict[tag]) == 1 and '#text' in elem_dict[tag]:
            return {tag: elem_dict[tag]['#text']}
        elif not elem_dict[tag]:
            return {tag: ''}

        return elem_dict

    # Convert the root element to dictionary
    return elem_to_dict(root)


def sha256_hex(input_string: str) -> str:
    return hashlib.sha256(input_string.encode()).hexdigest()
