# Source code for matchtable.src.matchtable.column

    from typing import Any, Literal, NotRequired, Self, TypedDict, cast
    from uuid import uuid4

    from .i18n import I18nWord, UidRef

    ColumnType = Literal["number", "string", "custom"]
    ColumnFlagType = Literal["isResizable", "destinationRelated", "sourceRelated"]

    class ColumnStyle(TypedDict):
        minWidth: NotRequired[int | UidRef]
        maxWidth: NotRequired[int | UidRef]

    class ColumnStyleBuilder:
        def __init__(self) -> None:
            """Create a style for a column."""
            self.min_width: int | UidRef | None = None
            self.max_width: int | UidRef | None = None

        def set_min_width(self, min_width: int) -> Self:
            """Set the minimum width to the column."""
            self.min_width = min_width
            return self

        def set_min_width_i18n(self, min_width: I18nWord) -> Self:
            """Set the i18n word as the minimum width of the column."""
            self.min_width = UidRef(uid_ref=min_width["key"], uid_type="i18n")
            return self

        def set_max_width(self, max_width: int) -> Self:
            """Set the maximum width to the column."""
            self.max_width = max_width
            return self

        def set_max_width_i18n(self, max_width: I18nWord) -> Self:
            """Set the i18n word as the maximum width of the column."""
            self.max_width = UidRef(uid_ref=max_width["key"], uid_type="i18n")
            return self

        def build(self) -> ColumnStyle:
            """Obtain the data."""
            s = ColumnStyle()
            if self.min_width is not None:
                s["minWidth"] = self.min_width
            if self.max_width is not None:
                s["maxWidth"] = self.max_width
            return s

    class Column(TypedDict):
        id: str
        type: ColumnType
        header: str | UidRef
        property: str
        propToSort: NotRequired[str]
        isResizable: NotRequired[bool]
        destinationRelated: NotRequired[bool]
        sourceRelated: NotRequired[bool]

    class ColumnBuilder:
        def __init__(self, column_type: ColumnType):
            """Create a column for the specified property.

            Args:
                column_type:
                    Type of the property
            """
            self.id = str(uuid4())
            self.name: str | UidRef
            self.type: ColumnType = column_type
            self.sort_key: str | None = None
            self.flags: dict[ColumnFlagType, bool] = {}
            self.style_builder: ColumnStyleBuilder | None = None

        def set_property_name(self, property_name: str) -> Self:
            """Set the property of match item shown in the column."""
            self.property_name = property_name
            return self

        def set_name(self, name: str) -> Self:
            """Set the name to the column."""
            self.name = name
            return self

        def set_name_i18n(self, name: I18nWord) -> Self:
            """Set the i18n word as the name of the column."""
            self.name = UidRef(uid_ref=name["key"], uid_type="i18n")
            return self

        def set_sort_key(self, sort_key: str) -> Self:
            """Specify the key used to sort the column."""
            self.sort_key = sort_key
            return self

        def set_flag(self, flag: ColumnFlagType) -> Self:
            """Set a flag to the column.

            Args:
                flag:
                    Type of the flag
            """
            self.flags[flag] = True
            return self

        def set_style(self, style_builder: ColumnStyleBuilder) -> Self:
            """Set a style to the column."""
            self.style_builder = style_builder
            return self

        def build(self) -> Column:
            """Obtain the data."""
            c = Column(
                id=self.id,
                property=self.property_name,
                header=self.name,
                type=self.type,
            )
            optional_keys: dict[Literal["propToSort"], Any] = {
                "propToSort": self.sort_key
            }
            for key, val in optional_keys.items():
                if val is not None:
                    c[key] = val
            if self.style_builder is not None:
                style = self.style_builder.build()
                for s in style:
                    cast(dict[Any, Any], c)[s] = cast(dict[Any, Any], style)[s]
            for f in self.flags:
                c[f] = self.flags[f]
            return c