"""DMSContent parser for HOL ``<Object>`` elements with child object type access."""
from typing import Optional

from ecmind_blue_client.ecm.parser._dms_content_object import DmsContentObject


class DmsContentObjectHol(DmsContentObject):
    """Wraps an HOL ``<Object>`` element and provides access to its child object types."""

    def child_object_types(self):
        """Yield all ``<ObjectType>`` elements from ``<ChildObjects>``."""
        from ecmind_blue_client.ecm.parser._dms_content_object_type_hol import (
            DmsContentObjectTypeHol,
        )

        nodes = self._node.findall("ChildObjects/ObjectType")
        for node in nodes:
            yield DmsContentObjectTypeHol(node, self._archive)

    def child_object_Type(self, internal_name_or_id: Optional[str | int]):
        """Yield the ``<ObjectType>`` matching the given internal name or numeric type ID."""
        from ecmind_blue_client.ecm.parser._dms_content_object_type_hol import (
            DmsContentObjectTypeHol,
        )

        if isinstance(internal_name_or_id, int):
            node = self._node.find(f"ChildObjects/ObjectType[@id='{internal_name_or_id}']", None)
        else:
            node = self._node.find(f"ChildObjects/ObjectType[@internal_name='{internal_name_or_id}']", None)

        if node:
            yield DmsContentObjectTypeHol(node, self._archive)

        raise ValueError("No object type found with the given internal name or ID.")
