"""Parser for LOL ``<ObjectType>`` elements in DMSContent responses."""
from ecmind_blue_client.ecm.parser._dms_content_object_type import DmsContentObjectType
from ecmind_blue_client.ecm.parser._dms_content_row import DmsContentRow


class DmsContentObjectTypeLol(DmsContentObjectType):
    """LOL-specific ``<ObjectType>`` that exposes its contents as ``<Row>`` elements."""

    def rows(self):
        """Yields all ``<Row>`` elements from ``<Rowset>/<Rows>``.

        Column definitions are resolved from ``<Rowset>/<Columns>/<Column>`` elements.
        Embedded table field column names are resolved from
        ``<Rowset>/<Columns>/<TableField>/<Columns>/<Column>`` and passed to each row.

        Yields:
            DmsContentRow: Parsed LOL rows.
        """
        column_nodes = self._node.findall("Rowset/Columns/Column")

        # Build a mapping of table field internal_name → list of column name strings.
        # The LOL Rowset/Columns/TableField elements declare column names as plain strings.
        table_field_columns: dict[str, list[str]] = {}
        for tf_node in self._node.findall("Rowset/Columns/TableField"):
            internal_name = tf_node.get("internal_name", "")
            table_field_columns[internal_name] = [col.text or "" for col in tf_node.findall("Columns/Column")]

        rows_nodes = self._node.findall("Rowset/Rows/Row")
        for row_node in rows_nodes:
            yield DmsContentRow(row_node, column_nodes, table_field_columns)
