"""Parser for ``<Rights>`` elements in DMSContent responses."""
import xml.etree.ElementTree as ET

from ecmind_blue_client.ecm.parser._dms_content_object_inserts import (
    DmsContentObjectInserts,
)


class DmsContentObjectRights:
    """Represents the ``<Rights>`` element of a HOL ``<Object>`` or LOL ``<Row>``.

    Available only when ``rights=1`` is set in the ``<DmsQuery>``. Exposes the
    permission flags and allowed insert types for the requesting user.
    """

    def __init__(self, node: ET.Element):
        self._node = node

    @property
    def object_inserts(self) -> bool:
        """``True`` if the user is allowed to insert child objects."""
        return bool(self._node.get("object_inserts", False))

    @property
    def modify_index(self) -> bool:
        """``True`` if the user is allowed to modify index fields."""
        return bool(self._node.get("modify_index", False))

    @property
    def delete_object(self) -> bool:
        """``True`` if the user is allowed to delete this object."""
        return bool(self._node.get("delete_object", False))

    @property
    def export_object(self) -> bool:
        """``True`` if the user is allowed to export this object."""
        return bool(self._node.get("export_object", False))

    @property
    def modify_object(self) -> bool:
        """``True`` if the user is allowed to modify this object."""
        return bool(self._node.get("modify_object", False))

    @property
    def ObjectInserts(self) -> list[DmsContentObjectInserts]:
        """Returns the list of allowed insert type entries from ``<ObjectInserts>`` children.

        Returns:
            list[DmsContentObjectInserts]: Insert permission entries, may be empty.
        """
        object_inserts = self._node.findall("ObjectInserts")
        return [
            DmsContentObjectInserts(int(object_insert.attrib["type"]), int(object_insert.attrib["count"]))
            for object_insert in object_inserts
        ]
