File size: 2,869 Bytes
c0e9d84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from __future__ import annotations

from telethon import Button

from .tg import (
    _is_built_button,
    _normalize_icon,
    _normalize_style,
    ikm,
    request_peer_button,
)

__all__ = (
    "STYLE_PRESETS",
    "RichKit",
    "button",
    "request_peer_button",
    "resolve_style",
    "rich_ikm",
    "rich_kit",
)

STYLE_PRESETS = {
    "default": (None, None),
    "neutral": (None, None),
    "unstyled": (None, None),
    "primary": ("primary", None),
    "info": ("primary", None),
    "blue": ("primary", None),
    "success": ("success", None),
    "ok": ("success", None),
    "confirm": ("success", None),
    "green": ("success", None),
    "danger": ("danger", None),
    "error": ("danger", None),
    "warn": ("danger", None),
    "red": ("danger", None),
    "back": ("primary", None),
    "cancel": ("danger", None),
}


class RichKit:
    """Build Telethon buttons with semantic styles and icons."""

    def resolve_style(self, style=None, icon=None, preset=None):
        if preset is not None:
            preset_style, preset_icon = STYLE_PRESETS.get(
                str(preset).strip().lower(), (None, None)
            )
            if style is None:
                style = preset_style
            if icon is None:
                icon = preset_icon
        return _normalize_style(style), _normalize_icon(icon)

    def button(
        self,
        text,
        data=None,
        *,
        url=None,
        style=None,
        icon=None,
        preset=None,
        request=None,
        button_id=1,
        max_quantity=1,
        **peer_kwargs,
    ):
        style, icon = self.resolve_style(style, icon, preset)
        if url is not None:
            return Button.url(text, url=url, style=style, icon=icon)
        if request is not None:
            return request_peer_button(
                text,
                request,
                button_id=button_id,
                max_quantity=max_quantity,
                style=style,
                icon=icon,
                **peer_kwargs,
            )
        return Button.inline(text, data=data, style=style, icon=icon)

    def rich_ikm(self, data):
        if data is None:
            return None
        if isinstance(data, (tuple, dict)) or _is_built_button(data):
            data = [data]
        rows = []
        for row in data:
            if isinstance(row, (tuple, dict)) or _is_built_button(row):
                row = [row]
            rows.append(
                [self.button(**item) if isinstance(item, dict) else item for item in row]
            )
        return ikm(rows)


rich_kit = RichKit()


def resolve_style(*args, **kwargs):
    return rich_kit.resolve_style(*args, **kwargs)


def button(*args, **kwargs):
    return rich_kit.button(*args, **kwargs)


def rich_ikm(*args, **kwargs):
    return rich_kit.rich_ikm(*args, **kwargs)