from __future__ import annotations

from collections.abc import Mapping
from typing import Any, TypeVar, BinaryIO, TextIO, TYPE_CHECKING, Generator

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

from ..types import UNSET, Unset
from typing import cast

if TYPE_CHECKING:
  from ..models.renderer_config import RendererConfig
  from ..models.source import Source





T = TypeVar("T", bound="Context")



@_attrs_define
class Context:
    """ A context is a named namespace that groups one or more sources. The
    context id is a URL-safe slug, supplied by the client on creation, and
    is immutable. Context-Docs are served under `/<context-id>/`.

        Example:
            {'id': 'project-docs', 'sources': [{'transport': 'local', 'location': '/path/to/project-docs', 'alias':
                'project-docs', 'slug': 'project-docs'}], 'renderer': {'md_engine': 'pandoc', 'hl_engine': 'pygments'}}

        Attributes:
            id (str): URL-safe slug, immutable after creation.
            sources (list[Source]): Sources belonging to this context.
            renderer (RendererConfig | Unset): Renderer configuration for Markdown-to-HTML conversion. Both engines are
                optional; partial overrides are allowed (set only `md_engine` or only
                `hl_engine`). When neither context nor source sets a value, the service
                default applies (`md_engine: "pandoc"`, `hl_engine: "pygments"`). Example: {'md_engine': 'pandoc', 'hl_engine':
                'pygments'}.
     """

    id: str
    sources: list[Source]
    renderer: RendererConfig | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)





    def to_dict(self) -> dict[str, Any]:
        from ..models.renderer_config import RendererConfig
        from ..models.source import Source
        id = self.id

        sources = []
        for sources_item_data in self.sources:
            sources_item = sources_item_data.to_dict()
            sources.append(sources_item)



        renderer: dict[str, Any] | Unset = UNSET
        if not isinstance(self.renderer, Unset):
            renderer = self.renderer.to_dict()


        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({
            "id": id,
            "sources": sources,
        })
        if renderer is not UNSET:
            field_dict["renderer"] = renderer

        return field_dict



    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.renderer_config import RendererConfig
        from ..models.source import Source
        d = dict(src_dict)
        id = d.pop("id")

        sources = []
        _sources = d.pop("sources")
        for sources_item_data in (_sources):
            sources_item = Source.from_dict(sources_item_data)



            sources.append(sources_item)


        _renderer = d.pop("renderer", UNSET)
        renderer: RendererConfig | Unset
        if isinstance(_renderer,  Unset):
            renderer = UNSET
        else:
            renderer = RendererConfig.from_dict(_renderer)




        context = cls(
            id=id,
            sources=sources,
            renderer=renderer,
        )


        context.additional_properties = d
        return context

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
