"""Parser for ``<RowField>`` elements within table field rows in DMSContent responses."""
import xml.etree.ElementTree as ET

from ecmind_blue_client.ecm.parser._dms_content_object_field import (
    DmsContentObjectFieldBase,
)


class DmsContentObjectRowField(DmsContentObjectFieldBase):
    """Represents a single cell value within a table-dialog or LOL row.

    Column metadata (``internal_name``, ``datatype``, etc.) is taken from the
    corresponding ``<Column>`` element; the actual value comes from a separate
    ``<Value>`` element.
    """

    def __init__(self, column_node: ET.Element, value_node: ET.Element, row_id: int):
        """Args:
            column_node: The ``<Column>`` element providing field metadata.
            value_node: The ``<Value>`` element containing the cell value.
            row_id: Numeric ID of the parent row.
        """
        super().__init__(column_node)
        self._value_node = value_node
        self._row_id = row_id

    def _getRawValue(self) -> str | None:
        """Returns the ``value`` attribute of the ``<Value>`` element, or its text content."""
        return self._value_node.attrib["value"] if "value" in self._value_node.attrib else self._value_node.text

    @property
    def display_value(self) -> str:
        """Human-readable display value (text content of the ``<Value>`` element)."""
        return self._value_node.text or ""

    @property
    def row_id(self) -> int:
        """Numeric ID of the parent row this field belongs to."""
        return self._row_id

    @property
    def system(self) -> bool:
        """``True`` if this is a system-managed column (not editable by users)."""
        return bool(self._node.get("system", False))
