@tma.js/sdk 0.11.4 → 0.11.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/{types → dts}/components/CloudStorage/CloudStorage.d.ts +2 -2
  2. package/dist/{types → dts}/components/ThemeParams/ThemeParams.d.ts +2 -3
  3. package/dist/{types → dts}/components/WebApp/WebApp.d.ts +3 -3
  4. package/dist/{types → dts}/state/State.d.ts +1 -1
  5. package/dist/index.js +1412 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/index.umd.cjs +2 -0
  8. package/dist/index.umd.cjs.map +1 -0
  9. package/package.json +20 -25
  10. package/src/url.ts +1 -2
  11. package/dist/lib/browser.js +0 -2
  12. package/dist/lib/browser.js.map +0 -1
  13. package/dist/lib/index.cjs +0 -2
  14. package/dist/lib/index.cjs.map +0 -1
  15. package/dist/lib/index.mjs +0 -2
  16. package/dist/lib/index.mjs.map +0 -1
  17. /package/dist/{types → dts}/components/BackButton/BackButton.d.ts +0 -0
  18. /package/dist/{types → dts}/components/BackButton/index.d.ts +0 -0
  19. /package/dist/{types → dts}/components/BackButton/types.d.ts +0 -0
  20. /package/dist/{types → dts}/components/ClosingBehaviour/ClosingBehaviour.d.ts +0 -0
  21. /package/dist/{types → dts}/components/ClosingBehaviour/index.d.ts +0 -0
  22. /package/dist/{types → dts}/components/ClosingBehaviour/types.d.ts +0 -0
  23. /package/dist/{types → dts}/components/CloudStorage/index.d.ts +0 -0
  24. /package/dist/{types → dts}/components/HapticFeedback/HapticFeedback.d.ts +0 -0
  25. /package/dist/{types → dts}/components/HapticFeedback/index.d.ts +0 -0
  26. /package/dist/{types → dts}/components/InitData/InitData.d.ts +0 -0
  27. /package/dist/{types → dts}/components/InitData/index.d.ts +0 -0
  28. /package/dist/{types → dts}/components/MainButton/MainButton.d.ts +0 -0
  29. /package/dist/{types → dts}/components/MainButton/index.d.ts +0 -0
  30. /package/dist/{types → dts}/components/MainButton/types.d.ts +0 -0
  31. /package/dist/{types → dts}/components/Popup/Popup.d.ts +0 -0
  32. /package/dist/{types → dts}/components/Popup/index.d.ts +0 -0
  33. /package/dist/{types → dts}/components/Popup/types.d.ts +0 -0
  34. /package/dist/{types → dts}/components/Popup/utils.d.ts +0 -0
  35. /package/dist/{types → dts}/components/QRScanner/QRScanner.d.ts +0 -0
  36. /package/dist/{types → dts}/components/QRScanner/index.d.ts +0 -0
  37. /package/dist/{types → dts}/components/QRScanner/types.d.ts +0 -0
  38. /package/dist/{types → dts}/components/ThemeParams/index.d.ts +0 -0
  39. /package/dist/{types → dts}/components/ThemeParams/types.d.ts +0 -0
  40. /package/dist/{types → dts}/components/Viewport/Viewport.d.ts +0 -0
  41. /package/dist/{types → dts}/components/Viewport/index.d.ts +0 -0
  42. /package/dist/{types → dts}/components/Viewport/types.d.ts +0 -0
  43. /package/dist/{types → dts}/components/WebApp/index.d.ts +0 -0
  44. /package/dist/{types → dts}/components/WebApp/types.d.ts +0 -0
  45. /package/dist/{types → dts}/components/index.d.ts +0 -0
  46. /package/dist/{types → dts}/env.d.ts +0 -0
  47. /package/dist/{types → dts}/errors/MethodNotSupportedError.d.ts +0 -0
  48. /package/dist/{types → dts}/errors/ParameterNotSupportedError.d.ts +0 -0
  49. /package/dist/{types → dts}/errors/index.d.ts +0 -0
  50. /package/dist/{types → dts}/index.d.ts +0 -0
  51. /package/dist/{types → dts}/init/creators/createBackButton.d.ts +0 -0
  52. /package/dist/{types → dts}/init/creators/createClosingBehavior.d.ts +0 -0
  53. /package/dist/{types → dts}/init/creators/createMainButton.d.ts +0 -0
  54. /package/dist/{types → dts}/init/creators/createPostEvent.d.ts +0 -0
  55. /package/dist/{types → dts}/init/creators/createRequestIdGenerator.d.ts +0 -0
  56. /package/dist/{types → dts}/init/creators/createSyncedThemeParams.d.ts +0 -0
  57. /package/dist/{types → dts}/init/creators/createViewport.d.ts +0 -0
  58. /package/dist/{types → dts}/init/creators/createWebApp.d.ts +0 -0
  59. /package/dist/{types → dts}/init/creators/index.d.ts +0 -0
  60. /package/dist/{types → dts}/init/css.d.ts +0 -0
  61. /package/dist/{types → dts}/init/index.d.ts +0 -0
  62. /package/dist/{types → dts}/init/init.d.ts +0 -0
  63. /package/dist/{types → dts}/init/types.d.ts +0 -0
  64. /package/dist/{types → dts}/launch-params.d.ts +0 -0
  65. /package/dist/{types → dts}/state/index.d.ts +0 -0
  66. /package/dist/{types → dts}/state/types.d.ts +0 -0
  67. /package/dist/{types → dts}/storage.d.ts +0 -0
  68. /package/dist/{types → dts}/supports.d.ts +0 -0
  69. /package/dist/{types → dts}/theme-params.d.ts +0 -0
  70. /package/dist/{types → dts}/types.d.ts +0 -0
  71. /package/dist/{types → dts}/url.d.ts +0 -0
