"""Columns block implementation."""

from __future__ import annotations

from yera.events.blocks.base import LayoutHandle, _BlockFactory
from yera.events.blocks.exit import unsupported_block_event
from yera.events.exceptions import UnsupportedAwaitUserBlockError
from yera.events.models import ColumnsData
from yera.events.stream import is_restricted_stream


class _ColumnsBlockFactory(_BlockFactory):
    """Factory for columns blocks."""

    def __init__(self):
        super().__init__("columns")

    def __call__(self, n: int, border: bool = False) -> LayoutHandle:
        """Return a context manager for creating column layouts with n columns."""
        if is_restricted_stream():
            unsupported_block_event(self.block_type)
            raise UnsupportedAwaitUserBlockError(self.block_type)
        block_id = self._generate_block_id()
        return LayoutHandle(
            block_id, "columns", ColumnsData(showBorder=border, column_count=n)
        )


# Create the columns callable/context manager
columns = _ColumnsBlockFactory()

__all__ = ["columns"]
