# coding: utf-8

"""
    Docling Serve

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictInt
from typing import Any, ClassVar, Dict, List, Optional
from yfkm3_services_parse_client.models.table_cell import TableCell
from yfkm3_services_parse_client.models.table_cells_inner import TableCellsInner
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class TableData(BaseModel):
    """
    BaseTableData.
    """ # noqa: E501
    table_cells: Optional[List[TableCellsInner]] = None
    num_rows: Optional[StrictInt] = 0
    num_cols: Optional[StrictInt] = 0
    grid: List[List[TableCell]] = Field(description="grid.")
    __properties: ClassVar[List[str]] = ["table_cells", "num_rows", "num_cols", "grid"]

    model_config = ConfigDict(
        validate_by_name=True,
        validate_by_alias=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(to_jsonable_python(self.to_dict()))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of TableData from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "grid",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of each item in table_cells (list)
        _items = []
        if self.table_cells:
            for _item_table_cells in self.table_cells:
                if _item_table_cells:
                    _items.append(_item_table_cells.to_dict())
            _dict['table_cells'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in grid (list of list)
        _items = []
        if self.grid:
            for _item_grid in self.grid:
                if _item_grid:
                    _items.append(
                         [_inner_item.to_dict() for _inner_item in _item_grid if _inner_item is not None]
                    )
            _dict['grid'] = _items
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of TableData from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "table_cells": [TableCellsInner.from_dict(_item) for _item in obj["table_cells"]] if obj.get("table_cells") is not None else None,
            "num_rows": obj.get("num_rows") if obj.get("num_rows") is not None else 0,
            "num_cols": obj.get("num_cols") if obj.get("num_cols") is not None else 0,
            "grid": [
                    [TableCell.from_dict(_inner_item) for _inner_item in _item]
                    for _item in obj["grid"]
                ] if obj.get("grid") is not None else None
        })
        return _obj