package/dist/index.js ADDED
@@ -0,0 +1,1412 @@
1
+ var nt = Object.defineProperty;
2
+ var it = (s, t, e) => t in s ? nt(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
3
+ var i = (s, t, e) => (it(s, typeof t != "symbol" ? t + "" : t, e), e);
4
+ import { array as at, string as P, json as j, rgb as w, searchParams as ct } from "@tma.js/parsing";
5
+ import { initData as ht } from "@tma.js/init-data";
6
+ import { supports as C, postEvent as l, on as S, off as F, request as g, detectSupportParams as pt, setDebug as ut, setTargetOrigin as lt, isIframe as dt } from "@tma.js/bridge";
7
+ import { compareVersions as gt, isRecord as bt, withTimeout as _t } from "@tma.js/utils";
8
+ import { EventEmitter as f } from "@tma.js/event-emitter";
9
+ import { isColorDark as J, isRGB as ft } from "@tma.js/colors";
10
+ function y(s, t) {
11
+ return (e) => C(t[e], s);
12
+ }
13
+ function mt(s, t) {
14
+ return (e) => {
15
+ const [o, r] = t[e];
16
+ return C(o, r, s);
17
+ };
18
+ }
19
+ class b {
20
+ constructor(t, e) {
21
+ this.state = t, this.ee = e;
22
+ }
23
+ emit(t, e) {
24
+ this.ee && this.ee.emit(t, e);
25
+ }
26
+ internalSet(t, e) {
27
+ return this.state[t] === e ? !1 : (this.state[t] = e, this.emit(`${t}Changed`, e), !0);
28
+ }
29
+ set(t, e) {
30
+ let o = !1;
31
+ if (typeof t == "string")
32
+ o = this.internalSet(t, e);
33
+ else
34
+ for (const r in t)
35
+ this.internalSet(r, t[r]) && (o = !0);
36
+ o && this.emit("changed");
37
+ }
38
+ get(t) {
39
+ return this.state[t];
40
+ }
41
+ }
42
+ class wt {
43
+ constructor(t, e, o = l) {
44
+ i(this, "ee", new f());
45
+ i(this, "state");
46
+ /**
47
+ * Adds event listener.
48
+ * @param event - event name.
49
+ * @param listener - event listener.
50
+ */
51
+ i(this, "on", (t, e) => {
52
+ if (t === "click")
53
+ return S("back_button_pressed", e);
54
+ this.ee.on(t, e);
55
+ });
56
+ /**
57
+ * Removes event listener.
58
+ * @param event - event name.
59
+ * @param listener - event listener.
60
+ */
61
+ i(this, "off", (t, e) => {
62
+ if (t === "click")
63
+ return F("back_button_pressed", e);
64
+ this.ee.off(t, e);
65
+ });
66
+ /**
67
+ * Checks if specified method is supported by current component.
68
+ */
69
+ i(this, "supports");
70
+ this.postEvent = o, this.state = new b({ isVisible: t }, this.ee), this.supports = y(e, {
71
+ show: "web_app_setup_back_button",
72
+ hide: "web_app_setup_back_button"
73
+ });
74
+ }
75
+ set isVisible(t) {
76
+ this.state.set("isVisible", t), this.postEvent("web_app_setup_back_button", { is_visible: t });
77
+ }
78
+ /**
79
+ * True if BackButton is currently visible.
80
+ */
81
+ get isVisible() {
82
+ return this.state.get("isVisible");
83
+ }
84
+ /**
85
+ * Hides the BackButton.
86
+ */
87
+ hide() {
88
+ this.isVisible = !1;
89
+ }
90
+ /**
91
+ * Shows the BackButton.
92
+ */
93
+ show() {
94
+ this.isVisible = !0;
95
+ }
96
+ }
97
+ class Ct {
98
+ constructor(t, e = l) {
99
+ i(this, "ee", new f());
100
+ i(this, "state");
101
+ /**
102
+ * Adds new event listener.
103
+ */
104
+ i(this, "on", this.ee.on.bind(this.ee));
105
+ /**
106
+ * Removes event listener.
107
+ */
108
+ i(this, "off", this.ee.off.bind(this.ee));
109
+ this.postEvent = e, this.state = new b({ isConfirmationNeeded: t }, this.ee);
110
+ }
111
+ set isConfirmationNeeded(t) {
112
+ this.state.set("isConfirmationNeeded", t), this.postEvent("web_app_setup_closing_behavior", { need_confirmation: t });
113
+ }
114
+ /**
115
+ * Returns true, if the confirmation dialog enabled while the user is trying
116
+ * to close the Web App.
117
+ */
118
+ get isConfirmationNeeded() {
119
+ return this.state.get("isConfirmationNeeded");
120
+ }
121
+ /**
122
+ * Disables the confirmation dialog while the user is trying to close the
123
+ * Web App.
124
+ */
125
+ disableConfirmation() {
126
+ this.isConfirmationNeeded = !1;
127
+ }
128
+ /**
129
+ * Enables the confirmation dialog while the user is trying to close the
130
+ * Web App.
131
+ */
132
+ enableConfirmation() {
133
+ this.isConfirmationNeeded = !0;
134
+ }
135
+ }
136
+ const yt = at().of(P());
137
+ function M(s, t) {
138
+ return s.reduce((e, o) => (e[o] = t, e), {});
139
+ }
140
+ class vt {
141
+ constructor(t, e, o = l) {
142
+ /**
143
+ * Checks if specified method is supported by current component.
144
+ */
145
+ i(this, "supports");
146
+ this.createRequestId = e, this.postEvent = o, this.supports = y(t, {
147
+ deleteKeys: "web_app_invoke_custom_method",
148
+ getKeys: "web_app_invoke_custom_method",
149
+ getValues: "web_app_invoke_custom_method",
150
+ saveValue: "web_app_invoke_custom_method"
151
+ });
152
+ }
153
+ /**
154
+ * Invokes custom method related to CloudStorage.
155
+ * @param method - method name.
156
+ * @param params - method parameters.
157
+ * @param options - execution options.
158
+ */
159
+ async invokeCustomMethod(t, e, o = {}) {
160
+ const { result: r, error: n } = await g(
161
+ "web_app_invoke_custom_method",
162
+ { method: t, params: e, req_id: this.createRequestId() },
163
+ "custom_method_invoked",
164
+ { ...o, postEvent: this.postEvent }
165
+ );
166
+ if (n)
167
+ throw new Error(typeof n == "string" ? n : `Unknown error: ${JSON.stringify(n)}`);
168
+ return r;
169
+ }
170
+ /**
171
+ * Deletes specified keys from the CloudStorage.
172
+ * @param keys - keys list.
173
+ * @param options - request execution options.
174
+ */
175
+ async deleteKeys(t, e) {
176
+ t.length !== 0 && await this.invokeCustomMethod("deleteStorageValues", { keys: t }, e);
177
+ }
178
+ /**
179
+ * Returns list of all keys presented in CloudStorage.
180
+ * @param options - request execution options.
181
+ */
182
+ async getKeys(t) {
183
+ const e = await this.invokeCustomMethod("getStorageKeys", {}, t);
184
+ return yt.parse(e);
185
+ }
186
+ /**
187
+ * Returns map, where key is one of the specified in keys argument, and value is according
188
+ * storage value.
189
+ * @param keys - keys list.
190
+ * @param options - request execution options.
191
+ */
192
+ async getValues(t, e) {
193
+ if (t.length === 0)
194
+ return M(t, "");
195
+ const o = j(
196
+ M(t, P())
197
+ // fixme
198
+ ), r = await this.invokeCustomMethod("getStorageValues", { keys: t }, e);
199
+ return o.parse(r);
200
+ }
201
+ /**
202
+ * Saves specified value by key.
203
+ * @param key - storage key.
204
+ * @param value - storage value.
205
+ * @param options - request execution options.
206
+ */
207
+ async saveValue(t, e, o) {
208
+ await this.invokeCustomMethod("saveStorageValue", { key: t, value: e }, o);
209
+ }
210
+ }
211
+ class kt {
212
+ constructor(t, e = l) {
213
+ /**
214
+ * Checks if specified method is supported by current component.
215
+ */
216
+ i(this, "supports");
217
+ this.postEvent = e, this.supports = y(t, {
218
+ impactOccurred: "web_app_trigger_haptic_feedback",
219
+ notificationOccurred: "web_app_trigger_haptic_feedback",
220
+ selectionChanged: "web_app_trigger_haptic_feedback"
221
+ });
222
+ }
223
+ /**
224
+ * A method tells that an impact occurred. The Telegram app may play the
225
+ * appropriate haptics based on style value passed.
226
+ * @param style - impact style.
227
+ */
228
+ impactOccurred(t) {
229
+ this.postEvent("web_app_trigger_haptic_feedback", { type: "impact", impact_style: t });
230
+ }
231
+ /**
232
+ * A method tells that a task or action has succeeded, failed, or produced
233
+ * a warning. The Telegram app may play the appropriate haptics based on
234
+ * type value passed.
235
+ * @param type - notification type.
236
+ */
237
+ notificationOccurred(t) {
238
+ this.postEvent("web_app_trigger_haptic_feedback", {
239
+ type: "notification",
240
+ notification_type: t
241
+ });
242
+ }
243
+ /**
244
+ * A method tells that the user has changed a selection. The Telegram app
245
+ * may play the appropriate haptics.
246
+ *
247
+ * Do not use this feedback when the user makes or confirms a selection;
248
+ * use it only when the selection changes.
249
+ */
250
+ selectionChanged() {
251
+ this.postEvent("web_app_trigger_haptic_feedback", { type: "selection_change" });
252
+ }
253
+ }
254
+ class Et {
255
+ constructor(t, e, o = {}) {
256
+ i(this, "state");
257
+ const {
258
+ chat: r = null,
259
+ canSendAfter: n = null,
260
+ user: c = null,
261
+ queryId: a = null,
262
+ receiver: p = null,
263
+ startParam: h = null
264
+ } = o;
265
+ this.state = new b({
266
+ authDate: t,
267
+ canSendAfter: n,
268
+ chat: r,
269
+ user: c,
270
+ queryId: a,
271
+ receiver: p,
272
+ startParam: h,
273
+ hash: e
274
+ });
275
+ }
276
+ /**
277
+ * Init data generation date.
278
+ */
279
+ get authDate() {
280
+ return this.state.get("authDate");
281
+ }
282
+ /**
283
+ * Date after which a message can be sent via the answerWebAppQuery
284
+ * method.
285
+ * @see https://core.telegram.org/bots/api#answerwebappquery
286
+ */
287
+ get canSendAfter() {
288
+ return this.state.get("canSendAfter");
289
+ }
290
+ /**
291
+ * An object containing data about the chat where the bot was
292
+ * launched via the attachment menu. Returned for supergroups, channels and
293
+ * group chats – only for Web Apps launched via the attachment menu.
294
+ */
295
+ get chat() {
296
+ return this.state.get("chat");
297
+ }
298
+ /**
299
+ * A hash of all passed parameters, which the bot server can use to
300
+ * check their validity.
301
+ * @see https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
302
+ */
303
+ get hash() {
304
+ return this.state.get("hash");
305
+ }
306
+ /**
307
+ * A unique identifier for the Web App session, required for sending
308
+ * messages via the `answerWebAppQuery` method.
309
+ * @see https://core.telegram.org/bots/api#answerwebappquery
310
+ */
311
+ get queryId() {
312
+ return this.state.get("queryId");
313
+ }
314
+ /**
315
+ * An object containing data about the chat partner of the current
316
+ * user in the chat where the bot was launched via the attachment menu.
317
+ * Returned only for private chats and only for Web Apps launched
318
+ * via the attachment menu.
319
+ */
320
+ get receiver() {
321
+ return this.state.get("receiver");
322
+ }
323
+ /**
324
+ * The value of the `startattach` parameter, passed via link. Only
325
+ * returned for Web Apps when launched from the attachment menu via link.
326
+ */
327
+ get startParam() {
328
+ return this.state.get("startParam");
329
+ }
330
+ /**
331
+ * An object containing data about the current user.
332
+ */
333
+ get user() {
334
+ return this.state.get("user");
335
+ }
336
+ }
337
+ class xt {
338
+ constructor(t, e, o, r, n, c, a = l) {
339
+ i(this, "ee", new f());
340
+ i(this, "state");
341
+ /**
342
+ * Adds new event listener.
343
+ * FIXME: Event 'main_button_pressed' is still being received on Android
344
+ * even if the main button is disabled.
345
+ * Issue: https://github.com/Telegram-Mini-Apps/tma.js/issues/3
346
+ * @param event - event name.
347
+ * @param listener - event listener.
348
+ */
349
+ i(this, "on", (t, e) => {
350
+ if (t === "click")
351
+ return S("main_button_pressed", e);
352
+ this.ee.on(t, e);
353
+ });
354
+ /**
355
+ * Removes event listener.
356
+ * @param event - event name.
357
+ * @param listener - event listener.
358
+ */
359
+ i(this, "off", (t, e) => {
360
+ if (t === "click")
361
+ return F("main_button_pressed", e);
362
+ this.ee.off(t, e);
363
+ });
364
+ this.postEvent = a, this.state = new b({
365
+ backgroundColor: t,
366
+ isEnabled: e,
367
+ isVisible: o,
368
+ isProgressVisible: r,
369
+ text: n,
370
+ textColor: c
371
+ }, this.ee);
372
+ }
373
+ set isEnabled(t) {
374
+ this.state.set("isEnabled", t), this.commit();
375
+ }
376
+ /**
377
+ * Returns true in case, MainButton is currently enabled.
378
+ */
379
+ get isEnabled() {
380
+ return this.state.get("isEnabled");
381
+ }
382
+ set isProgressVisible(t) {
383
+ this.state.set("isProgressVisible", t), this.commit();
384
+ }
385
+ /**
386
+ * Returns true in case, MainButton loading progress is currently visible.
387
+ */
388
+ get isProgressVisible() {
389
+ return this.state.get("isProgressVisible");
390
+ }
391
+ set isVisible(t) {
392
+ this.state.set("isVisible", t), this.commit();
393
+ }
394
+ /**
395
+ * Returns true in case, MainButton is currently visible.
396
+ */
397
+ get isVisible() {
398
+ return this.state.get("isVisible");
399
+ }
400
+ /**
401
+ * Sends current local button state to Telegram application.
402
+ */
403
+ commit() {
404
+ this.text !== "" && this.postEvent("web_app_setup_main_button", {
405
+ is_visible: this.isVisible,
406
+ is_active: this.isEnabled,
407
+ is_progress_visible: this.isProgressVisible,
408
+ text: this.text,
409
+ color: this.backgroundColor,
410
+ text_color: this.textColor
411
+ });
412
+ }
413
+ /**
414
+ * Returns current main button background color.
415
+ */
416
+ get backgroundColor() {
417
+ return this.state.get("backgroundColor");
418
+ }
419
+ /**
420
+ * Returns current main button text.
421
+ */
422
+ get text() {
423
+ return this.state.get("text");
424
+ }
425
+ /**
426
+ * Returns current main button text color.
427
+ */
428
+ get textColor() {
429
+ return this.state.get("textColor");
430
+ }
431
+ /**
432
+ * Disables button. Returns current button instance for chaining.
433
+ */
434
+ disable() {
435
+ return this.isEnabled = !1, this;
436
+ }
437
+ /**
438
+ * Enables button. Returns current button instance for chaining.
439
+ */
440
+ enable() {
441
+ return this.isEnabled = !0, this;
442
+ }
443
+ /**
444
+ * Hides button. Returns current button instance for chaining.
445
+ */
446
+ hide() {
447
+ return this.isVisible = !1, this;
448
+ }
449
+ /**
450
+ * Hides button progress. Returns current button instance for chaining.
451
+ */
452
+ hideProgress() {
453
+ return this.isProgressVisible = !1, this;
454
+ }
455
+ /**
456
+ * Shows the button. Note that opening the Web App from the attachment
457
+ * menu hides the main button until the user interacts with the Web App
458
+ * interface.
459
+ *
460
+ * Returns current button instance for chaining.
461
+ */
462
+ show() {
463
+ return this.isVisible = !0, this;
464
+ }
465
+ /**
466
+ * A method to show a loading indicator on the button.
467
+ * It is recommended to display loading progress if the action tied to the
468
+ * button may take a long time.
469
+ *
470
+ * Returns current button instance for chaining.
471
+ */
472
+ showProgress() {
473
+ return this.isProgressVisible = !0, this;
474
+ }
475
+ /**
476
+ * Sets new main button text. Returns current button instance for chaining.
477
+ * Minimal length for text is 1 symbol, and maximum is 64 symbols.
478
+ *
479
+ * Returns current button instance for chaining.
480
+ * @param value - new text.
481
+ */
482
+ setText(t) {
483
+ return this.state.set("text", t), this.commit(), this;
484
+ }
485
+ /**
486
+ * Sets new main button text color. Returns current button instance for
487
+ * chaining.
488
+ *
489
+ * Returns current button instance for chaining.
490
+ * @param value - new text color.
491
+ */
492
+ setTextColor(t) {
493
+ return this.state.set("textColor", t), this.commit(), this;
494
+ }
495
+ /**
496
+ * Updates current button color. Returns current button instance for
497
+ * chaining.
498
+ *
499
+ * Returns current button instance for chaining.
500
+ * @param value - color to set.
501
+ */
502
+ setBackgroundColor(t) {
503
+ return this.state.set("backgroundColor", t), this.commit(), this;
504
+ }
505
+ }
506
+ function Pt(s) {
507
+ const t = s.message.trim(), e = (s.title || "").trim(), o = s.buttons || [];
508
+ let r;
509
+ if (e.length > 64)
510
+ throw new Error(`Title has incorrect size: ${e.length}`);
511
+ if (t.length === 0 || t.length > 256)
512
+ throw new Error(`Message has incorrect size: ${t.length}`);
513
+ if (o.length > 3)
514
+ throw new Error(`Buttons have incorrect size: ${o.length}`);
515
+ return o.length === 0 ? r = [{ type: "close", id: "" }] : r = o.map((n) => {
516
+ const { id: c = "" } = n;
517
+ if (c.length > 64)
518
+ throw new Error(`Button ID has incorrect size: ${c}`);
519
+ if (n.type === void 0 || n.type === "default" || n.type === "destructive") {
520
+ const a = n.text.trim();
521
+ if (a.length === 0 || a.length > 64) {
522
+ const p = n.type || "default";
523
+ throw new Error(`Button text with type "${p}" has incorrect size: ${n.text.length}`);
524
+ }
525
+ return { ...n, text: a, id: c };
526
+ }
527
+ return { ...n, id: c };
528
+ }), { title: e, message: t, buttons: r };
529
+ }
530
+ class Vt {
531
+ constructor(t, e = l) {
532
+ i(this, "ee", new f());
533
+ i(this, "state");
534
+ /**
535
+ * Adds new event listener.
536
+ */
537
+ i(this, "on", this.ee.on.bind(this.ee));
538
+ /**
539
+ * Removes event listener.
540
+ */
541
+ i(this, "off", this.ee.off.bind(this.ee));
542
+ /**
543
+ * Checks if specified method is supported by current component.
544
+ */
545
+ i(this, "supports");
546
+ this.postEvent = e, this.state = new b({ isOpened: !1 }, this.ee), this.supports = y(t, { open: "web_app_open_popup" });
547
+ }
548
+ /**
549
+ * Shows whether popup is currently opened.
550
+ */
551
+ get isOpened() {
552
+ return this.state.get("isOpened");
553
+ }
554
+ /**
555
+ * A method that shows a native popup described by the `params` argument.
556
+ * Promise will be resolved when popup is closed. Resolved value will have
557
+ * an identifier of pressed button.
558
+ *
559
+ * In case, user clicked outside the popup or clicked top right popup close
560
+ * button, null will be returned.
561
+ *
562
+ * FIXME: In desktop, this function may work incorrectly.
563
+ * Issue: https://github.com/Telegram-Mini-Apps/tma.js/issues/7
564
+ * @param params - popup parameters.
565
+ * @throws {Error} Popup is already opened.
566
+ */
567
+ async open(t) {
568
+ if (this.isOpened)
569
+ throw new Error("Popup is already opened.");
570
+ this.state.set("isOpened", !0);
571
+ try {
572
+ const { button_id: e = null } = await g(
573
+ "web_app_open_popup",
574
+ Pt(t),
575
+ "popup_closed",
576
+ { postEvent: this.postEvent }
577
+ );
578
+ return e;
579
+ } finally {
580
+ this.state.set("isOpened", !1);
581
+ }
582
+ }
583
+ }
584
+ class St {
585
+ constructor(t, e = l) {
586
+ i(this, "ee", new f());
587
+ i(this, "state");
588
+ /**
589
+ * Adds new event listener.
590
+ */
591
+ i(this, "on", this.ee.on.bind(this.ee));
592
+ /**
593
+ * Removes event listener.
594
+ */
595
+ i(this, "off", this.ee.off.bind(this.ee));
596
+ /**
597
+ * Checks if specified method is supported by current component.
598
+ */
599
+ i(this, "supports");
600
+ this.postEvent = e, this.state = new b({ isOpened: !1 }, this.ee), this.supports = y(t, {
601
+ close: "web_app_close_scan_qr_popup",
602
+ open: "web_app_open_scan_qr_popup"
603
+ });
604
+ }
605
+ /**
606
+ * Closes scanner.
607
+ */
608
+ close() {
609
+ this.postEvent("web_app_close_scan_qr_popup"), this.isOpened = !1;
610
+ }
611
+ set isOpened(t) {
612
+ this.state.set("isOpened", t);
613
+ }
614
+ /**
615
+ * Returns true in case, QR scanner is currently opened.
616
+ */
617
+ get isOpened() {
618
+ return this.state.get("isOpened");
619
+ }
620
+ /**
621
+ * Opens scanner with specified title shown to user. Method returns promise
622
+ * with scanned QR content in case, it was scanned. It will contain null in
623
+ * case, scanner was closed.
624
+ * @param text - title to display.
625
+ */
626
+ async open(t) {
627
+ if (this.isOpened)
628
+ throw new Error("QR scanner is already opened.");
629
+ this.isOpened = !0;
630
+ try {
631
+ const e = await g(
632
+ "web_app_open_scan_qr_popup",
633
+ { text: t },
634
+ ["qr_text_received", "scan_qr_popup_closed"],
635
+ { postEvent: this.postEvent }
636
+ );
637
+ return typeof e == "object" && typeof e.data == "string" ? e.data : null;
638
+ } finally {
639
+ this.isOpened = !1;
640
+ }
641
+ }
642
+ }
643
+ const qt = j({
644
+ backgroundColor: { type: w().optional(), from: "bg_color" },
645
+ buttonColor: { type: w().optional(), from: "button_color" },
646
+ buttonTextColor: { type: w().optional(), from: "button_text_color" },
647
+ hintColor: { type: w().optional(), from: "hint_color" },
648
+ linkColor: { type: w().optional(), from: "link_color" },
649
+ textColor: { type: w().optional(), from: "text_color" },
650
+ secondaryBackgroundColor: { type: w().optional(), from: "secondary_bg_color" }
651
+ });
652
+ function $(s) {
653
+ return qt.parse(s);
654
+ }
655
+ function U(s) {
656
+ const {
657
+ backgroundColor: t = null,
658
+ buttonTextColor: e = null,
659
+ buttonColor: o = null,
660
+ hintColor: r = null,
661
+ linkColor: n = null,
662
+ textColor: c = null,
663
+ secondaryBackgroundColor: a = null
664
+ } = s;
665
+ return {
666
+ backgroundColor: t,
667
+ buttonTextColor: e,
668
+ buttonColor: o,
669
+ hintColor: r,
670
+ linkColor: n,
671
+ textColor: c,
672
+ secondaryBackgroundColor: a
673
+ };
674
+ }
675
+ class q {
676
+ constructor(t) {
677
+ i(this, "ee", new f());
678
+ i(this, "state");
679
+ /**
680
+ * Adds new event listener.
681
+ */
682
+ i(this, "on", this.ee.on.bind(this.ee));
683
+ /**
684
+ * Removes event listener.
685
+ */
686
+ i(this, "off", this.ee.off.bind(this.ee));
687
+ this.state = new b(U(t), this.ee);
688
+ }
689
+ /**
690
+ * Requests fresh information about current theme.
691
+ * FIXME: Be careful using this function in desktop version of Telegram as
692
+ * long as method web_app_request_theme does not work on `macos` platform.
693
+ * @param options - method options.
694
+ */
695
+ static async request(t = {}) {
696
+ const { timeout: e = 1e3, ...o } = t, r = await g("web_app_request_theme", "theme_changed", {
697
+ ...o,
698
+ timeout: e
699
+ });
700
+ return $(r.theme_params);
701
+ }
702
+ /**
703
+ * Synchronizes specified instance of ThemeParams with the actual value in the native
704
+ * application.
705
+ * @param themeParams - ThemeParams instance.
706
+ */
707
+ static sync(t) {
708
+ S("theme_changed", (e) => {
709
+ t.state.set(U($(e.theme_params)));
710
+ });
711
+ }
712
+ /**
713
+ * Returns instance of ThemeParams which is synchronized with external
714
+ * environment.
715
+ * @param options - method options.
716
+ */
717
+ static async synced(t) {
718
+ const e = await this.request(t), o = new q(e);
719
+ return this.sync(o), o;
720
+ }
721
+ /**
722
+ * Returns background color.
723
+ */
724
+ get backgroundColor() {
725
+ return this.state.get("backgroundColor");
726
+ }
727
+ /**
728
+ * Returns button color.
729
+ */
730
+ get buttonColor() {
731
+ return this.state.get("buttonColor");
732
+ }
733
+ /**
734
+ * Returns button text color.
735
+ */
736
+ get buttonTextColor() {
737
+ return this.state.get("buttonTextColor");
738
+ }
739
+ /**
740
+ * Returns hint color.
741
+ */
742
+ get hintColor() {
743
+ return this.state.get("hintColor");
744
+ }
745
+ /**
746
+ * Returns true in case, current color scheme is recognized as dark. This
747
+ * value is calculated according to theme background color.
748
+ */
749
+ get isDark() {
750
+ return this.backgroundColor === null || J(this.backgroundColor);
751
+ }
752
+ /**
753
+ * Returns current link color.
754
+ */
755
+ get linkColor() {
756
+ return this.state.get("linkColor");
757
+ }
758
+ /**
759
+ * Returns secondary background color.
760
+ */
761
+ get secondaryBackgroundColor() {
762
+ return this.state.get("secondaryBackgroundColor");
763
+ }
764
+ /**
765
+ * Returns text color.
766
+ */
767
+ get textColor() {
768
+ return this.state.get("textColor");
769
+ }
770
+ }
771
+ function x(s) {
772
+ return s < 0 ? 0 : s;
773
+ }
774
+ class V {
775
+ constructor(t, e, o, r, n = l) {
776
+ i(this, "ee", new f());
777
+ i(this, "state");
778
+ /**
779
+ * Adds new event listener.
780
+ */
781
+ i(this, "on", this.ee.on.bind(this.ee));
782
+ /**
783
+ * Removes event listener.
784
+ */
785
+ i(this, "off", this.ee.off.bind(this.ee));
786
+ this.postEvent = n, this.state = new b({
787
+ height: x(t),
788
+ isExpanded: r,
789
+ stableHeight: x(o),
790
+ width: x(e)
791
+ }, this.ee);
792
+ }
793
+ /**
794
+ * Requests fresh information about current viewport.
795
+ * FIXME: Be careful using this function in desktop version of Telegram as
796
+ * long as method web_app_request_viewport does not work on `macos` platform.
797
+ * @see Issue: https://github.com/Telegram-Mini-Apps/tma.js/issues/5
798
+ * @param options - method options.
799
+ */
800
+ static async request(t = {}) {
801
+ const { timeout: e = 1e3, ...o } = t, {
802
+ is_expanded: r,
803
+ is_state_stable: n,
804
+ ...c
805
+ } = await g("web_app_request_viewport", "viewport_changed", {
806
+ ...o,
807
+ timeout: e
808
+ });
809
+ return { ...c, isExpanded: r, isStateStable: n };
810
+ }
811
+ /**
812
+ * Synchronizes specified instance of Viewport with the actual value in the native
813
+ * application.
814
+ * @param viewport - Viewport instance.
815
+ */
816
+ static sync(t) {
817
+ S("viewport_changed", (e) => {
818
+ const {
819
+ height: o,
820
+ width: r,
821
+ is_expanded: n,
822
+ is_state_stable: c
823
+ } = e, a = x(o);
824
+ t.state.set({
825
+ height: a,
826
+ isExpanded: n,
827
+ width: x(r),
828
+ stableHeight: c ? a : void 0
829
+ });
830
+ });
831
+ }
832
+ /**
833
+ * Returns initialized instance of Viewport which is synchronized with
834
+ * its actual state in Web Apps.
835
+ * @param options - method options.
836
+ */
837
+ static async synced(t = {}) {
838
+ const { height: e, isExpanded: o, width: r } = await this.request(t), n = new V(e, r, e, o, t.postEvent);
839
+ return this.sync(n), n;
840
+ }
841
+ /**
842
+ * The current height of the visible area of the Web App.
843
+ *
844
+ * The application can display just the top part of the Web App, with its
845
+ * lower part remaining outside the screen area. From this position, the
846
+ * user can "pull" the Web App to its maximum height, while the bot can do
847
+ * the same by calling `expand` method. As the position of the Web App
848
+ * changes, the current height value of the visible area will be updated
849
+ * in real time.
850
+ *
851
+ * Please note that the refresh rate of this value is not sufficient
852
+ * to smoothly follow the lower border of the window. It should not be
853
+ * used to pin interface elements to the bottom of the visible area. It's
854
+ * more appropriate to use the value of the `stableHeight`
855
+ * field for this purpose.
856
+ *
857
+ * @see init
858
+ * @see expand
859
+ * @see stableHeight
860
+ */
861
+ get height() {
862
+ return this.state.get("height");
863
+ }
864
+ /**
865
+ * The height of the visible area of the Web App in its last stable state.
866
+ *
867
+ * The application can display just the top part of the Web App, with its
868
+ * lower part remaining outside the screen area. From this position,
869
+ * the user can "pull" the Web App to its maximum height, while the bot can
870
+ * do the same by calling `expand` method.
871
+ *
872
+ * Unlike the value of `height`, the value of `stableHeight`
873
+ * does not change as the position of the Web App changes with user
874
+ * gestures or during animations. The value of `stableHeight`
875
+ * will be updated after all gestures and animations are completed and
876
+ * the Web App reaches its final size.
877
+ *
878
+ * @see init
879
+ * @see expand
880
+ * @see height
881
+ */
882
+ get stableHeight() {
883
+ return this.state.get("stableHeight");
884
+ }
885
+ /**
886
+ * Returns true if the Web App is expanded to the maximum available height.
887
+ * Otherwise, if the Web App occupies part of the screen and can be expanded
888
+ * to the full height using `expand` method.
889
+ * @see expand
890
+ */
891
+ get isExpanded() {
892
+ return this.state.get("isExpanded");
893
+ }
894
+ /**
895
+ * Current viewport width.
896
+ */
897
+ get width() {
898
+ return this.state.get("width");
899
+ }
900
+ /**
901
+ * A method that expands the Web App to the maximum available height. To
902
+ * find out if the Web App is expanded to the maximum height, refer to the
903
+ * value of the `isExpanded`.
904
+ * @see isExpanded
905
+ */
906
+ expand() {
907
+ this.state.set("isExpanded", !0), this.postEvent("web_app_expand");
908
+ }
909
+ /**
910
+ * Returns true in case current viewport height is stable and is not going to
911
+ * change in the next moment.
912
+ */
913
+ get isStable() {
914
+ return this.stableHeight === this.height;
915
+ }
916
+ }
917
+ function T(s) {
918
+ const t = document.createElement("a");
919
+ if (t.href = s, t.protocol !== "http:" && t.protocol !== "https:")
920
+ throw Error(
921
+ `URL protocol is not supported by OS, or link has not allowed protocol: ${t.protocol}`
922
+ );
923
+ return t.href;
924
+ }
925
+ class Ot {
926
+ constructor(t, e, o, r, n, c = l) {
927
+ i(this, "ee", new f());
928
+ i(this, "state");
929
+ /**
930
+ * Adds new event listener.
931
+ */
932
+ i(this, "on", this.ee.on.bind(this.ee));
933
+ /**
934
+ * Removes event listener.
935
+ */
936
+ i(this, "off", this.ee.off.bind(this.ee));
937
+ /**
938
+ * Checks if specified method is supported by current component.
939
+ */
940
+ i(this, "supports");
941
+ /**
942
+ * Checks if specified method parameter is supported by current component.
943
+ */
944
+ i(this, "supportsParam");
945
+ this.currentVersion = o, this.currentPlatform = r, this.createRequestId = n, this.postEvent = c, this.state = new b({
946
+ backgroundColor: e,
947
+ headerColor: t
948
+ }, this.ee), this.supports = y(o, {
949
+ openInvoice: "web_app_open_invoice",
950
+ readTextFromClipboard: "web_app_read_text_from_clipboard",
951
+ setHeaderColor: "web_app_set_header_color",
952
+ setBackgroundColor: "web_app_set_background_color",
953
+ requestPhoneAccess: "web_app_request_phone",
954
+ requestWriteAccess: "web_app_request_write_access"
955
+ }), this.supportsParam = mt(o, {
956
+ "setHeaderColor.color": ["web_app_set_header_color", "color"],
957
+ "openLink.tryInstantView": ["web_app_open_link", "try_instant_view"]
958
+ });
959
+ }
960
+ /**
961
+ * Returns current application background color.
962
+ */
963
+ get backgroundColor() {
964
+ return this.state.get("backgroundColor");
965
+ }
966
+ /**
967
+ * Returns current application color scheme. This value is
968
+ * computed based on the current background color.
969
+ */
970
+ get colorScheme() {
971
+ return J(this.backgroundColor) ? "dark" : "light";
972
+ }
973
+ /**
974
+ * Closes the Web App.
975
+ */
976
+ close() {
977
+ this.postEvent("web_app_close");
978
+ }
979
+ /**
980
+ * Returns current application header color.
981
+ */
982
+ get headerColor() {
983
+ return this.state.get("headerColor");
984
+ }
985
+ /**
986
+ * Returns true if passed version is more than or equal to current
987
+ * Web App version.
988
+ * @param version - compared version.
989
+ */
990
+ isVersionAtLeast(t) {
991
+ return gt(t, this.version) >= 0;
992
+ }
993
+ /**
994
+ * Opens a link in an external browser. The Web App will not be closed.
995
+ *
996
+ * Note that this method can be called only in response to the user
997
+ * interaction with the Web App interface (e.g. click inside the Web App
998
+ * or on the main button).
999
+ * @param url - URL to be opened.
1000
+ * @param tryInstantView
1001
+ */
1002
+ openLink(t, e) {
1003
+ const o = T(t);
1004
+ if (!C("web_app_open_link", this.version)) {
1005
+ window.open(o, "_blank");
1006
+ return;
1007
+ }
1008
+ return this.postEvent("web_app_open_link", {
1009
+ url: o,
1010
+ ...typeof e == "boolean" ? { try_instant_view: e } : {}
1011
+ });
1012
+ }
1013
+ /**
1014
+ * Opens a Telegram link inside Telegram app. The Web App will be closed.
1015
+ * It expects passing link in full format, with hostname "t.me".
1016
+ * @param url - URL to be opened.
1017
+ * @throws {Error} URL has not allowed hostname.
1018
+ */
1019
+ openTelegramLink(t) {
1020
+ const { hostname: e, pathname: o, search: r } = new URL(T(t));
1021
+ if (e !== "t.me")
1022
+ throw new Error(`URL has not allowed hostname: ${e}. Only "t.me" is allowed`);
1023
+ if (!C("web_app_open_tg_link", this.version)) {
1024
+ window.location.href = t;
1025
+ return;
1026
+ }
1027
+ return this.postEvent("web_app_open_tg_link", { path_full: o + r });
1028
+ }
1029
+ /**
1030
+ * Opens an invoice using its url. It expects passing link in full format,
1031
+ * with hostname "t.me".
1032
+ * @param url - invoice URL.
1033
+ */
1034
+ async openInvoice(t) {
1035
+ const { hostname: e, pathname: o } = new URL(T(t));
1036
+ if (e !== "t.me")
1037
+ throw new Error(`Incorrect hostname: ${e}`);
1038
+ const r = o.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);
1039
+ if (r === null)
1040
+ throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/slug" or "/$slug"');
1041
+ const [, , n] = r;
1042
+ return (await g("web_app_open_invoice", { slug: n }, "invoice_closed", {
1043
+ postEvent: this.postEvent,
1044
+ capture: ({ slug: a }) => n === a
1045
+ })).status;
1046
+ }
1047
+ /**
1048
+ * Returns current Web App platform.
1049
+ */
1050
+ get platform() {
1051
+ return this.currentPlatform;
1052
+ }
1053
+ /**
1054
+ * Informs the Telegram app that the Web App is ready to be displayed.
1055
+ *
1056
+ * It is recommended to call this method as early as possible, as soon as
1057
+ * all essential interface elements loaded. Once this method called,
1058
+ * the loading placeholder is hidden and the Web App shown.
1059
+ *
1060
+ * If the method not called, the placeholder will be hidden only when
1061
+ * the page fully loaded.
1062
+ */
1063
+ ready() {
1064
+ this.postEvent("web_app_ready");
1065
+ }
1066
+ /**
1067
+ * Reads text from clipboard and returns string or null. null is returned
1068
+ * in cases:
1069
+ * - Value in clipboard is not text
1070
+ * - Access to clipboard is not allowed
1071
+ */
1072
+ async readTextFromClipboard() {
1073
+ const { data: t = null } = await g(
1074
+ "web_app_read_text_from_clipboard",
1075
+ { req_id: this.createRequestId() },
1076
+ "clipboard_text_received",
1077
+ { postEvent: this.postEvent }
1078
+ );
1079
+ return t;
1080
+ }
1081
+ /**
1082
+ * Requests current user phone access.
1083
+ */
1084
+ async requestPhoneAccess() {
1085
+ const { status: t } = await g("web_app_request_phone", "phone_requested", {
1086
+ postEvent: this.postEvent
1087
+ });
1088
+ return t;
1089
+ }
1090
+ /**
1091
+ * Requests write message access to current user.
1092
+ */
1093
+ async requestWriteAccess() {
1094
+ const { status: t } = await g("web_app_request_write_access", "write_access_requested", {
1095
+ postEvent: this.postEvent
1096
+ });
1097
+ return t;
1098
+ }
1099
+ /**
1100
+ * A method used to send data to the bot. When this method called, a
1101
+ * service message sent to the bot containing the data of the
1102
+ * length up to 4096 bytes, and the Web App closed. See the field
1103
+ * `web_app_data` in the class Message.
1104
+ *
1105
+ * This method is only available for Web Apps launched via a Keyboard button.
1106
+ * @param data - data to send to bot.
1107
+ * @throws {Error} data has incorrect size.
1108
+ */
1109
+ sendData(t) {
1110
+ const { size: e } = new Blob([t]);
1111
+ if (e === 0 || e > 4096)
1112
+ throw new Error(`Passed data has incorrect size: ${e}`);
1113
+ this.postEvent("web_app_data_send", { data: t });
1114
+ }
1115
+ /**
1116
+ * Updates current application header color.
1117
+ * FIXME: Has no effect on desktop, works incorrectly on Android.
1118
+ * Issues:
1119
+ * https://github.com/Telegram-Mini-Apps/tma.js/issues/9
1120
+ * https://github.com/Telegram-Mini-Apps/tma.js/issues/8
1121
+ * @param color - color key or RGB color.
1122
+ */
1123
+ setHeaderColor(t) {
1124
+ this.postEvent("web_app_set_header_color", ft(t) ? { color: t } : { color_key: t }), this.state.set("headerColor", t);
1125
+ }
1126
+ /**
1127
+ * Updates current application background color.
1128
+ * FIXME: Has no effect on desktop, works incorrectly in Android.
1129
+ * Issues:
1130
+ * https://github.com/Telegram-Mini-Apps/tma.js/issues/9
1131
+ * https://github.com/Telegram-Mini-Apps/tma.js/issues/8
1132
+ * @param color - RGB color.
1133
+ */
1134
+ setBackgroundColor(t) {
1135
+ this.postEvent("web_app_set_background_color", { color: t }), this.state.set("backgroundColor", t);
1136
+ }
1137
+ /**
1138
+ * Current Web App version. This property is used by other components to check if
1139
+ * some functionality is available on current device.
1140
+ */
1141
+ get version() {
1142
+ return this.currentVersion;
1143
+ }
1144
+ }
1145
+ function G(s) {
1146
+ return `telegram-web-apps-${s}`;
1147
+ }
1148
+ function v(s, t) {
1149
+ sessionStorage.setItem(G(s), JSON.stringify(t));
1150
+ }
1151
+ function k(s) {
1152
+ const t = sessionStorage.getItem(G(s));
1153
+ return t ? JSON.parse(t) : null;
1154
+ }
1155
+ const Bt = ct({
1156
+ version: { type: P(), from: "tgWebAppVersion" },
1157
+ initData: { type: ht.optional(), from: "tgWebAppData" },
1158
+ initDataRaw: { type: P().optional(), from: "tgWebAppData" },
1159
+ platform: { type: P(), from: "tgWebAppPlatform" },
1160
+ themeParams: { type: $, from: "tgWebAppThemeParams" }
1161
+ });
1162
+ function R(s) {
1163
+ return Bt.parse(s);
1164
+ }
1165
+ function Q() {
1166
+ const s = [];
1167
+ try {
1168
+ const t = window.location.hash.slice(1), e = R(t);
1169
+ return v("launch-params", t), e;
1170
+ } catch (t) {
1171
+ s.push(t instanceof Error ? t.message : "unknown error");
1172
+ }
1173
+ try {
1174
+ const t = k("launch-params");
1175
+ if (t)
1176
+ return R(t);
1177
+ s.push("Launch params are missing in local storage");
1178
+ } catch (t) {
1179
+ s.push(t instanceof Error ? t.message : "unknown error");
1180
+ }
1181
+ throw new Error(`Unable to extract launch params. Errors: "${s.join('", "')}"`);
1182
+ }
1183
+ function Z(s, t) {
1184
+ document.documentElement.style.setProperty(s, t);
1185
+ }
1186
+ function d(s, t) {
1187
+ t !== null && Z(s, t);
1188
+ }
1189
+ function K(s, t) {
1190
+ Z(s, `${t}px`);
1191
+ }
1192
+ function At(s) {
1193
+ const {
1194
+ backgroundColor: t,
1195
+ buttonTextColor: e,
1196
+ secondaryBackgroundColor: o,
1197
+ hintColor: r,
1198
+ buttonColor: n,
1199
+ linkColor: c,
1200
+ textColor: a
1201
+ } = s;
1202
+ d("--tg-theme-bg-color", t), d("--tg-theme-button-color", n), d("--tg-theme-button-text-color", e), d("--tg-theme-hint-color", r), d("--tg-theme-link-color", c), d("--tg-theme-secondary-bg-color", o), d("--tg-theme-text-color", a);
1203
+ }
1204
+ function Tt(s, t) {
1205
+ const { backgroundColor: e, secondaryBackgroundColor: o } = t, { backgroundColor: r, headerColor: n } = s;
1206
+ d("--tg-bg-color", r), d("--tg-header-color", n === "bg_color" ? e : o);
1207
+ }
1208
+ function $t(s) {
1209
+ const t = () => At(s);
1210
+ s.on("changed", t), t();
1211
+ }
1212
+ function Rt(s, t) {
1213
+ const e = () => Tt(s, t);
1214
+ t.on("changed", e), s.on("backgroundColorChanged", e), s.on("headerColorChanged", e), e();
1215
+ }
1216
+ function Ht(s) {
1217
+ const t = () => {
1218
+ K("--tg-viewport-height", s.height);
1219
+ }, e = () => {
1220
+ K("--tg-viewport-stable-height", s.stableHeight);
1221
+ };
1222
+ s.on("heightChanged", t), s.on("stableHeightChanged", e), t(), e();
1223
+ }
1224
+ function It(s) {
1225
+ return typeof s == "boolean" ? s ? { themeParams: !0, viewport: !0, webApp: !0 } : {} : s;
1226
+ }
1227
+ function Dt(s, t) {
1228
+ const { isVisible: e = !1 } = k("back-button") || {}, o = new wt(e, s, t);
1229
+ return o.on("isVisibleChanged", () => {
1230
+ v("back-button", { isVisible: o.isVisible });
1231
+ }), o;
1232
+ }
1233
+ function Wt(s) {
1234
+ const { isConfirmationNeeded: t = !1 } = k("closing-behavior") || {}, e = new Ct(t, s);
1235
+ return e.on("isConfirmationNeededChanged", () => v("closing-behavior", {
1236
+ isConfirmationNeeded: e.isConfirmationNeeded
1237
+ })), e;
1238
+ }
1239
+ function Lt(s, t, e) {
1240
+ const {
1241
+ backgroundColor: o = s,
1242
+ isEnabled: r = !1,
1243
+ isVisible: n = !1,
1244
+ isProgressVisible: c = !1,
1245
+ textColor: a = t,
1246
+ text: p = ""
1247
+ } = k("main-button") || {}, h = new xt(
1248
+ o,
1249
+ r,
1250
+ n,
1251
+ c,
1252
+ p,
1253
+ a,
1254
+ e
1255
+ ), _ = () => v("main-button", {
1256
+ backgroundColor: h.backgroundColor,
1257
+ isEnabled: h.isEnabled,
1258
+ isVisible: h.isVisible,
1259
+ isProgressVisible: h.isProgressVisible,
1260
+ text: h.text,
1261
+ textColor: h.textColor
1262
+ });
1263
+ return h.on("backgroundColorChanged", _), h.on("isEnabledChanged", _), h.on("isVisibleChanged", _), h.on("isProgressVisibleChanged", _), h.on("textColorChanged", _), h.on("textChanged", _), h;
1264
+ }
1265
+ class H extends Error {
1266
+ constructor(t, e) {
1267
+ super(`Method "${t}" is not supported in the Web Apps version ${e}.`), Object.setPrototypeOf(this, H.prototype);
1268
+ }
1269
+ }
1270
+ class I extends Error {
1271
+ constructor(t, e, o) {
1272
+ super(`Parameter "${e}" in method "${t}" is not supported in the Web Apps version ${o}.`), Object.setPrototypeOf(this, I.prototype);
1273
+ }
1274
+ }
1275
+ function Nt(s, t) {
1276
+ return s ? (e, o) => {
1277
+ if (!C(e, t))
1278
+ throw new H(e, t);
1279
+ return bt(o) && pt(e, o).forEach((r) => {
1280
+ if (!C(e, r, t))
1281
+ throw new I(e, r, t);
1282
+ }), l(e, o);
1283
+ } : l;
1284
+ }
1285
+ function zt() {
1286
+ let s = 0;
1287
+ return () => (s += 1, s.toString());
1288
+ }
1289
+ function Mt(s) {
1290
+ const t = new q(s);
1291
+ return q.sync(t), t;
1292
+ }
1293
+ async function Ut(s, t) {
1294
+ const {
1295
+ height: e = window.innerHeight,
1296
+ stableHeight: o = window.innerHeight,
1297
+ width: r = window.innerWidth,
1298
+ isExpanded: n = !1
1299
+ } = k("viewport") || {}, a = s === "macos" || s === "web" ? (() => {
1300
+ const h = new V(e, r, o, n, t);
1301
+ return V.sync(h), h;
1302
+ })() : await V.synced({ postEvent: t }), p = () => v("viewport", {
1303
+ height: a.height,
1304
+ isExpanded: a.isExpanded,
1305
+ stableHeight: a.stableHeight,
1306
+ width: a.width
1307
+ });
1308
+ return a.on("heightChanged", p), a.on("isExpandedChanged", p), a.on("stableHeightChanged", p), a.on("widthChanged", p), a;
1309
+ }
1310
+ function Kt(s, t, e, o, r) {
1311
+ const {
1312
+ backgroundColor: n = s,
1313
+ headerColor: c = "bg_color"
1314
+ } = k("web-app") || {}, a = new Ot(
1315
+ c,
1316
+ n,
1317
+ t,
1318
+ e,
1319
+ o,
1320
+ r
1321
+ ), p = () => v("web-app", {
1322
+ backgroundColor: a.backgroundColor,
1323
+ headerColor: a.headerColor
1324
+ });
1325
+ return a.on("backgroundColorChanged", p), a.on("headerColorChanged", p), a;
1326
+ }
1327
+ async function jt(s = {}) {
1328
+ const {
1329
+ checkCompat: t = !0,
1330
+ cssVars: e = !1,
1331
+ acceptScrollbarStyle: o = !0,
1332
+ acceptCustomStyles: r = o,
1333
+ targetOrigin: n,
1334
+ debug: c = !1,
1335
+ launchParams: a
1336
+ } = s;
1337
+ c && ut(c), typeof n == "string" && lt(n);
1338
+ let p;
1339
+ a ? p = a instanceof URLSearchParams || typeof a == "string" ? R(a) : a : p = Q();
1340
+ const {
1341
+ initData: h,
1342
+ initDataRaw: _,
1343
+ version: m,
1344
+ platform: D,
1345
+ themeParams: W
1346
+ } = p, {
1347
+ backgroundColor: X = "#ffffff",
1348
+ buttonColor: Y = "#000000",
1349
+ buttonTextColor: tt = "#ffffff"
1350
+ } = W, L = zt(), u = Nt(t, m), O = Mt(W), N = Kt(X, m, D, L, u), {
1351
+ themeParams: et,
1352
+ viewport: st,
1353
+ webApp: ot
1354
+ } = It(e);
1355
+ ot && Rt(N, O), et && $t(O);
1356
+ const z = await Ut(D, u);
1357
+ if (st && Ht(z), r && dt()) {
1358
+ const E = document.createElement("style");
1359
+ E.id = "telegram-custom-styles", document.head.appendChild(E), S("set_custom_style", (A) => {
1360
+ E.innerHTML = A;
1361
+ }), u("iframe_ready");
1362
+ }
1363
+ const B = {
1364
+ backButton: Dt(m, u),
1365
+ closingBehavior: Wt(u),
1366
+ cloudStorage: new vt(m, L, u),
1367
+ haptic: new kt(m, u),
1368
+ mainButton: Lt(Y, tt, u),
1369
+ popup: new Vt(m, u),
1370
+ postEvent: u,
1371
+ qrScanner: new St(m, u),
1372
+ themeParams: O,
1373
+ viewport: z,
1374
+ webApp: N
1375
+ };
1376
+ if (h !== void 0) {
1377
+ const { authDate: E, hash: A, ...rt } = h;
1378
+ B.initData = new Et(E, A, rt), B.initDataRaw = _;
1379
+ }
1380
+ return B;
1381
+ }
1382
+ function te(s = {}) {
1383
+ return _t(jt(s), s.timeout || 1e3);
1384
+ }
1385
+ function ee() {
1386
+ try {
1387
+ return Q(), !0;
1388
+ } catch {
1389
+ return !1;
1390
+ }
1391
+ }
1392
+ export {
1393
+ wt as BackButton,
1394
+ Ct as ClosingBehaviour,
1395
+ vt as CloudStorage,
1396
+ kt as HapticFeedback,
1397
+ Et as InitData,
1398
+ xt as MainButton,
1399
+ H as MethodNotSupportedError,
1400
+ Vt as Popup,
1401
+ St as QRScanner,
1402
+ q as ThemeParams,
1403
+ V as Viewport,
1404
+ Ot as WebApp,
1405
+ T as formatURL,
1406
+ te as init,
1407
+ ee as isTWA,
1408
+ R as parseLaunchParams,
1409
+ $ as parseThemeParams,
1410
+ Q as retrieveLaunchParams
1411
+ };
1412
+ //# sourceMappingURL=index.js.map