@mr-m/telegram-webapp-kit 1.0.0

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.
package/dist/index.js ADDED
@@ -0,0 +1,1232 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
+ var __export = (target, all) => {
24
+ for (var name in all)
25
+ __defProp(target, name, { get: all[name], enumerable: true });
26
+ };
27
+ var __copyProps = (to, from, except, desc) => {
28
+ if (from && typeof from === "object" || typeof from === "function") {
29
+ for (let key of __getOwnPropNames(from))
30
+ if (!__hasOwnProp.call(to, key) && key !== except)
31
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
32
+ }
33
+ return to;
34
+ };
35
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
+
37
+ // src/index.ts
38
+ var index_exports = {};
39
+ __export(index_exports, {
40
+ FullscreenProvider: () => FullscreenProvider,
41
+ RTL_LANGS: () => RTL_LANGS,
42
+ SUPPORTED_LANGS: () => SUPPORTED_LANGS,
43
+ TelegramProvider: () => TelegramProvider,
44
+ biometric: () => biometric,
45
+ cloudStorage: () => cloudStorage,
46
+ dialog: () => dialog,
47
+ getRawUserData: () => getRawUserData,
48
+ getUserAvatarUrl: () => getUserAvatarUrl,
49
+ getUserDisplayName: () => getUserDisplayName,
50
+ getUserIdentifier: () => getUserIdentifier,
51
+ getUserInfoWithAvatar: () => getUserInfoWithAvatar,
52
+ getWebApp: () => getWebApp,
53
+ haptic: () => haptic,
54
+ isInTelegram: () => isInTelegram,
55
+ isRtlLang: () => isRtlLang,
56
+ isVersionAtLeast: () => isVersionAtLeast,
57
+ location: () => location,
58
+ openExternalLink: () => openExternalLink,
59
+ openInvoice: () => openInvoice,
60
+ openTelegramLink: () => openTelegramLink,
61
+ readClipboard: () => readClipboard,
62
+ scanQr: () => scanQr,
63
+ tgLangToTmdb: () => tgLangToTmdb,
64
+ tgLangToUi: () => tgLangToUi,
65
+ useAccelerometer: () => useAccelerometer,
66
+ useBiometric: () => useBiometric,
67
+ useCloudStorage: () => useCloudStorage,
68
+ useDeviceOrientation: () => useDeviceOrientation,
69
+ useFullscreen: () => useFullscreen,
70
+ useGyroscope: () => useGyroscope,
71
+ useHapticFeedback: () => useHapticFeedback,
72
+ useHomeScreen: () => useHomeScreen,
73
+ useIsActive: () => useIsActive,
74
+ useLocation: () => useLocation,
75
+ useSafeArea: () => useSafeArea,
76
+ useTelegram: () => useTelegram,
77
+ useTelegramBackButton: () => useTelegramBackButton,
78
+ useTelegramEvent: () => useTelegramEvent,
79
+ useTelegramFullscreen: () => useTelegramFullscreen,
80
+ useTelegramMainButton: () => useTelegramMainButton,
81
+ useTelegramSecondaryButton: () => useTelegramSecondaryButton,
82
+ useTelegramSettingsButton: () => useTelegramSettingsButton,
83
+ useTelegramStartParam: () => useTelegramStartParam,
84
+ useTelegramTheme: () => useTelegramTheme,
85
+ useTelegramUser: () => useTelegramUser,
86
+ useTelegramViewport: () => useTelegramViewport,
87
+ useTelegramWebApp: () => useTelegramWebApp
88
+ });
89
+ module.exports = __toCommonJS(index_exports);
90
+
91
+ // src/core/index.ts
92
+ function getWebApp() {
93
+ var _a, _b;
94
+ if (typeof window === "undefined") return null;
95
+ return (_b = (_a = window.Telegram) == null ? void 0 : _a.WebApp) != null ? _b : null;
96
+ }
97
+ function isInTelegram() {
98
+ const wa = getWebApp();
99
+ return Boolean(wa && wa.initData && wa.initData.length > 0);
100
+ }
101
+ function isVersionAtLeast(version) {
102
+ var _a, _b;
103
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.isVersionAtLeast(version)) != null ? _b : false;
104
+ }
105
+ function tgLangToTmdb(code) {
106
+ var _a;
107
+ if (!code) return "en-US";
108
+ const base = code.toLowerCase().split("-")[0];
109
+ const map = {
110
+ en: "en-US",
111
+ ar: "ar-SA",
112
+ es: "es-ES",
113
+ fr: "fr-FR",
114
+ de: "de-DE",
115
+ it: "it-IT",
116
+ pt: "pt-BR",
117
+ ru: "ru-RU",
118
+ zh: "zh-CN",
119
+ ja: "ja-JP",
120
+ ko: "ko-KR",
121
+ tr: "tr-TR",
122
+ hi: "hi-IN",
123
+ id: "id-ID",
124
+ nl: "nl-NL",
125
+ pl: "pl-PL",
126
+ sv: "sv-SE",
127
+ uk: "uk-UA",
128
+ vi: "vi-VN",
129
+ fa: "fa-IR",
130
+ he: "he-IL",
131
+ cs: "cs-CZ",
132
+ da: "da-DK",
133
+ fi: "fi-FI",
134
+ hu: "hu-HU",
135
+ nb: "nb-NO",
136
+ ro: "ro-RO",
137
+ sk: "sk-SK",
138
+ th: "th-TH",
139
+ ms: "ms-MY"
140
+ };
141
+ return (_a = map[base]) != null ? _a : "en-US";
142
+ }
143
+ function tgLangToUi(code) {
144
+ if (!code) return "en";
145
+ const base = code.toLowerCase().split("-")[0];
146
+ const supported = [
147
+ "en",
148
+ "ar",
149
+ "es",
150
+ "fr",
151
+ "de",
152
+ "it",
153
+ "pt",
154
+ "ru",
155
+ "zh",
156
+ "ja",
157
+ "ko",
158
+ "tr",
159
+ "hi",
160
+ "id",
161
+ "nl",
162
+ "pl",
163
+ "sv",
164
+ "uk",
165
+ "vi",
166
+ "fa",
167
+ "he",
168
+ "cs",
169
+ "da",
170
+ "fi",
171
+ "hu",
172
+ "nb",
173
+ "ro",
174
+ "sk",
175
+ "th",
176
+ "ms"
177
+ ];
178
+ return supported.includes(base) ? base : "en";
179
+ }
180
+ var RTL_LANGS = /* @__PURE__ */ new Set(["ar", "fa", "he", "ur", "yi", "ug", "ku"]);
181
+ function isRtlLang(lang) {
182
+ if (!lang) return false;
183
+ return RTL_LANGS.has(lang.toLowerCase().split("-")[0]);
184
+ }
185
+ var SUPPORTED_LANGS = [
186
+ { code: "en", countryCode: "us", name: "English", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/us.svg" },
187
+ { code: "ar", countryCode: "sa", name: "\u0627\u0644\u0639\u0631\u0628\u064A\u0629", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/sa.svg" },
188
+ { code: "es", countryCode: "es", name: "Espa\xF1ol", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/es.svg" },
189
+ { code: "fr", countryCode: "fr", name: "Fran\xE7ais", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/fr.svg" },
190
+ { code: "de", countryCode: "de", name: "Deutsch", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/de.svg" },
191
+ { code: "it", countryCode: "it", name: "Italiano", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/it.svg" },
192
+ { code: "pt", countryCode: "pt", name: "Portugu\xEAs", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/pt.svg" },
193
+ { code: "ru", countryCode: "ru", name: "\u0420\u0443\u0441\u0441\u043A\u0438\u0439", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/ru.svg" },
194
+ { code: "zh", countryCode: "cn", name: "\u4E2D\u6587", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/cn.svg" },
195
+ { code: "ja", countryCode: "jp", name: "\u65E5\u672C\u8A9E", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/jp.svg" },
196
+ { code: "ko", countryCode: "kr", name: "\uD55C\uAD6D\uC5B4", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/kr.svg" },
197
+ { code: "tr", countryCode: "tr", name: "T\xFCrk\xE7e", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/tr.svg" },
198
+ { code: "hi", countryCode: "in", name: "\u0939\u093F\u0928\u094D\u0926\u0940", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/in.svg" },
199
+ { code: "id", countryCode: "id", name: "Indonesia", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/id.svg" },
200
+ { code: "nl", countryCode: "nl", name: "Nederlands", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/nl.svg" },
201
+ { code: "pl", countryCode: "pl", name: "Polski", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/pl.svg" },
202
+ { code: "sv", countryCode: "se", name: "Svenska", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/se.svg" },
203
+ { code: "uk", countryCode: "ua", name: "\u0423\u043A\u0440\u0430\u0457\u043D\u0441\u044C\u043A\u0430", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/ua.svg" },
204
+ { code: "vi", countryCode: "vn", name: "Ti\u1EBFng Vi\u1EC7t", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/vn.svg" },
205
+ { code: "fa", countryCode: "ir", name: "\u0641\u0627\u0631\u0633\u06CC", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/ir.svg" },
206
+ { code: "he", countryCode: "il", name: "\u05E2\u05D1\u05E8\u05D9\u05EA", flag: "https://raw.githubusercontent.com/mr-m-apps/icons/refs/heads/main/flags/il.svg" }
207
+ ];
208
+ function openExternalLink(url, tryInstantView = false) {
209
+ const wa = getWebApp();
210
+ if (wa == null ? void 0 : wa.openLink) {
211
+ wa.openLink(url, { try_instant_view: tryInstantView });
212
+ } else if (typeof window !== "undefined") {
213
+ window.open(url, "_blank");
214
+ }
215
+ }
216
+ function openTelegramLink(url) {
217
+ const wa = getWebApp();
218
+ if (wa == null ? void 0 : wa.openTelegramLink) {
219
+ wa.openTelegramLink(url);
220
+ } else if (typeof window !== "undefined") {
221
+ window.open(url, "_blank");
222
+ }
223
+ }
224
+ function getUserAvatarUrl(user, defaultAvatarUrl = "https://via.placeholder.com/100x100?text=No+Avatar") {
225
+ var _a, _b;
226
+ if (user == null ? void 0 : user.photo_url) return user.photo_url;
227
+ if (user == null ? void 0 : user.id) {
228
+ const firstName = (_a = user == null ? void 0 : user.first_name) != null ? _a : "";
229
+ const lastName = (_b = user == null ? void 0 : user.last_name) != null ? _b : "";
230
+ const name = `${firstName} ${lastName}`.trim() || (user == null ? void 0 : user.username) || `user_${user.id}`;
231
+ return `https://ui-avatars.com/api/?name=${encodeURIComponent(name)}&background=random&color=fff&size=100&length=2&rounded=true&bold=true`;
232
+ }
233
+ return defaultAvatarUrl;
234
+ }
235
+ function getUserIdentifier(user) {
236
+ if (user == null ? void 0 : user.username) return user.username;
237
+ if (user == null ? void 0 : user.id) return user.id.toString();
238
+ return "unknown_user";
239
+ }
240
+ function getUserDisplayName(user) {
241
+ var _a, _b;
242
+ if (!user) return "User";
243
+ const fullName = `${(_a = user.first_name) != null ? _a : ""} ${(_b = user.last_name) != null ? _b : ""}`.trim();
244
+ if (fullName) return fullName;
245
+ if (user.username) return `@${user.username}`;
246
+ return `User_${user.id}`;
247
+ }
248
+ function getUserInfoWithAvatar() {
249
+ var _a;
250
+ const wa = getWebApp();
251
+ const user = (_a = wa == null ? void 0 : wa.initDataUnsafe) == null ? void 0 : _a.user;
252
+ return {
253
+ user,
254
+ avatarUrl: getUserAvatarUrl(user),
255
+ displayName: getUserDisplayName(user),
256
+ identifier: getUserIdentifier(user)
257
+ };
258
+ }
259
+ function getRawUserData() {
260
+ var _a, _b;
261
+ const wa = getWebApp();
262
+ return (_b = (_a = wa == null ? void 0 : wa.initDataUnsafe) == null ? void 0 : _a.user) != null ? _b : null;
263
+ }
264
+ var haptic = {
265
+ light: () => {
266
+ var _a, _b;
267
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred("light");
268
+ },
269
+ medium: () => {
270
+ var _a, _b;
271
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred("medium");
272
+ },
273
+ heavy: () => {
274
+ var _a, _b;
275
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred("heavy");
276
+ },
277
+ rigid: () => {
278
+ var _a, _b;
279
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred("rigid");
280
+ },
281
+ soft: () => {
282
+ var _a, _b;
283
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred("soft");
284
+ },
285
+ success: () => {
286
+ var _a, _b;
287
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.notificationOccurred("success");
288
+ },
289
+ error: () => {
290
+ var _a, _b;
291
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.notificationOccurred("error");
292
+ },
293
+ warning: () => {
294
+ var _a, _b;
295
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.notificationOccurred("warning");
296
+ },
297
+ selection: () => {
298
+ var _a, _b;
299
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.selectionChanged();
300
+ }
301
+ };
302
+ var cloudStorage = {
303
+ setItem: (key, value) => new Promise((resolve, reject) => {
304
+ const wa = getWebApp();
305
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
306
+ wa.CloudStorage.setItem(key, value, (err, success) => {
307
+ if (err) reject(err);
308
+ else resolve(success);
309
+ });
310
+ }),
311
+ getItem: (key) => new Promise((resolve, reject) => {
312
+ const wa = getWebApp();
313
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
314
+ wa.CloudStorage.getItem(key, (err, value) => {
315
+ if (err) reject(err);
316
+ else resolve(value);
317
+ });
318
+ }),
319
+ getItems: (keys) => new Promise((resolve, reject) => {
320
+ const wa = getWebApp();
321
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
322
+ wa.CloudStorage.getItems(keys, (err, values) => {
323
+ if (err) reject(err);
324
+ else resolve(values);
325
+ });
326
+ }),
327
+ removeItem: (key) => new Promise((resolve, reject) => {
328
+ const wa = getWebApp();
329
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
330
+ wa.CloudStorage.removeItem(key, (err, success) => {
331
+ if (err) reject(err);
332
+ else resolve(success);
333
+ });
334
+ }),
335
+ getKeys: () => new Promise((resolve, reject) => {
336
+ const wa = getWebApp();
337
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
338
+ wa.CloudStorage.getKeys((err, keys) => {
339
+ if (err) reject(err);
340
+ else resolve(keys);
341
+ });
342
+ })
343
+ };
344
+ var dialog = {
345
+ alert: (message) => new Promise((resolve) => {
346
+ const wa = getWebApp();
347
+ if (!wa) {
348
+ alert(message);
349
+ resolve();
350
+ return;
351
+ }
352
+ wa.showAlert(message, resolve);
353
+ }),
354
+ confirm: (message) => new Promise((resolve) => {
355
+ const wa = getWebApp();
356
+ if (!wa) {
357
+ resolve(window.confirm(message));
358
+ return;
359
+ }
360
+ wa.showConfirm(message, resolve);
361
+ }),
362
+ popup: (params) => new Promise((resolve) => {
363
+ const wa = getWebApp();
364
+ if (!wa) {
365
+ resolve("");
366
+ return;
367
+ }
368
+ wa.showPopup(params, resolve);
369
+ })
370
+ };
371
+ function readClipboard() {
372
+ return new Promise((resolve, reject) => {
373
+ const wa = getWebApp();
374
+ if (!(wa == null ? void 0 : wa.readTextFromClipboard)) return reject(new Error("readTextFromClipboard not supported"));
375
+ wa.readTextFromClipboard((text) => resolve(text != null ? text : ""));
376
+ });
377
+ }
378
+ function openInvoice(url) {
379
+ return new Promise((resolve, reject) => {
380
+ const wa = getWebApp();
381
+ if (!wa) return reject(new Error("WebApp not available"));
382
+ wa.openInvoice(url, (status) => resolve(status));
383
+ });
384
+ }
385
+ function scanQr(text) {
386
+ return new Promise((resolve, reject) => {
387
+ const wa = getWebApp();
388
+ if (!wa) return reject(new Error("WebApp not available"));
389
+ wa.showScanQrPopup({ text }, (result) => {
390
+ wa.closeScanQrPopup();
391
+ resolve(result);
392
+ });
393
+ });
394
+ }
395
+ var biometric = {
396
+ init: () => new Promise((resolve, reject) => {
397
+ var _a;
398
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
399
+ if (!bio) return reject(new Error("BiometricManager not available"));
400
+ bio.init(resolve);
401
+ }),
402
+ requestAccess: (reason) => new Promise((resolve, reject) => {
403
+ var _a;
404
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
405
+ if (!bio) return reject(new Error("BiometricManager not available"));
406
+ bio.requestAccess({ reason }, resolve);
407
+ }),
408
+ authenticate: (reason) => new Promise((resolve, reject) => {
409
+ var _a;
410
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
411
+ if (!bio) return reject(new Error("BiometricManager not available"));
412
+ bio.authenticate({ reason }, (authenticated, token) => resolve({ authenticated, token }));
413
+ })
414
+ };
415
+ var location = {
416
+ init: () => new Promise((resolve, reject) => {
417
+ var _a;
418
+ const loc = (_a = getWebApp()) == null ? void 0 : _a.LocationManager;
419
+ if (!loc) return reject(new Error("LocationManager not available"));
420
+ loc.init((error) => {
421
+ if (error) {
422
+ reject(error);
423
+ } else {
424
+ resolve();
425
+ }
426
+ });
427
+ }),
428
+ getLocation: () => new Promise((resolve, reject) => {
429
+ var _a;
430
+ const loc = (_a = getWebApp()) == null ? void 0 : _a.LocationManager;
431
+ if (!loc) return reject(new Error("LocationManager not available"));
432
+ loc.getLocation((location2, error) => {
433
+ if (error) {
434
+ reject(error);
435
+ } else {
436
+ resolve(location2);
437
+ }
438
+ });
439
+ })
440
+ };
441
+
442
+ // src/hooks/index.ts
443
+ var import_react = require("react");
444
+ function useTelegramWebApp() {
445
+ const [wa, setWa] = (0, import_react.useState)(null);
446
+ (0, import_react.useEffect)(() => {
447
+ setWa(getWebApp());
448
+ }, []);
449
+ return wa;
450
+ }
451
+ function useTelegramUser() {
452
+ var _a, _b, _c;
453
+ return (_c = (_b = (_a = getWebApp()) == null ? void 0 : _a.initDataUnsafe) == null ? void 0 : _b.user) != null ? _c : null;
454
+ }
455
+ function useTelegramEvent(eventType, handler) {
456
+ const handlerRef = (0, import_react.useRef)(handler);
457
+ handlerRef.current = handler;
458
+ (0, import_react.useEffect)(() => {
459
+ const wa = getWebApp();
460
+ if (!wa) return;
461
+ const cb = (...args) => handlerRef.current(...args);
462
+ wa.onEvent(eventType, cb);
463
+ return () => wa.offEvent(eventType, cb);
464
+ }, [eventType]);
465
+ }
466
+ function useTelegramBackButton(options) {
467
+ const { pathname = "/", onBack, hideOnRoot = true } = options != null ? options : {};
468
+ (0, import_react.useEffect)(() => {
469
+ const wa = getWebApp();
470
+ if (!(wa == null ? void 0 : wa.BackButton)) return;
471
+ const handleBack = onBack != null ? onBack : (() => {
472
+ if (typeof window !== "undefined" && window.history.length > 1) {
473
+ window.history.back();
474
+ } else {
475
+ wa.close();
476
+ }
477
+ });
478
+ wa.onEvent("backButtonClicked", handleBack);
479
+ if (hideOnRoot && pathname === "/") {
480
+ wa.BackButton.hide();
481
+ } else {
482
+ wa.BackButton.show();
483
+ }
484
+ return () => {
485
+ var _a;
486
+ wa.offEvent("backButtonClicked", handleBack);
487
+ (_a = wa.BackButton) == null ? void 0 : _a.hide();
488
+ };
489
+ }, [pathname, onBack, hideOnRoot]);
490
+ }
491
+ function useTelegramMainButton(options) {
492
+ const { text, onClick, isVisible = true, isActive = true, color, textColor, hasShineEffect, showProgress } = options;
493
+ const onClickRef = (0, import_react.useRef)(onClick);
494
+ onClickRef.current = onClick;
495
+ (0, import_react.useEffect)(() => {
496
+ const wa = getWebApp();
497
+ if (!(wa == null ? void 0 : wa.MainButton)) return;
498
+ const cb = () => onClickRef.current();
499
+ wa.MainButton.onClick(cb);
500
+ wa.MainButton.setParams(__spreadValues(__spreadValues(__spreadValues({
501
+ text,
502
+ is_visible: isVisible,
503
+ is_active: isActive
504
+ }, color && { color }), textColor && { text_color: textColor }), hasShineEffect !== void 0 && { hasShineEffect }));
505
+ if (showProgress) wa.MainButton.showProgress();
506
+ else wa.MainButton.hideProgress();
507
+ return () => wa.MainButton.offClick(cb);
508
+ }, [text, isVisible, isActive, color, textColor, hasShineEffect, showProgress]);
509
+ }
510
+ function useTelegramSecondaryButton(options) {
511
+ const { text, onClick, isVisible = true, isActive = true, position, color, textColor } = options;
512
+ const onClickRef = (0, import_react.useRef)(onClick);
513
+ onClickRef.current = onClick;
514
+ (0, import_react.useEffect)(() => {
515
+ const wa = getWebApp();
516
+ if (!(wa == null ? void 0 : wa.SecondaryButton)) return;
517
+ const cb = () => onClickRef.current();
518
+ wa.SecondaryButton.onClick(cb);
519
+ wa.SecondaryButton.setParams(__spreadValues(__spreadValues(__spreadValues({
520
+ text,
521
+ is_visible: isVisible,
522
+ is_active: isActive
523
+ }, position && { position }), color && { color }), textColor && { text_color: textColor }));
524
+ return () => {
525
+ var _a;
526
+ return (_a = wa.SecondaryButton) == null ? void 0 : _a.offClick(cb);
527
+ };
528
+ }, [text, isVisible, isActive, position, color, textColor]);
529
+ }
530
+ function useTelegramSettingsButton(onSettings) {
531
+ const onSettingsRef = (0, import_react.useRef)(onSettings);
532
+ onSettingsRef.current = onSettings;
533
+ (0, import_react.useEffect)(() => {
534
+ const wa = getWebApp();
535
+ if (!(wa == null ? void 0 : wa.SettingsButton)) return;
536
+ const cb = () => onSettingsRef.current();
537
+ wa.SettingsButton.show();
538
+ wa.onEvent("settingsButtonClicked", cb);
539
+ return () => {
540
+ var _a;
541
+ wa.offEvent("settingsButtonClicked", cb);
542
+ (_a = wa.SettingsButton) == null ? void 0 : _a.hide();
543
+ };
544
+ }, []);
545
+ }
546
+ function useHapticFeedback() {
547
+ return {
548
+ impact: (0, import_react.useCallback)((style = "medium") => {
549
+ var _a, _b;
550
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.impactOccurred(style);
551
+ }, []),
552
+ notification: (0, import_react.useCallback)((type) => {
553
+ var _a, _b;
554
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.notificationOccurred(type);
555
+ }, []),
556
+ selectionChanged: (0, import_react.useCallback)(() => {
557
+ var _a, _b;
558
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.HapticFeedback) == null ? void 0 : _b.selectionChanged();
559
+ }, [])
560
+ };
561
+ }
562
+ function useTelegramTheme() {
563
+ const [colorScheme, setColorScheme] = (0, import_react.useState)(
564
+ () => {
565
+ var _a, _b;
566
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.colorScheme) != null ? _b : "dark";
567
+ }
568
+ );
569
+ const [themeParams, setThemeParams] = (0, import_react.useState)(
570
+ () => {
571
+ var _a, _b;
572
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.themeParams) != null ? _b : {};
573
+ }
574
+ );
575
+ useTelegramEvent("themeChanged", () => {
576
+ const wa = getWebApp();
577
+ if (!wa) return;
578
+ setColorScheme(wa.colorScheme);
579
+ setThemeParams(wa.themeParams);
580
+ });
581
+ return { colorScheme, themeParams, isDark: colorScheme === "dark" };
582
+ }
583
+ function useTelegramViewport() {
584
+ const [viewport, setViewport] = (0, import_react.useState)(() => {
585
+ var _a, _b, _c, _d, _e, _f;
586
+ return {
587
+ height: (_b = (_a = getWebApp()) == null ? void 0 : _a.viewportHeight) != null ? _b : 0,
588
+ stableHeight: (_d = (_c = getWebApp()) == null ? void 0 : _c.viewportStableHeight) != null ? _d : 0,
589
+ isExpanded: (_f = (_e = getWebApp()) == null ? void 0 : _e.isExpanded) != null ? _f : false
590
+ };
591
+ });
592
+ useTelegramEvent("viewportChanged", () => {
593
+ const wa = getWebApp();
594
+ if (!wa) return;
595
+ setViewport({
596
+ height: wa.viewportHeight,
597
+ stableHeight: wa.viewportStableHeight,
598
+ isExpanded: wa.isExpanded
599
+ });
600
+ });
601
+ const expand = (0, import_react.useCallback)(() => {
602
+ var _a;
603
+ return (_a = getWebApp()) == null ? void 0 : _a.expand();
604
+ }, []);
605
+ return __spreadProps(__spreadValues({}, viewport), { expand });
606
+ }
607
+ function useTelegramFullscreen() {
608
+ const [isFullscreen, setIsFullscreen] = (0, import_react.useState)(
609
+ () => {
610
+ var _a;
611
+ return Boolean((_a = getWebApp()) == null ? void 0 : _a.isFullscreen);
612
+ }
613
+ );
614
+ const [error, setError] = (0, import_react.useState)(null);
615
+ useTelegramEvent("fullscreenChanged", () => {
616
+ var _a;
617
+ setIsFullscreen(Boolean((_a = getWebApp()) == null ? void 0 : _a.isFullscreen));
618
+ });
619
+ useTelegramEvent("fullscreenFailed", (...args) => {
620
+ const err = args[0];
621
+ if (err && typeof err === "object" && "error" in err) {
622
+ setError(err);
623
+ }
624
+ });
625
+ const enter = (0, import_react.useCallback)(() => {
626
+ var _a, _b;
627
+ setError(null);
628
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.requestFullscreen) == null ? void 0 : _b.call(_a);
629
+ }, []);
630
+ const exit = (0, import_react.useCallback)(() => {
631
+ var _a, _b;
632
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.exitFullscreen) == null ? void 0 : _b.call(_a);
633
+ }, []);
634
+ const toggle = (0, import_react.useCallback)(() => {
635
+ if (isFullscreen) exit();
636
+ else enter();
637
+ }, [isFullscreen, enter, exit]);
638
+ return { isFullscreen, error, enter, exit, toggle };
639
+ }
640
+ function useSafeArea() {
641
+ const [safeArea, setSafeArea] = (0, import_react.useState)(
642
+ () => {
643
+ var _a, _b;
644
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.safeAreaInset) != null ? _b : { top: 0, bottom: 0, left: 0, right: 0 };
645
+ }
646
+ );
647
+ const [contentSafeArea, setContentSafeArea] = (0, import_react.useState)(
648
+ () => {
649
+ var _a, _b;
650
+ return (_b = (_a = getWebApp()) == null ? void 0 : _a.contentSafeAreaInset) != null ? _b : { top: 0, bottom: 0, left: 0, right: 0 };
651
+ }
652
+ );
653
+ useTelegramEvent("safeAreaChanged", () => {
654
+ var _a;
655
+ const inset = (_a = getWebApp()) == null ? void 0 : _a.safeAreaInset;
656
+ if (inset) setSafeArea(inset);
657
+ });
658
+ useTelegramEvent("contentSafeAreaChanged", () => {
659
+ var _a;
660
+ const inset = (_a = getWebApp()) == null ? void 0 : _a.contentSafeAreaInset;
661
+ if (inset) setContentSafeArea(inset);
662
+ });
663
+ return { safeArea, contentSafeArea };
664
+ }
665
+ function useCloudStorage() {
666
+ const setItem = (0, import_react.useCallback)((key, value) => new Promise((resolve, reject) => {
667
+ const wa = getWebApp();
668
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
669
+ wa.CloudStorage.setItem(key, value, (err, success) => {
670
+ if (err) reject(err);
671
+ else resolve(success);
672
+ });
673
+ }), []);
674
+ const getItem = (0, import_react.useCallback)((key) => new Promise((resolve, reject) => {
675
+ const wa = getWebApp();
676
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
677
+ wa.CloudStorage.getItem(key, (err, value) => {
678
+ if (err) reject(err);
679
+ else resolve(value);
680
+ });
681
+ }), []);
682
+ const getItems = (0, import_react.useCallback)((keys) => new Promise((resolve, reject) => {
683
+ const wa = getWebApp();
684
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
685
+ wa.CloudStorage.getItems(keys, (err, values) => {
686
+ if (err) reject(err);
687
+ else resolve(values);
688
+ });
689
+ }), []);
690
+ const removeItem = (0, import_react.useCallback)((key) => new Promise((resolve, reject) => {
691
+ const wa = getWebApp();
692
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
693
+ wa.CloudStorage.removeItem(key, (err, success) => {
694
+ if (err) reject(err);
695
+ else resolve(success);
696
+ });
697
+ }), []);
698
+ const getKeys = (0, import_react.useCallback)(() => new Promise((resolve, reject) => {
699
+ const wa = getWebApp();
700
+ if (!(wa == null ? void 0 : wa.CloudStorage)) return reject(new Error("CloudStorage not available"));
701
+ wa.CloudStorage.getKeys((err, keys) => {
702
+ if (err) reject(err);
703
+ else resolve(keys);
704
+ });
705
+ }), []);
706
+ return { setItem, getItem, getItems, removeItem, getKeys };
707
+ }
708
+ function useAccelerometer(options) {
709
+ const { refreshRate = 100, autoStart = false } = options != null ? options : {};
710
+ const [data, setData] = (0, import_react.useState)({ x: 0, y: 0, z: 0 });
711
+ const [isStarted, setIsStarted] = (0, import_react.useState)(false);
712
+ useTelegramEvent("accelerometerChanged", () => {
713
+ var _a;
714
+ const acc = (_a = getWebApp()) == null ? void 0 : _a.Accelerometer;
715
+ if (acc) setData({ x: acc.x, y: acc.y, z: acc.z });
716
+ });
717
+ useTelegramEvent("accelerometerStarted", () => setIsStarted(true));
718
+ useTelegramEvent("accelerometerStopped", () => setIsStarted(false));
719
+ const start = (0, import_react.useCallback)(() => {
720
+ var _a, _b;
721
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.Accelerometer) == null ? void 0 : _b.start({ refresh_rate: refreshRate });
722
+ }, [refreshRate]);
723
+ const stop = (0, import_react.useCallback)(() => {
724
+ var _a, _b;
725
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.Accelerometer) == null ? void 0 : _b.stop();
726
+ }, []);
727
+ (0, import_react.useEffect)(() => {
728
+ if (autoStart) start();
729
+ return () => {
730
+ if (autoStart) stop();
731
+ };
732
+ }, [autoStart, start, stop]);
733
+ return __spreadProps(__spreadValues({}, data), { isStarted, start, stop });
734
+ }
735
+ function useGyroscope(options) {
736
+ const { refreshRate = 100, autoStart = false } = options != null ? options : {};
737
+ const [data, setData] = (0, import_react.useState)({ x: 0, y: 0, z: 0 });
738
+ const [isStarted, setIsStarted] = (0, import_react.useState)(false);
739
+ useTelegramEvent("gyroscopeChanged", () => {
740
+ var _a;
741
+ const gyro = (_a = getWebApp()) == null ? void 0 : _a.Gyroscope;
742
+ if (gyro) setData({ x: gyro.x, y: gyro.y, z: gyro.z });
743
+ });
744
+ useTelegramEvent("gyroscopeStarted", () => setIsStarted(true));
745
+ useTelegramEvent("gyroscopeStopped", () => setIsStarted(false));
746
+ const start = (0, import_react.useCallback)(() => {
747
+ var _a, _b;
748
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.Gyroscope) == null ? void 0 : _b.start({ refresh_rate: refreshRate });
749
+ }, [refreshRate]);
750
+ const stop = (0, import_react.useCallback)(() => {
751
+ var _a, _b;
752
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.Gyroscope) == null ? void 0 : _b.stop();
753
+ }, []);
754
+ (0, import_react.useEffect)(() => {
755
+ if (autoStart) start();
756
+ return () => {
757
+ if (autoStart) stop();
758
+ };
759
+ }, [autoStart, start, stop]);
760
+ return __spreadProps(__spreadValues({}, data), { isStarted, start, stop });
761
+ }
762
+ function useDeviceOrientation(options) {
763
+ const { refreshRate = 100, needAbsolute = false, autoStart = false } = options != null ? options : {};
764
+ const [data, setData] = (0, import_react.useState)({ alpha: 0, beta: 0, gamma: 0, absolute: false });
765
+ const [isStarted, setIsStarted] = (0, import_react.useState)(false);
766
+ useTelegramEvent("deviceOrientationChanged", () => {
767
+ var _a, _b, _c, _d;
768
+ const ori = (_a = getWebApp()) == null ? void 0 : _a.DeviceOrientation;
769
+ if (ori) setData({
770
+ alpha: (_b = ori.alpha) != null ? _b : 0,
771
+ beta: (_c = ori.beta) != null ? _c : 0,
772
+ gamma: (_d = ori.gamma) != null ? _d : 0,
773
+ absolute: ori.absolute
774
+ });
775
+ });
776
+ useTelegramEvent("deviceOrientationStarted", () => setIsStarted(true));
777
+ useTelegramEvent("deviceOrientationStopped", () => setIsStarted(false));
778
+ const start = (0, import_react.useCallback)(() => {
779
+ var _a, _b;
780
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.DeviceOrientation) == null ? void 0 : _b.start({ refresh_rate: refreshRate, need_absolute: needAbsolute });
781
+ }, [refreshRate, needAbsolute]);
782
+ const stop = (0, import_react.useCallback)(() => {
783
+ var _a, _b;
784
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.DeviceOrientation) == null ? void 0 : _b.stop();
785
+ }, []);
786
+ (0, import_react.useEffect)(() => {
787
+ if (autoStart) start();
788
+ return () => {
789
+ if (autoStart) stop();
790
+ };
791
+ }, [autoStart, start, stop]);
792
+ return __spreadProps(__spreadValues({}, data), { isStarted, start, stop });
793
+ }
794
+ function useBiometric() {
795
+ const [isInited, setIsInited] = (0, import_react.useState)(false);
796
+ const [isAvailable, setIsAvailable] = (0, import_react.useState)(false);
797
+ const [biometricType, setBiometricType] = (0, import_react.useState)("unknown");
798
+ useTelegramEvent("biometricManagerUpdated", () => {
799
+ var _a;
800
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
801
+ if (!bio) return;
802
+ setIsInited(bio.isInited);
803
+ setIsAvailable(bio.isBiometricAvailable);
804
+ setBiometricType(bio.biometricType);
805
+ });
806
+ const init = (0, import_react.useCallback)(() => new Promise((resolve, reject) => {
807
+ var _a;
808
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
809
+ if (!bio) return reject(new Error("BiometricManager not available"));
810
+ bio.init(() => {
811
+ setIsInited(true);
812
+ setIsAvailable(bio.isBiometricAvailable);
813
+ setBiometricType(bio.biometricType);
814
+ resolve();
815
+ });
816
+ }), []);
817
+ const requestAccess = (0, import_react.useCallback)((reason) => new Promise((resolve, reject) => {
818
+ var _a;
819
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
820
+ if (!bio) return reject(new Error("BiometricManager not available"));
821
+ bio.requestAccess({ reason }, resolve);
822
+ }), []);
823
+ const authenticate = (0, import_react.useCallback)((reason) => new Promise((resolve, reject) => {
824
+ var _a;
825
+ const bio = (_a = getWebApp()) == null ? void 0 : _a.BiometricManager;
826
+ if (!bio) return reject(new Error("BiometricManager not available"));
827
+ bio.authenticate({ reason }, (authenticated, token) => resolve({ authenticated, token }));
828
+ }), []);
829
+ return { isInited, isAvailable, biometricType, init, requestAccess, authenticate };
830
+ }
831
+ function useLocation() {
832
+ const [isAvailable, setIsAvailable] = (0, import_react.useState)(false);
833
+ const [isGranted, setIsGranted] = (0, import_react.useState)(false);
834
+ useTelegramEvent("locationManagerUpdated", () => {
835
+ var _a;
836
+ const loc = (_a = getWebApp()) == null ? void 0 : _a.LocationManager;
837
+ if (!loc) return;
838
+ setIsAvailable(loc.isLocationAvailable);
839
+ setIsGranted(loc.isAccessGranted);
840
+ });
841
+ const init = (0, import_react.useCallback)(() => new Promise((resolve, reject) => {
842
+ var _a;
843
+ const loc = (_a = getWebApp()) == null ? void 0 : _a.LocationManager;
844
+ if (!loc) return reject(new Error("LocationManager not available"));
845
+ loc.init((err) => {
846
+ if (err) reject(err);
847
+ else {
848
+ setIsAvailable(loc.isLocationAvailable);
849
+ setIsGranted(loc.isAccessGranted);
850
+ resolve();
851
+ }
852
+ });
853
+ }), []);
854
+ const getLocation = (0, import_react.useCallback)(() => new Promise((resolve, reject) => {
855
+ var _a;
856
+ const loc = (_a = getWebApp()) == null ? void 0 : _a.LocationManager;
857
+ if (!loc) return reject(new Error("LocationManager not available"));
858
+ loc.getLocation(resolve);
859
+ }), []);
860
+ const openSettings = (0, import_react.useCallback)(() => {
861
+ var _a, _b;
862
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.LocationManager) == null ? void 0 : _b.openSettings();
863
+ }, []);
864
+ return { isAvailable, isGranted, init, getLocation, openSettings };
865
+ }
866
+ function useHomeScreen() {
867
+ const addToHomeScreen = (0, import_react.useCallback)(() => {
868
+ var _a, _b;
869
+ (_b = (_a = getWebApp()) == null ? void 0 : _a.addToHomeScreen) == null ? void 0 : _b.call(_a);
870
+ }, []);
871
+ const checkStatus = (0, import_react.useCallback)(() => new Promise((resolve, reject) => {
872
+ const wa = getWebApp();
873
+ if (!(wa == null ? void 0 : wa.checkHomeScreenStatus)) return reject(new Error("Not supported"));
874
+ wa.checkHomeScreenStatus((status) => resolve(status != null ? status : "unsupported"));
875
+ }), []);
876
+ return { addToHomeScreen, checkStatus };
877
+ }
878
+ function useIsActive() {
879
+ const [isActive, setIsActive] = (0, import_react.useState)(() => {
880
+ var _a;
881
+ return Boolean((_a = getWebApp()) == null ? void 0 : _a.isActive);
882
+ });
883
+ useTelegramEvent("activated", () => setIsActive(true));
884
+ useTelegramEvent("deactivated", () => setIsActive(false));
885
+ return isActive;
886
+ }
887
+ function useTelegramStartParam() {
888
+ var _a, _b, _c;
889
+ return (_c = (_b = (_a = getWebApp()) == null ? void 0 : _a.initDataUnsafe) == null ? void 0 : _b.start_param) != null ? _c : null;
890
+ }
891
+
892
+ // src/providers/TelegramProvider.tsx
893
+ var import_react2 = require("react");
894
+ var import_jsx_runtime = require("react/jsx-runtime");
895
+ var defaultCtx = {
896
+ ready: false,
897
+ inTelegram: false,
898
+ bypass: false,
899
+ webApp: null,
900
+ user: null,
901
+ language: "en-US",
902
+ uiLang: "en",
903
+ colorScheme: "dark",
904
+ startParam: null,
905
+ isRtl: false,
906
+ changeLanguage: () => {
907
+ }
908
+ };
909
+ var TelegramContext = (0, import_react2.createContext)(defaultCtx);
910
+ function useTelegram() {
911
+ return (0, import_react2.useContext)(TelegramContext);
912
+ }
913
+ function applyLangToDOM(lang) {
914
+ const rtl = isRtlLang(lang);
915
+ document.documentElement.setAttribute("dir", rtl ? "rtl" : "ltr");
916
+ document.documentElement.setAttribute("lang", lang);
917
+ }
918
+ function applyColorSchemeToDOM(scheme) {
919
+ if (scheme === "dark") {
920
+ document.documentElement.classList.add("dark");
921
+ } else {
922
+ document.documentElement.classList.remove("dark");
923
+ }
924
+ }
925
+ function TelegramProvider({
926
+ children,
927
+ options = {}
928
+ }) {
929
+ const {
930
+ langStorageKey = "tg-kit-ui-lang",
931
+ onUserReady,
932
+ onReady,
933
+ onLanguageChange,
934
+ loadingComponent = null,
935
+ notInTelegramComponent = null,
936
+ allowOutsideTelegram = false
937
+ } = options;
938
+ const [isInitialized, setIsInitialized] = (0, import_react2.useState)(false);
939
+ const [ctx, setCtx] = (0, import_react2.useState)(defaultCtx);
940
+ const getSavedLang = (0, import_react2.useCallback)(() => {
941
+ try {
942
+ return localStorage.getItem(langStorageKey);
943
+ } catch (e) {
944
+ return null;
945
+ }
946
+ }, [langStorageKey]);
947
+ const saveLang = (0, import_react2.useCallback)((lang) => {
948
+ try {
949
+ localStorage.setItem(langStorageKey, lang);
950
+ } catch (e) {
951
+ }
952
+ }, [langStorageKey]);
953
+ const changeLanguage = (0, import_react2.useCallback)((lang) => {
954
+ saveLang(lang);
955
+ applyLangToDOM(lang);
956
+ onLanguageChange == null ? void 0 : onLanguageChange(lang);
957
+ setCtx((prev) => __spreadProps(__spreadValues({}, prev), {
958
+ uiLang: lang,
959
+ language: tgLangToTmdb(lang),
960
+ isRtl: isRtlLang(lang)
961
+ }));
962
+ }, [saveLang, onLanguageChange]);
963
+ (0, import_react2.useLayoutEffect)(() => {
964
+ var _a, _b, _c, _d, _e, _f, _g;
965
+ const wa = getWebApp();
966
+ const inTg = isInTelegram();
967
+ const isDev = process.env.NODE_ENV === "development" || typeof window !== "undefined" && new URLSearchParams(window.location.search).has("bypass");
968
+ const bypass = !inTg && isDev;
969
+ let user = null;
970
+ let startParam = null;
971
+ let colorScheme = "dark";
972
+ if (wa) {
973
+ try {
974
+ wa.expand();
975
+ } catch (e) {
976
+ }
977
+ try {
978
+ (_a = wa.disableVerticalSwipes) == null ? void 0 : _a.call(wa);
979
+ } catch (e) {
980
+ }
981
+ try {
982
+ (_b = wa.enableClosingConfirmation) == null ? void 0 : _b.call(wa);
983
+ } catch (e) {
984
+ }
985
+ colorScheme = (_c = wa.colorScheme) != null ? _c : "dark";
986
+ user = (_e = (_d = wa.initDataUnsafe) == null ? void 0 : _d.user) != null ? _e : null;
987
+ startParam = (_g = (_f = wa.initDataUnsafe) == null ? void 0 : _f.start_param) != null ? _g : null;
988
+ if (user) onUserReady == null ? void 0 : onUserReady(user);
989
+ onReady == null ? void 0 : onReady(wa);
990
+ }
991
+ const tgLangCode = user == null ? void 0 : user.language_code;
992
+ const tgUiLang = tgLangToUi(tgLangCode);
993
+ const savedLang = getSavedLang();
994
+ const finalUiLang = savedLang != null ? savedLang : tgUiLang;
995
+ const finalLanguage = tgLangToTmdb(finalUiLang);
996
+ if (!savedLang) saveLang(finalUiLang);
997
+ applyLangToDOM(finalUiLang);
998
+ applyColorSchemeToDOM(colorScheme);
999
+ onLanguageChange == null ? void 0 : onLanguageChange(finalUiLang);
1000
+ setCtx({
1001
+ ready: true,
1002
+ inTelegram: inTg,
1003
+ bypass,
1004
+ webApp: wa,
1005
+ user,
1006
+ language: finalLanguage,
1007
+ uiLang: finalUiLang,
1008
+ colorScheme,
1009
+ startParam,
1010
+ isRtl: isRtlLang(finalUiLang),
1011
+ changeLanguage
1012
+ });
1013
+ setIsInitialized(true);
1014
+ try {
1015
+ wa == null ? void 0 : wa.ready();
1016
+ } catch (e) {
1017
+ }
1018
+ }, [changeLanguage, getSavedLang, saveLang, onUserReady, onReady, onLanguageChange]);
1019
+ if (!isInitialized) return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: loadingComponent });
1020
+ if (ctx.ready && !ctx.inTelegram && !ctx.bypass && !allowOutsideTelegram) {
1021
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: notInTelegramComponent });
1022
+ }
1023
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TelegramContext.Provider, { value: ctx, children });
1024
+ }
1025
+
1026
+ // src/providers/FullscreenProvider.tsx
1027
+ var import_react3 = require("react");
1028
+ var import_jsx_runtime2 = require("react/jsx-runtime");
1029
+ var DEFAULT_INSET = { top: 0, bottom: 0, left: 0, right: 0 };
1030
+ var SAFE_AREA_VARS = {
1031
+ top: "--tg-safe-area-inset-top",
1032
+ bottom: "--tg-safe-area-inset-bottom",
1033
+ left: "--tg-safe-area-inset-left",
1034
+ right: "--tg-safe-area-inset-right"
1035
+ };
1036
+ var CONTENT_SAFE_AREA_VARS = {
1037
+ top: "--tg-content-safe-area-inset-top",
1038
+ bottom: "--tg-content-safe-area-inset-bottom",
1039
+ left: "--tg-content-safe-area-inset-left",
1040
+ right: "--tg-content-safe-area-inset-right"
1041
+ };
1042
+ function applySafeAreaVars(inset, vars) {
1043
+ const root = document.documentElement.style;
1044
+ root.setProperty(vars.top, `${inset.top}px`);
1045
+ root.setProperty(vars.bottom, `${inset.bottom}px`);
1046
+ root.setProperty(vars.left, `${inset.left}px`);
1047
+ root.setProperty(vars.right, `${inset.right}px`);
1048
+ }
1049
+ var FullscreenContext = (0, import_react3.createContext)(null);
1050
+ function useFullscreen() {
1051
+ const ctx = (0, import_react3.useContext)(FullscreenContext);
1052
+ if (!ctx) throw new Error("useFullscreen must be used within FullscreenProvider");
1053
+ return ctx;
1054
+ }
1055
+ function FullscreenProvider({
1056
+ children,
1057
+ options = {}
1058
+ }) {
1059
+ const { storageKey = "tg-kit-fullscreen", persistPreference = true } = options;
1060
+ const [isFullscreen, setIsFullscreen] = (0, import_react3.useState)(false);
1061
+ const [isSupported, setIsSupported] = (0, import_react3.useState)(false);
1062
+ const [isActive, setIsActive] = (0, import_react3.useState)(true);
1063
+ const [isOrientationLocked, setIsOrientationLocked] = (0, import_react3.useState)(false);
1064
+ const [lastFullscreenError, setLastFullscreenError] = (0, import_react3.useState)(null);
1065
+ const [safeArea, setSafeArea] = (0, import_react3.useState)(DEFAULT_INSET);
1066
+ const [contentSafeArea, setContentSafeArea] = (0, import_react3.useState)(DEFAULT_INSET);
1067
+ const isInitializedRef = (0, import_react3.useRef)(false);
1068
+ const savePreference = (0, import_react3.useCallback)((value) => {
1069
+ if (!persistPreference) return;
1070
+ try {
1071
+ localStorage.setItem(storageKey, String(value));
1072
+ } catch (e) {
1073
+ }
1074
+ }, [storageKey, persistPreference]);
1075
+ const enterFullscreen = (0, import_react3.useCallback)(() => {
1076
+ const wa = getWebApp();
1077
+ if (!(wa == null ? void 0 : wa.requestFullscreen)) return;
1078
+ setLastFullscreenError(null);
1079
+ try {
1080
+ wa.requestFullscreen();
1081
+ savePreference(true);
1082
+ } catch (e) {
1083
+ }
1084
+ }, [savePreference]);
1085
+ const exitFullscreen = (0, import_react3.useCallback)(() => {
1086
+ const wa = getWebApp();
1087
+ if (!(wa == null ? void 0 : wa.exitFullscreen)) return;
1088
+ try {
1089
+ wa.exitFullscreen();
1090
+ savePreference(false);
1091
+ } catch (e) {
1092
+ }
1093
+ }, [savePreference]);
1094
+ const toggleFullscreen = (0, import_react3.useCallback)(() => {
1095
+ if (isFullscreen) exitFullscreen();
1096
+ else enterFullscreen();
1097
+ }, [isFullscreen, enterFullscreen, exitFullscreen]);
1098
+ (0, import_react3.useEffect)(() => {
1099
+ const wa = getWebApp();
1100
+ if (!wa) return;
1101
+ setIsSupported(typeof wa.requestFullscreen === "function");
1102
+ setIsFullscreen(Boolean(wa.isFullscreen));
1103
+ setIsActive(Boolean(wa.isActive));
1104
+ setIsOrientationLocked(Boolean(wa.isOrientationLocked));
1105
+ if (wa.safeAreaInset) {
1106
+ setSafeArea(wa.safeAreaInset);
1107
+ applySafeAreaVars(wa.safeAreaInset, SAFE_AREA_VARS);
1108
+ }
1109
+ if (wa.contentSafeAreaInset) {
1110
+ setContentSafeArea(wa.contentSafeAreaInset);
1111
+ applySafeAreaVars(wa.contentSafeAreaInset, CONTENT_SAFE_AREA_VARS);
1112
+ }
1113
+ const onFullscreen = () => setIsFullscreen(Boolean(wa.isFullscreen));
1114
+ const onFullscreenFailed = (...args) => {
1115
+ const err = args[0];
1116
+ if (err && typeof err === "object" && "error" in err) {
1117
+ setLastFullscreenError(err);
1118
+ }
1119
+ };
1120
+ const onActivated = () => setIsActive(true);
1121
+ const onDeactivated = () => setIsActive(false);
1122
+ const onSafeArea = () => {
1123
+ if (!wa.safeAreaInset) return;
1124
+ setSafeArea(wa.safeAreaInset);
1125
+ applySafeAreaVars(wa.safeAreaInset, SAFE_AREA_VARS);
1126
+ };
1127
+ const onContentSafeArea = () => {
1128
+ if (!wa.contentSafeAreaInset) return;
1129
+ setContentSafeArea(wa.contentSafeAreaInset);
1130
+ applySafeAreaVars(wa.contentSafeAreaInset, CONTENT_SAFE_AREA_VARS);
1131
+ };
1132
+ const onOrientationLocked = () => setIsOrientationLocked(Boolean(wa.isOrientationLocked));
1133
+ wa.onEvent("fullscreenChanged", onFullscreen);
1134
+ wa.onEvent("fullscreenFailed", onFullscreenFailed);
1135
+ wa.onEvent("activated", onActivated);
1136
+ wa.onEvent("deactivated", onDeactivated);
1137
+ wa.onEvent("safeAreaChanged", onSafeArea);
1138
+ wa.onEvent("contentSafeAreaChanged", onContentSafeArea);
1139
+ wa.onEvent("orientationLockedChanged", onOrientationLocked);
1140
+ if (!isInitializedRef.current && persistPreference) {
1141
+ isInitializedRef.current = true;
1142
+ try {
1143
+ const saved = localStorage.getItem(storageKey) === "true";
1144
+ if (saved && !wa.isFullscreen && wa.requestFullscreen) {
1145
+ setTimeout(() => {
1146
+ var _a;
1147
+ return (_a = wa.requestFullscreen) == null ? void 0 : _a.call(wa);
1148
+ }, 100);
1149
+ }
1150
+ } catch (e) {
1151
+ }
1152
+ }
1153
+ return () => {
1154
+ wa.offEvent("fullscreenChanged", onFullscreen);
1155
+ wa.offEvent("fullscreenFailed", onFullscreenFailed);
1156
+ wa.offEvent("activated", onActivated);
1157
+ wa.offEvent("deactivated", onDeactivated);
1158
+ wa.offEvent("safeAreaChanged", onSafeArea);
1159
+ wa.offEvent("contentSafeAreaChanged", onContentSafeArea);
1160
+ wa.offEvent("orientationLockedChanged", onOrientationLocked);
1161
+ };
1162
+ }, [storageKey, persistPreference]);
1163
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1164
+ FullscreenContext.Provider,
1165
+ {
1166
+ value: {
1167
+ isFullscreen,
1168
+ isSupported,
1169
+ isActive,
1170
+ isOrientationLocked,
1171
+ safeArea,
1172
+ contentSafeArea,
1173
+ lastFullscreenError,
1174
+ enterFullscreen,
1175
+ exitFullscreen,
1176
+ toggleFullscreen
1177
+ },
1178
+ children
1179
+ }
1180
+ );
1181
+ }
1182
+ // Annotate the CommonJS export names for ESM import in node:
1183
+ 0 && (module.exports = {
1184
+ FullscreenProvider,
1185
+ RTL_LANGS,
1186
+ SUPPORTED_LANGS,
1187
+ TelegramProvider,
1188
+ biometric,
1189
+ cloudStorage,
1190
+ dialog,
1191
+ getRawUserData,
1192
+ getUserAvatarUrl,
1193
+ getUserDisplayName,
1194
+ getUserIdentifier,
1195
+ getUserInfoWithAvatar,
1196
+ getWebApp,
1197
+ haptic,
1198
+ isInTelegram,
1199
+ isRtlLang,
1200
+ isVersionAtLeast,
1201
+ location,
1202
+ openExternalLink,
1203
+ openInvoice,
1204
+ openTelegramLink,
1205
+ readClipboard,
1206
+ scanQr,
1207
+ tgLangToTmdb,
1208
+ tgLangToUi,
1209
+ useAccelerometer,
1210
+ useBiometric,
1211
+ useCloudStorage,
1212
+ useDeviceOrientation,
1213
+ useFullscreen,
1214
+ useGyroscope,
1215
+ useHapticFeedback,
1216
+ useHomeScreen,
1217
+ useIsActive,
1218
+ useLocation,
1219
+ useSafeArea,
1220
+ useTelegram,
1221
+ useTelegramBackButton,
1222
+ useTelegramEvent,
1223
+ useTelegramFullscreen,
1224
+ useTelegramMainButton,
1225
+ useTelegramSecondaryButton,
1226
+ useTelegramSettingsButton,
1227
+ useTelegramStartParam,
1228
+ useTelegramTheme,
1229
+ useTelegramUser,
1230
+ useTelegramViewport,
1231
+ useTelegramWebApp
1232
+ });