@tma.js/sdk 0.12.7 → 0.12.9

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/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [code-link]: https://github.com/Telegram-Mini-Apps/tma.js/tree/master/packages/sdk
6
6
 
7
- [docs-link]: https://docs.telegram-mini-apps.com/docs/libraries/tma-js-sdk
7
+ [docs-link]: https://docs.telegram-mini-apps.com/packages/typescript/tma-js-sdk/about
8
8
 
9
9
  [docs-badge]: https://img.shields.io/badge/documentation-blue?logo=gitbook&logoColor=white
10
10
 
@@ -19,11 +19,11 @@
19
19
  [![docs-badge]][docs-link]
20
20
  [![code-badge]][code-link]
21
21
 
22
- Made from scratch production-ready TypeScript Telegram Web Apps
22
+ Made from scratch production-ready TypeScript Telegram Mini Apps
23
23
  Source Development Kit. It includes all features provided
24
24
  by other packages extending them with intuitively clear
25
25
  functionality.
26
26
 
27
27
  This library is a part of TypeScript packages ecosystem around Telegram Web
28
28
  Apps. You can learn more about this package in this
29
- [documentation](https://docs.telegram-mini-apps.com/docs/libraries/tma-js-sdk/about).
29
+ [documentation][docs-link].
@@ -3,7 +3,7 @@ import type { Version } from '@tma.js/utils';
3
3
  import { type SupportsFunc } from '../../supports.js';
4
4
  /**
5
5
  * Class which controls the back button displayed in the header
6
- * of the Web App in the Telegram interface. It is mostly used in case, when
6
+ * of the Mini App in the Telegram interface. It is mostly used in case, when
7
7
  * you want to provide a way to go bach in routing history or "rollback" some
8
8
  * action.
9
9
  */
@@ -11,17 +11,17 @@ export declare class ClosingBehaviour {
11
11
  private set isConfirmationNeeded(value);
12
12
  /**
13
13
  * Returns true, if the confirmation dialog enabled while the user is trying
14
- * to close the Web App.
14
+ * to close the Mini App.
15
15
  */
16
16
  get isConfirmationNeeded(): boolean;
17
17
  /**
18
18
  * Disables the confirmation dialog while the user is trying to close the
19
- * Web App.
19
+ * Mini App.
20
20
  */
21
21
  disableConfirmation(): void;
22
22
  /**
23
23
  * Enables the confirmation dialog while the user is trying to close the
24
- * Web App.
24
+ * Mini App.
25
25
  */
26
26
  enableConfirmation(): void;
27
27
  /**
@@ -1,6 +1,6 @@
1
1
  import type { Chat, ChatType, InitData as InitDataType, User } from '@tma.js/init-data';
2
2
  /**
3
- * Class which is responsible for displaying Web Apps init data.
3
+ * Class which is responsible for displaying Mini Apps init data.
4
4
  */
5
5
  export declare class InitData {
6
6
  private readonly state;
@@ -10,15 +10,14 @@ export declare class InitData {
10
10
  */
11
11
  get authDate(): Date;
12
12
  /**
13
- * Date after which a message can be sent via the answerWebAppQuery
14
- * method.
15
- * @see https://core.telegram.org/bots/api#answerwebappquery
13
+ * Date after which a message can be sent via the
14
+ * [answerWebAppQuery](https://core.telegram.org/bots/api#answerwebappquery) method.
16
15
  */
17
16
  get canSendAfter(): Date | null;
18
17
  /**
19
- * An object containing data about the chat where the bot was
20
- * launched via the attachment menu. Returned for supergroups, channels and
21
- * group chats – only for Web Apps launched via the attachment menu.
18
+ * An object containing data about the chat where the bot was launched via the attachment
19
+ * menu. Returned for supergroups, channels and group chats – only for Mini Apps launched via
20
+ * the attachment menu.
22
21
  */
23
22
  get chat(): Chat | null;
24
23
  /**
@@ -26,10 +25,10 @@ export declare class InitData {
26
25
  */
27
26
  get chatType(): ChatType | null;
28
27
  /**
29
- * A global identifier indicating the chat from which Mini App was
30
- * opened. Returned only for applications opened by direct link.
28
+ * A global identifier indicating the chat from which Mini App was opened. Returned only for
29
+ * applications opened by direct link.
31
30
  */
32
- get chatInstance(): ChatType | null;
31
+ get chatInstance(): string | null;
33
32
  /**
34
33
  * A hash of all passed parameters, which the bot server can use to
35
34
  * check their validity.
@@ -37,7 +36,7 @@ export declare class InitData {
37
36
  */
38
37
  get hash(): string;
39
38
  /**
40
- * A unique identifier for the Web App session, required for sending
39
+ * A unique identifier for the Mini App session, required for sending
41
40
  * messages via the `answerWebAppQuery` method.
42
41
  * @see https://core.telegram.org/bots/api#answerwebappquery
43
42
  */
@@ -45,13 +44,13 @@ export declare class InitData {
45
44
  /**
46
45
  * An object containing data about the chat partner of the current
47
46
  * user in the chat where the bot was launched via the attachment menu.
48
- * Returned only for private chats and only for Web Apps launched
47
+ * Returned only for private chats and only for Mini Apps launched
49
48
  * via the attachment menu.
50
49
  */
51
50
  get receiver(): User | null;
52
51
  /**
53
52
  * The value of the `startattach` parameter, passed via link. Only
54
- * returned for Web Apps when launched from the attachment menu via link.
53
+ * returned for Mini Apps when launched from the attachment menu via link.
55
54
  */
56
55
  get startParam(): string | null;
57
56
  /**
@@ -2,7 +2,7 @@ import { type PostEvent } from '@tma.js/bridge';
2
2
  import type { RGB } from '@tma.js/colors';
3
3
  /**
4
4
  * Controls the main button, which is displayed at the bottom
5
- * of the Web App in the Telegram interface.
5
+ * of the Mini App in the Telegram interface.
6
6
  */
7
7
  export declare class MainButton {
8
8
  private readonly postEvent;
@@ -72,8 +72,8 @@ export declare class MainButton {
72
72
  */
73
73
  off: typeof this.ee.off;
74
74
  /**
75
- * Shows the button. Note that opening the Web App from the attachment
76
- * menu hides the main button until the user interacts with the Web App
75
+ * Shows the button. Note that opening the Mini App from the attachment
76
+ * menu hides the main button until the user interacts with the Mini App
77
77
  * interface.
78
78
  *
79
79
  * Returns current button instance for chaining.
@@ -27,7 +27,7 @@ export declare class Viewport {
27
27
  static sync(viewport: Viewport): void;
28
28
  /**
29
29
  * Returns initialized instance of Viewport which is synchronized with
30
- * its actual state in Web Apps.
30
+ * its actual state in Mini Apps.
31
31
  * @param options - method options.
32
32
  */
33
33
  static synced(options?: RequestOptions): Promise<Viewport>;
@@ -35,12 +35,12 @@ export declare class Viewport {
35
35
  private readonly state;
36
36
  constructor(height: number, width: number, stableHeight: number, isExpanded: boolean, postEvent?: PostEvent);
37
37
  /**
38
- * The current height of the visible area of the Web App.
38
+ * The current height of the visible area of the Mini App.
39
39
  *
40
- * The application can display just the top part of the Web App, with its
40
+ * The application can display just the top part of the Mini App, with its
41
41
  * lower part remaining outside the screen area. From this position, the
42
- * user can "pull" the Web App to its maximum height, while the bot can do
43
- * the same by calling `expand` method. As the position of the Web App
42
+ * user can "pull" the Mini App to its maximum height, while the bot can do
43
+ * the same by calling `expand` method. As the position of the Mini App
44
44
  * changes, the current height value of the visible area will be updated
45
45
  * in real time.
46
46
  *
@@ -56,18 +56,18 @@ export declare class Viewport {
56
56
  */
57
57
  get height(): number;
58
58
  /**
59
- * The height of the visible area of the Web App in its last stable state.
59
+ * The height of the visible area of the Mini App in its last stable state.
60
60
  *
61
- * The application can display just the top part of the Web App, with its
61
+ * The application can display just the top part of the Mini App, with its
62
62
  * lower part remaining outside the screen area. From this position,
63
- * the user can "pull" the Web App to its maximum height, while the bot can
63
+ * the user can "pull" the Mini App to its maximum height, while the bot can
64
64
  * do the same by calling `expand` method.
65
65
  *
66
66
  * Unlike the value of `height`, the value of `stableHeight`
67
- * does not change as the position of the Web App changes with user
67
+ * does not change as the position of the Mini App changes with user
68
68
  * gestures or during animations. The value of `stableHeight`
69
69
  * will be updated after all gestures and animations are completed and
70
- * the Web App reaches its final size.
70
+ * the Mini App reaches its final size.
71
71
  *
72
72
  * @see init
73
73
  * @see expand
@@ -75,8 +75,8 @@ export declare class Viewport {
75
75
  */
76
76
  get stableHeight(): number;
77
77
  /**
78
- * Returns true if the Web App is expanded to the maximum available height.
79
- * Otherwise, if the Web App occupies part of the screen and can be expanded
78
+ * Returns true if the Mini App is expanded to the maximum available height.
79
+ * Otherwise, if the Mini App occupies part of the screen and can be expanded
80
80
  * to the full height using `expand` method.
81
81
  * @see expand
82
82
  */
@@ -86,8 +86,8 @@ export declare class Viewport {
86
86
  */
87
87
  get width(): number;
88
88
  /**
89
- * A method that expands the Web App to the maximum available height. To
90
- * find out if the Web App is expanded to the maximum height, refer to the
89
+ * A method that expands the Mini App to the maximum available height. To
90
+ * find out if the Mini App is expanded to the maximum height, refer to the
91
91
  * value of the `isExpanded`.
92
92
  * @see isExpanded
93
93
  */
@@ -6,7 +6,7 @@ import { type SupportsFunc } from '../../supports.js';
6
6
  import type { ColorScheme } from '../../types.js';
7
7
  import type { WebAppEvents, WebAppHeaderColor } from './types.js';
8
8
  /**
9
- * Provides common Web Apps functionality not covered by other system
9
+ * Provides common Mini Apps functionality not covered by other system
10
10
  * components.
11
11
  */
12
12
  export declare class WebApp {
@@ -27,7 +27,7 @@ export declare class WebApp {
27
27
  */
28
28
  get colorScheme(): ColorScheme;
29
29
  /**
30
- * Closes the Web App.
30
+ * Closes the Mini App.
31
31
  */
32
32
  close(): void;
33
33
  /**
@@ -36,22 +36,22 @@ export declare class WebApp {
36
36
  get headerColor(): WebAppHeaderColor;
37
37
  /**
38
38
  * Returns true if passed version is more than or equal to current
39
- * Web App version.
39
+ * Mini App version.
40
40
  * @param version - compared version.
41
41
  */
42
42
  isVersionAtLeast(version: Version): boolean;
43
43
  /**
44
- * Opens a link in an external browser. The Web App will not be closed.
44
+ * Opens a link in an external browser. The Mini App will not be closed.
45
45
  *
46
46
  * Note that this method can be called only in response to the user
47
- * interaction with the Web App interface (e.g. click inside the Web App
47
+ * interaction with the Mini App interface (e.g. click inside the Mini App
48
48
  * or on the main button).
49
49
  * @param url - URL to be opened.
50
50
  * @param tryInstantView
51
51
  */
52
52
  openLink(url: string, tryInstantView?: boolean): void;
53
53
  /**
54
- * Opens a Telegram link inside Telegram app. The Web App will be closed.
54
+ * Opens a Telegram link inside Telegram app. The Mini App will be closed.
55
55
  * It expects passing link in full format, with hostname "t.me".
56
56
  * @param url - URL to be opened.
57
57
  * @throws {Error} URL has not allowed hostname.
@@ -72,15 +72,15 @@ export declare class WebApp {
72
72
  */
73
73
  off: <E extends "changed" | "backgroundColorChanged" | "headerColorChanged">(event: E, listener: import("@tma.js/event-emitter").EventListener<WebAppEvents[E]>) => void;
74
74
  /**
75
- * Returns current Web App platform.
75
+ * Returns current Mini App platform.
76
76
  */
77
77
  get platform(): Platform;
78
78
  /**
79
- * Informs the Telegram app that the Web App is ready to be displayed.
79
+ * Informs the Telegram app that the Mini App is ready to be displayed.
80
80
  *
81
81
  * It is recommended to call this method as early as possible, as soon as
82
82
  * all essential interface elements loaded. Once this method called,
83
- * the loading placeholder is hidden and the Web App shown.
83
+ * the loading placeholder is hidden and the Mini App shown.
84
84
  *
85
85
  * If the method not called, the placeholder will be hidden only when
86
86
  * the page fully loaded.
@@ -104,10 +104,10 @@ export declare class WebApp {
104
104
  /**
105
105
  * A method used to send data to the bot. When this method called, a
106
106
  * service message sent to the bot containing the data of the
107
- * length up to 4096 bytes, and the Web App closed. See the field
107
+ * length up to 4096 bytes, and the Mini App closed. See the field
108
108
  * `web_app_data` in the class Message.
109
109
  *
110
- * This method is only available for Web Apps launched via a Keyboard button.
110
+ * This method is only available for Mini Apps launched via a Keyboard button.
111
111
  * @param data - data to send to bot.
112
112
  * @throws {Error} data has incorrect size.
113
113
  */
@@ -139,7 +139,7 @@ export declare class WebApp {
139
139
  */
140
140
  supportsParam: SupportsFunc<'setHeaderColor.color' | 'openLink.tryInstantView'>;
141
141
  /**
142
- * Current Web App version. This property is used by other components to check if
142
+ * Current Mini App version. This property is used by other components to check if
143
143
  * some functionality is available on current device.
144
144
  */
145
145
  get version(): Version;
package/dist/dts/env.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Returns true in case, current environment is Telegram Web Apps.
2
+ * Returns true in case, current environment is Telegram Mini Apps.
3
3
  *
4
4
  * `isTWA` utilizes such function as `retrieveLaunchParams`, which attempts to retrieve
5
5
  * launch parameters from the current environment.
@@ -5,7 +5,7 @@ import { Viewport } from '../../components/index.js';
5
5
  * Creates Viewport instance using last locally saved data also saving each state in
6
6
  * the storage.
7
7
  * @param isPageReload - was current page reloaded.
8
- * @param platform - Telegram Web Apps platform name.
8
+ * @param platform - Telegram Mini Apps platform name.
9
9
  * @param postEvent - Bridge postEvent function
10
10
  */
11
11
  export declare function createViewport(isPageReload: boolean, platform: Platform, postEvent: PostEvent): Promise<Viewport>;
@@ -9,7 +9,7 @@ import type { CreateRequestIdFunc } from '../../types.js';
9
9
  * @param isPageReload - was current page reloaded.
10
10
  * @param backgroundColor - web app background color.
11
11
  * @param version - platform version.
12
- * @param platform - Telegram Web Apps platform name.
12
+ * @param platform - Telegram Mini Apps platform name.
13
13
  * @param createRequestId - function which generates request identifiers.
14
14
  * @param postEvent - Bridge postEvent function
15
15
  */
@@ -62,7 +62,7 @@ export interface InitOptions {
62
62
  acceptCustomStyles?: boolean;
63
63
  /**
64
64
  * Should SDK throw an error in case, unsupported by current version of
65
- * Web App method was called. It is highly recommended not to disable this
65
+ * Mini App method was called. It is highly recommended not to disable this
66
66
  * feature as long as it helps developer to find issues related to usage
67
67
  * of unsupported methods.
68
68
  *
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var pt=Object.defineProperty;var lt=(s,t,e)=>t in s?pt(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var i=(s,t,e)=>(lt(s,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@tma.js/launch-params"),f=require("@tma.js/event-emitter"),a=require("@tma.js/bridge"),P=require("@tma.js/parsing"),W=require("@tma.js/theme-params"),A=require("@tma.js/colors"),R=require("@tma.js/utils");function C(s,t){return e=>a.supports(t[e],s)}function dt(s,t){return e=>{const[o,r]=t[e];return a.supports(o,r,s)}}class _{constructor(t,e){this.state=t,this.ee=e}emit(t,e){this.ee&&this.ee.emit(t,e)}internalSet(t,e){return this.state[t]===e?!1:(this.state[t]=e,this.emit(`${t}Changed`,e),!0)}set(t,e){let o=!1;if(typeof t=="string")o=this.internalSet(t,e);else for(const r in t)this.internalSet(r,t[r])&&(o=!0);o&&this.emit("changed")}get(t){return this.state[t]}}class K{constructor(t,e,o=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",(t,e)=>t==="click"?a.on("back_button_pressed",e):this.ee.on(t,e));i(this,"off",(t,e)=>{if(t==="click")return a.off("back_button_pressed",e);this.ee.off(t,e)});i(this,"supports");this.postEvent=o,this.state=new _({isVisible:t},this.ee),this.supports=C(e,{show:"web_app_setup_back_button",hide:"web_app_setup_back_button"})}set isVisible(t){this.state.set("isVisible",t),this.postEvent("web_app_setup_back_button",{is_visible:t})}get isVisible(){return this.state.get("isVisible")}hide(){this.isVisible=!1}show(){this.isVisible=!0}}class j{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=e,this.state=new _({isConfirmationNeeded:t},this.ee)}set isConfirmationNeeded(t){this.state.set("isConfirmationNeeded",t),this.postEvent("web_app_setup_closing_behavior",{need_confirmation:t})}get isConfirmationNeeded(){return this.state.get("isConfirmationNeeded")}disableConfirmation(){this.isConfirmationNeeded=!1}enableConfirmation(){this.isConfirmationNeeded=!0}}const gt=P.array().of(P.string());function z(s,t){return s.reduce((e,o)=>(e[o]=t,e),{})}class J{constructor(t,e,o=a.postEvent){i(this,"supports");this.createRequestId=e,this.postEvent=o,this.supports=C(t,{deleteKeys:"web_app_invoke_custom_method",getKeys:"web_app_invoke_custom_method",getValues:"web_app_invoke_custom_method",saveValue:"web_app_invoke_custom_method"})}async invokeCustomMethod(t,e,o={}){const{result:r,error:n}=await a.request("web_app_invoke_custom_method",{method:t,params:e,req_id:this.createRequestId()},"custom_method_invoked",{...o,postEvent:this.postEvent});if(n)throw new Error(typeof n=="string"?n:`Unknown error: ${JSON.stringify(n)}`);return r}async deleteKeys(t,e){t.length!==0&&await this.invokeCustomMethod("deleteStorageValues",{keys:t},e)}async getKeys(t){const e=await this.invokeCustomMethod("getStorageKeys",{},t);return gt.parse(e)}async getValues(t,e){if(t.length===0)return z(t,"");const o=P.json(z(t,P.string())),r=await this.invokeCustomMethod("getStorageValues",{keys:t},e);return o.parse(r)}async saveValue(t,e,o){await this.invokeCustomMethod("saveStorageValue",{key:t,value:e},o)}}class Q{constructor(t,e=a.postEvent){i(this,"supports");this.postEvent=e,this.supports=C(t,{impactOccurred:"web_app_trigger_haptic_feedback",notificationOccurred:"web_app_trigger_haptic_feedback",selectionChanged:"web_app_trigger_haptic_feedback"})}impactOccurred(t){this.postEvent("web_app_trigger_haptic_feedback",{type:"impact",impact_style:t})}notificationOccurred(t){this.postEvent("web_app_trigger_haptic_feedback",{type:"notification",notification_type:t})}selectionChanged(){this.postEvent("web_app_trigger_haptic_feedback",{type:"selection_change"})}}class G{constructor(t,e,o={}){i(this,"state");const{chat:r=null,canSendAfter:n=null,chatType:c=null,chatInstance:h=null,user:p=null,queryId:l=null,receiver:u=null,startParam:d=null}=o;this.state=new _({authDate:t,canSendAfter:n===null?null:new Date(t.getTime()+n*1e3),chat:r,chatType:c,chatInstance:h,user:p,queryId:l,receiver:u,startParam:d,hash:e})}get authDate(){return this.state.get("authDate")}get canSendAfter(){return this.state.get("canSendAfter")}get chat(){return this.state.get("chat")}get chatType(){return this.state.get("chatType")}get chatInstance(){return this.state.get("chatInstance")}get hash(){return this.state.get("hash")}get queryId(){return this.state.get("queryId")}get receiver(){return this.state.get("receiver")}get startParam(){return this.state.get("startParam")}get user(){return this.state.get("user")}}class Z{constructor(t,e,o,r,n,c,h=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",(t,e)=>t==="click"?a.on("main_button_pressed",e):this.ee.on(t,e));i(this,"off",(t,e)=>{if(t==="click")return a.off("main_button_pressed",e);this.ee.off(t,e)});this.postEvent=h,this.state=new _({backgroundColor:t,isEnabled:e,isVisible:o,isProgressVisible:r,text:n,textColor:c},this.ee)}set isEnabled(t){this.state.set("isEnabled",t),this.commit()}get isEnabled(){return this.state.get("isEnabled")}set isProgressVisible(t){this.state.set("isProgressVisible",t),this.commit()}get isProgressVisible(){return this.state.get("isProgressVisible")}set isVisible(t){this.state.set("isVisible",t),this.commit()}get isVisible(){return this.state.get("isVisible")}commit(){this.text!==""&&this.postEvent("web_app_setup_main_button",{is_visible:this.isVisible,is_active:this.isEnabled,is_progress_visible:this.isProgressVisible,text:this.text,color:this.backgroundColor,text_color:this.textColor})}get backgroundColor(){return this.state.get("backgroundColor")}get text(){return this.state.get("text")}get textColor(){return this.state.get("textColor")}disable(){return this.isEnabled=!1,this}enable(){return this.isEnabled=!0,this}hide(){return this.isVisible=!1,this}hideProgress(){return this.isProgressVisible=!1,this}show(){return this.isVisible=!0,this}showProgress(){return this.isProgressVisible=!0,this}setText(t){return this.state.set("text",t),this.commit(),this}setTextColor(t){return this.state.set("textColor",t),this.commit(),this}setBackgroundColor(t){return this.state.set("backgroundColor",t),this.commit(),this}}function bt(s){const t=s.message.trim(),e=(s.title||"").trim(),o=s.buttons||[];let r;if(e.length>64)throw new Error(`Title has incorrect size: ${e.length}`);if(t.length===0||t.length>256)throw new Error(`Message has incorrect size: ${t.length}`);if(o.length>3)throw new Error(`Buttons have incorrect size: ${o.length}`);return o.length===0?r=[{type:"close",id:""}]:r=o.map(n=>{const{id:c=""}=n;if(c.length>64)throw new Error(`Button ID has incorrect size: ${c}`);if(n.type===void 0||n.type==="default"||n.type==="destructive"){const h=n.text.trim();if(h.length===0||h.length>64){const p=n.type||"default";throw new Error(`Button text with type "${p}" has incorrect size: ${n.text.length}`)}return{...n,text:h,id:c}}return{...n,id:c}}),{title:e,message:t,buttons:r}}class X{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");this.postEvent=e,this.state=new _({isOpened:!1},this.ee),this.supports=C(t,{open:"web_app_open_popup"})}get isOpened(){return this.state.get("isOpened")}async open(t){if(this.isOpened)throw new Error("Popup is already opened.");this.state.set("isOpened",!0);try{const{button_id:e=null}=await a.request("web_app_open_popup",bt(t),"popup_closed",{postEvent:this.postEvent});return e}finally{this.state.set("isOpened",!1)}}}class Y{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");this.postEvent=e,this.state=new _({isOpened:!1},this.ee),this.supports=C(t,{close:"web_app_close_scan_qr_popup",open:"web_app_open_scan_qr_popup"})}close(){this.postEvent("web_app_close_scan_qr_popup"),this.isOpened=!1}set isOpened(t){this.state.set("isOpened",t)}get isOpened(){return this.state.get("isOpened")}async open(t){if(this.isOpened)throw new Error("QR scanner is already opened.");this.isOpened=!0;try{const e=await a.request("web_app_open_scan_qr_popup",{text:t},["qr_text_received","scan_qr_popup_closed"],{postEvent:this.postEvent});return typeof e=="object"&&typeof e.data=="string"?e.data:null}finally{this.isOpened=!1}}}function U(s){const{backgroundColor:t=null,buttonTextColor:e=null,buttonColor:o=null,hintColor:r=null,linkColor:n=null,textColor:c=null,secondaryBackgroundColor:h=null}=s;return{backgroundColor:t,buttonTextColor:e,buttonColor:o,hintColor:r,linkColor:n,textColor:c,secondaryBackgroundColor:h}}class k{constructor(t){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.state=new _(U(t),this.ee)}static async request(t={}){const{timeout:e=1e3,...o}=t,r=await a.request("web_app_request_theme","theme_changed",{...o,timeout:e});return W.parse(r.theme_params)}static sync(t){a.on("theme_changed",e=>{t.state.set(U(W.parse(e.theme_params)))})}static async synced(t){const e=await this.request(t),o=new k(e);return this.sync(o),o}get backgroundColor(){return this.state.get("backgroundColor")}get buttonColor(){return this.state.get("buttonColor")}get buttonTextColor(){return this.state.get("buttonTextColor")}get hintColor(){return this.state.get("hintColor")}get isDark(){return this.backgroundColor===null||A.isColorDark(this.backgroundColor)}get linkColor(){return this.state.get("linkColor")}get secondaryBackgroundColor(){return this.state.get("secondaryBackgroundColor")}get textColor(){return this.state.get("textColor")}}function E(s){return s<0?0:s}class w{constructor(t,e,o,r,n=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=n,this.state=new _({height:E(t),isExpanded:r,stableHeight:E(o),width:E(e)},this.ee)}static async request(t={}){const{timeout:e=1e3,...o}=t,{is_expanded:r,is_state_stable:n,...c}=await a.request("web_app_request_viewport","viewport_changed",{...o,timeout:e});return{...c,isExpanded:r,isStateStable:n}}static sync(t){a.on("viewport_changed",e=>{const{height:o,width:r,is_expanded:n,is_state_stable:c}=e,h=E(o);t.state.set({height:h,isExpanded:n,width:E(r),stableHeight:c?h:void 0})})}static async synced(t={}){const{height:e,isExpanded:o,width:r}=await this.request(t),n=new w(e,r,e,o,t.postEvent);return this.sync(n),n}get height(){return this.state.get("height")}get stableHeight(){return this.state.get("stableHeight")}get isExpanded(){return this.state.get("isExpanded")}get width(){return this.state.get("width")}expand(){this.state.set("isExpanded",!0),this.postEvent("web_app_expand")}get isStable(){return this.stableHeight===this.height}}function V(s){const t=document.createElement("a");if(t.href=s,t.protocol!=="http:"&&t.protocol!=="https:")throw Error(`URL protocol is not supported by OS, or link has not allowed protocol: ${t.protocol}`);return t.href}class tt{constructor(t,e,o,r,n,c=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");i(this,"supportsParam");this.currentVersion=o,this.currentPlatform=r,this.createRequestId=n,this.postEvent=c,this.state=new _({backgroundColor:e,headerColor:t},this.ee),this.supports=C(o,{openInvoice:"web_app_open_invoice",readTextFromClipboard:"web_app_read_text_from_clipboard",setHeaderColor:"web_app_set_header_color",setBackgroundColor:"web_app_set_background_color",requestPhoneAccess:"web_app_request_phone",requestWriteAccess:"web_app_request_write_access"}),this.supportsParam=dt(o,{"setHeaderColor.color":["web_app_set_header_color","color"],"openLink.tryInstantView":["web_app_open_link","try_instant_view"]})}get backgroundColor(){return this.state.get("backgroundColor")}get colorScheme(){return A.isColorDark(this.backgroundColor)?"dark":"light"}close(){this.postEvent("web_app_close")}get headerColor(){return this.state.get("headerColor")}isVersionAtLeast(t){return R.compareVersions(t,this.version)>=0}openLink(t,e){const o=V(t);if(!a.supports("web_app_open_link",this.version)){window.open(o,"_blank");return}return this.postEvent("web_app_open_link",{url:o,...typeof e=="boolean"?{try_instant_view:e}:{}})}openTelegramLink(t){const{hostname:e,pathname:o,search:r}=new URL(V(t));if(e!=="t.me")throw new Error(`URL has not allowed hostname: ${e}. Only "t.me" is allowed`);if(!a.supports("web_app_open_tg_link",this.version)){window.location.href=t;return}return this.postEvent("web_app_open_tg_link",{path_full:o+r})}async openInvoice(t){const{hostname:e,pathname:o}=new URL(V(t));if(e!=="t.me")throw new Error(`Incorrect hostname: ${e}`);const r=o.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);if(r===null)throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/slug" or "/$slug"');const[,,n]=r;return(await a.request("web_app_open_invoice",{slug:n},"invoice_closed",{postEvent:this.postEvent,capture:({slug:h})=>n===h})).status}get platform(){return this.currentPlatform}ready(){this.postEvent("web_app_ready")}async readTextFromClipboard(){const{data:t=null}=await a.request("web_app_read_text_from_clipboard",{req_id:this.createRequestId()},"clipboard_text_received",{postEvent:this.postEvent});return t}async requestPhoneAccess(){const{status:t}=await a.request("web_app_request_phone","phone_requested",{postEvent:this.postEvent});return t}async requestWriteAccess(){const{status:t}=await a.request("web_app_request_write_access","write_access_requested",{postEvent:this.postEvent});return t}sendData(t){const{size:e}=new Blob([t]);if(e===0||e>4096)throw new Error(`Passed data has incorrect size: ${e}`);this.postEvent("web_app_data_send",{data:t})}setHeaderColor(t){this.postEvent("web_app_set_header_color",A.isRGB(t)?{color:t}:{color_key:t}),this.state.set("headerColor",t)}setBackgroundColor(t){this.postEvent("web_app_set_background_color",{color:t}),this.state.set("backgroundColor",t)}get version(){return this.currentVersion}}class q extends Error{constructor(t,e){super(`Method "${t}" is not supported in the Web Apps version ${e}.`),Object.setPrototypeOf(this,q.prototype)}}class B extends Error{constructor(t,e,o){super(`Parameter "${e}" in method "${t}" is not supported in the Web Apps version ${o}.`),Object.setPrototypeOf(this,B.prototype)}}function et(s,t){document.documentElement.style.setProperty(s,t)}function b(s,t){t!==null&&et(s,t)}function F(s,t){et(s,`${t}px`)}function _t(s){const{backgroundColor:t,buttonTextColor:e,secondaryBackgroundColor:o,hintColor:r,buttonColor:n,linkColor:c,textColor:h}=s;b("--tg-theme-bg-color",t),b("--tg-theme-button-color",n),b("--tg-theme-button-text-color",e),b("--tg-theme-hint-color",r),b("--tg-theme-link-color",c),b("--tg-theme-secondary-bg-color",o),b("--tg-theme-text-color",h)}function ft(s,t){const{backgroundColor:e,secondaryBackgroundColor:o}=t,{backgroundColor:r,headerColor:n}=s;b("--tg-bg-color",r),b("--tg-header-color",n==="bg_color"?e:o)}function mt(s){const t=()=>_t(s);s.on("changed",t),t()}function wt(s,t){const e=()=>ft(s,t);t.on("changed",e),s.on("backgroundColorChanged",e),s.on("headerColorChanged",e),e()}function Ct(s){const t=()=>{F("--tg-viewport-height",s.height)},e=()=>{F("--tg-viewport-stable-height",s.stableHeight)};s.on("heightChanged",t),s.on("stableHeightChanged",e),t(),e()}function vt(s){return typeof s=="boolean"?s?{themeParams:!0,viewport:!0,webApp:!0}:{}:s}function st(s){return`telegram-mini-apps-${s}`}function S(s,t){sessionStorage.setItem(st(s),JSON.stringify(t))}function x(s){const t=sessionStorage.getItem(st(s));return t?JSON.parse(t):null}function Et(s,t,e){const{isVisible:o=!1}=s?x("back-button")||{}:{},r=new K(o,t,e);return r.on("isVisibleChanged",()=>{S("back-button",{isVisible:r.isVisible})}),r}function yt(s,t){const{isConfirmationNeeded:e=!1}=s?x("closing-behavior")||{}:{},o=new j(e,t);return o.on("isConfirmationNeededChanged",()=>S("closing-behavior",{isConfirmationNeeded:o.isConfirmationNeeded})),o}function kt(s,t,e,o){const{backgroundColor:r=t,isEnabled:n=!1,isVisible:c=!1,isProgressVisible:h=!1,textColor:p=e,text:l=""}=s?x("main-button")||{}:{},u=new Z(r,n,c,h,l,p,o),d=()=>S("main-button",{backgroundColor:u.backgroundColor,isEnabled:u.isEnabled,isVisible:u.isVisible,isProgressVisible:u.isProgressVisible,text:u.text,textColor:u.textColor});return u.on("backgroundColorChanged",d),u.on("isEnabledChanged",d),u.on("isVisibleChanged",d),u.on("isProgressVisibleChanged",d),u.on("textColorChanged",d),u.on("textChanged",d),u}function St(s,t){return s?(e,o)=>{if(!a.supports(e,t))throw new q(e,t);return R.isRecord(o)&&a.detectSupportParams(e,o).forEach(r=>{if(!a.supports(e,r,t))throw new B(e,r,t)}),a.postEvent(e,o)}:a.postEvent}function xt(){let s=0;return()=>(s+=1,s.toString())}function Vt(s){const t=new k(s);return k.sync(t),t}async function Pt(s,t,e){const{height:o=window.innerHeight,stableHeight:r=window.innerHeight,width:n=window.innerWidth,isExpanded:c=!1}=s?x("viewport")||{}:{},p=t==="macos"||t==="web"?(()=>{const u=new w(o,n,r,c,e);return w.sync(u),u})():await w.synced({postEvent:e}),l=()=>S("viewport",{height:p.height,isExpanded:p.isExpanded,stableHeight:p.stableHeight,width:p.width});return p.on("heightChanged",l),p.on("isExpandedChanged",l),p.on("stableHeightChanged",l),p.on("widthChanged",l),p}function qt(s,t,e,o,r,n){const{backgroundColor:c=t,headerColor:h="bg_color"}=s?x("web-app")||{}:{},p=new tt(h,c,e,o,r,n),l=()=>S("web-app",{backgroundColor:p.backgroundColor,headerColor:p.headerColor});return p.on("backgroundColorChanged",l),p.on("headerColorChanged",l),p}function $(){let s;try{return y.parse(window.location.hash.slice(1))}catch(e){s=e}const t=y.retrieveFromStorage();if(t)return t;throw new Error("Unable to extract launch params",{cause:s})}function Bt(){return window.performance.getEntriesByType("navigation").some(s=>s.type==="reload")}function Ot(s,t){var e,o;return s?((e=s.initData)==null?void 0:e.hash)===((o=t.initData)==null?void 0:o.hash):!1}async function Tt(s={}){const{checkCompat:t=!0,cssVars:e=!1,acceptScrollbarStyle:o=!0,acceptCustomStyles:r=o,targetOrigin:n,debug:c=!1,launchParams:h}=s;c&&a.setDebug(c),typeof n=="string"&&a.setTargetOrigin(n);const p=y.retrieveFromStorage(),l=h instanceof URLSearchParams||typeof h=="string"?y.parse(h):$();y.saveToStorage(l);const u=Bt()||Ot(p,l),{initData:d,initDataRaw:ot,version:m,platform:H,themeParams:D}=l,{backgroundColor:rt="#ffffff",buttonColor:nt="#000000",buttonTextColor:it="#ffffff"}=D,L=xt(),g=St(t,m),O=Vt(D),N=qt(u,rt,m,H,L,g),{themeParams:at,viewport:ct,webApp:ht}=vt(e);ht&&wt(N,O),at&&mt(O);const M=await Pt(u,H,g);if(ct&&Ct(M),r&&a.isIframe()){const v=document.createElement("style");v.id="telegram-custom-styles",document.head.appendChild(v),a.on("set_custom_style",I=>{v.innerHTML=I}),g("iframe_ready")}const T={backButton:Et(u,m,g),closingBehavior:yt(u,g),cloudStorage:new J(m,L,g),haptic:new Q(m,g),mainButton:kt(u,nt,it,g),popup:new X(m,g),postEvent:g,qrScanner:new Y(m,g),themeParams:O,viewport:M,webApp:N};if(d!==void 0){const{authDate:v,hash:I,...ut}=d;T.initData=new G(v,I,ut),T.initDataRaw=ot}return T}function It(s={}){return R.withTimeout(Tt(s),s.timeout||1e3)}function At(){try{return $(),!0}catch{return!1}}exports.BackButton=K;exports.ClosingBehaviour=j;exports.CloudStorage=J;exports.HapticFeedback=Q;exports.InitData=G;exports.MainButton=Z;exports.MethodNotSupportedError=q;exports.ParameterUnsupportedError=B;exports.Popup=X;exports.QRScanner=Y;exports.ThemeParams=k;exports.Viewport=w;exports.WebApp=tt;exports.formatURL=V;exports.init=It;exports.isTWA=At;exports.retrieveLaunchParams=$;
1
+ "use strict";var pt=Object.defineProperty;var lt=(s,t,e)=>t in s?pt(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var i=(s,t,e)=>(lt(s,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("@tma.js/launch-params"),f=require("@tma.js/event-emitter"),a=require("@tma.js/bridge"),P=require("@tma.js/parsing"),z=require("@tma.js/theme-params"),A=require("@tma.js/colors"),R=require("@tma.js/utils");function C(s,t){return e=>a.supports(t[e],s)}function dt(s,t){return e=>{const[o,r]=t[e];return a.supports(o,r,s)}}class _{constructor(t,e){this.state=t,this.ee=e}emit(t,e){this.ee&&this.ee.emit(t,e)}internalSet(t,e){return this.state[t]===e?!1:(this.state[t]=e,this.emit(`${t}Changed`,e),!0)}set(t,e){let o=!1;if(typeof t=="string")o=this.internalSet(t,e);else for(const r in t)this.internalSet(r,t[r])&&(o=!0);o&&this.emit("changed")}get(t){return this.state[t]}}class K{constructor(t,e,o=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",(t,e)=>t==="click"?a.on("back_button_pressed",e):this.ee.on(t,e));i(this,"off",(t,e)=>{if(t==="click")return a.off("back_button_pressed",e);this.ee.off(t,e)});i(this,"supports");this.postEvent=o,this.state=new _({isVisible:t},this.ee),this.supports=C(e,{show:"web_app_setup_back_button",hide:"web_app_setup_back_button"})}set isVisible(t){this.state.set("isVisible",t),this.postEvent("web_app_setup_back_button",{is_visible:t})}get isVisible(){return this.state.get("isVisible")}hide(){this.isVisible=!1}show(){this.isVisible=!0}}class j{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=e,this.state=new _({isConfirmationNeeded:t},this.ee)}set isConfirmationNeeded(t){this.state.set("isConfirmationNeeded",t),this.postEvent("web_app_setup_closing_behavior",{need_confirmation:t})}get isConfirmationNeeded(){return this.state.get("isConfirmationNeeded")}disableConfirmation(){this.isConfirmationNeeded=!1}enableConfirmation(){this.isConfirmationNeeded=!0}}const gt=P.array().of(P.string());function U(s,t){return s.reduce((e,o)=>(e[o]=t,e),{})}class J{constructor(t,e,o=a.postEvent){i(this,"supports");this.createRequestId=e,this.postEvent=o,this.supports=C(t,{deleteKeys:"web_app_invoke_custom_method",getKeys:"web_app_invoke_custom_method",getValues:"web_app_invoke_custom_method",saveValue:"web_app_invoke_custom_method"})}async invokeCustomMethod(t,e,o={}){const{result:r,error:n}=await a.request("web_app_invoke_custom_method",{method:t,params:e,req_id:this.createRequestId()},"custom_method_invoked",{...o,postEvent:this.postEvent});if(n)throw new Error(typeof n=="string"?n:`Unknown error: ${JSON.stringify(n)}`);return r}async deleteKeys(t,e){t.length!==0&&await this.invokeCustomMethod("deleteStorageValues",{keys:t},e)}async getKeys(t){const e=await this.invokeCustomMethod("getStorageKeys",{},t);return gt.parse(e)}async getValues(t,e){if(t.length===0)return U(t,"");const o=P.json(U(t,P.string())),r=await this.invokeCustomMethod("getStorageValues",{keys:t},e);return o.parse(r)}async saveValue(t,e,o){await this.invokeCustomMethod("saveStorageValue",{key:t,value:e},o)}}class Q{constructor(t,e=a.postEvent){i(this,"supports");this.postEvent=e,this.supports=C(t,{impactOccurred:"web_app_trigger_haptic_feedback",notificationOccurred:"web_app_trigger_haptic_feedback",selectionChanged:"web_app_trigger_haptic_feedback"})}impactOccurred(t){this.postEvent("web_app_trigger_haptic_feedback",{type:"impact",impact_style:t})}notificationOccurred(t){this.postEvent("web_app_trigger_haptic_feedback",{type:"notification",notification_type:t})}selectionChanged(){this.postEvent("web_app_trigger_haptic_feedback",{type:"selection_change"})}}class G{constructor(t,e,o={}){i(this,"state");const{chat:r=null,canSendAfter:n=null,chatType:c=null,chatInstance:h=null,user:p=null,queryId:l=null,receiver:u=null,startParam:d=null}=o;this.state=new _({authDate:t,canSendAfter:n===null?null:new Date(t.getTime()+n*1e3),chat:r,chatType:c,chatInstance:h,user:p,queryId:l,receiver:u,startParam:d,hash:e})}get authDate(){return this.state.get("authDate")}get canSendAfter(){return this.state.get("canSendAfter")}get chat(){return this.state.get("chat")}get chatType(){return this.state.get("chatType")}get chatInstance(){return this.state.get("chatInstance")}get hash(){return this.state.get("hash")}get queryId(){return this.state.get("queryId")}get receiver(){return this.state.get("receiver")}get startParam(){return this.state.get("startParam")}get user(){return this.state.get("user")}}class Z{constructor(t,e,o,r,n,c,h=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",(t,e)=>t==="click"?a.on("main_button_pressed",e):this.ee.on(t,e));i(this,"off",(t,e)=>{if(t==="click")return a.off("main_button_pressed",e);this.ee.off(t,e)});this.postEvent=h,this.state=new _({backgroundColor:t,isEnabled:e,isVisible:o,isProgressVisible:r,text:n,textColor:c},this.ee)}set isEnabled(t){this.state.set("isEnabled",t),this.commit()}get isEnabled(){return this.state.get("isEnabled")}set isProgressVisible(t){this.state.set("isProgressVisible",t),this.commit()}get isProgressVisible(){return this.state.get("isProgressVisible")}set isVisible(t){this.state.set("isVisible",t),this.commit()}get isVisible(){return this.state.get("isVisible")}commit(){this.text!==""&&this.postEvent("web_app_setup_main_button",{is_visible:this.isVisible,is_active:this.isEnabled,is_progress_visible:this.isProgressVisible,text:this.text,color:this.backgroundColor,text_color:this.textColor})}get backgroundColor(){return this.state.get("backgroundColor")}get text(){return this.state.get("text")}get textColor(){return this.state.get("textColor")}disable(){return this.isEnabled=!1,this}enable(){return this.isEnabled=!0,this}hide(){return this.isVisible=!1,this}hideProgress(){return this.isProgressVisible=!1,this}show(){return this.isVisible=!0,this}showProgress(){return this.isProgressVisible=!0,this}setText(t){return this.state.set("text",t),this.commit(),this}setTextColor(t){return this.state.set("textColor",t),this.commit(),this}setBackgroundColor(t){return this.state.set("backgroundColor",t),this.commit(),this}}function bt(s){const t=s.message.trim(),e=(s.title||"").trim(),o=s.buttons||[];let r;if(e.length>64)throw new Error(`Title has incorrect size: ${e.length}`);if(t.length===0||t.length>256)throw new Error(`Message has incorrect size: ${t.length}`);if(o.length>3)throw new Error(`Buttons have incorrect size: ${o.length}`);return o.length===0?r=[{type:"close",id:""}]:r=o.map(n=>{const{id:c=""}=n;if(c.length>64)throw new Error(`Button ID has incorrect size: ${c}`);if(n.type===void 0||n.type==="default"||n.type==="destructive"){const h=n.text.trim();if(h.length===0||h.length>64){const p=n.type||"default";throw new Error(`Button text with type "${p}" has incorrect size: ${n.text.length}`)}return{...n,text:h,id:c}}return{...n,id:c}}),{title:e,message:t,buttons:r}}class X{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");this.postEvent=e,this.state=new _({isOpened:!1},this.ee),this.supports=C(t,{open:"web_app_open_popup"})}get isOpened(){return this.state.get("isOpened")}async open(t){if(this.isOpened)throw new Error("Popup is already opened.");this.state.set("isOpened",!0);try{const{button_id:e=null}=await a.request("web_app_open_popup",bt(t),"popup_closed",{postEvent:this.postEvent});return e}finally{this.state.set("isOpened",!1)}}}class Y{constructor(t,e=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");this.postEvent=e,this.state=new _({isOpened:!1},this.ee),this.supports=C(t,{close:"web_app_close_scan_qr_popup",open:"web_app_open_scan_qr_popup"})}close(){this.postEvent("web_app_close_scan_qr_popup"),this.isOpened=!1}set isOpened(t){this.state.set("isOpened",t)}get isOpened(){return this.state.get("isOpened")}async open(t){if(this.isOpened)throw new Error("QR scanner is already opened.");this.isOpened=!0;try{const e=await a.request("web_app_open_scan_qr_popup",{text:t},["qr_text_received","scan_qr_popup_closed"],{postEvent:this.postEvent});return typeof e=="object"&&typeof e.data=="string"?e.data:null}finally{this.isOpened=!1}}}function W(s){const{backgroundColor:t=null,buttonTextColor:e=null,buttonColor:o=null,hintColor:r=null,linkColor:n=null,textColor:c=null,secondaryBackgroundColor:h=null}=s;return{backgroundColor:t,buttonTextColor:e,buttonColor:o,hintColor:r,linkColor:n,textColor:c,secondaryBackgroundColor:h}}class k{constructor(t){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.state=new _(W(t),this.ee)}static async request(t={}){const{timeout:e=1e3,...o}=t,r=await a.request("web_app_request_theme","theme_changed",{...o,timeout:e});return z.parse(r.theme_params)}static sync(t){a.on("theme_changed",e=>{t.state.set(W(z.parse(e.theme_params)))})}static async synced(t){const e=await this.request(t),o=new k(e);return this.sync(o),o}get backgroundColor(){return this.state.get("backgroundColor")}get buttonColor(){return this.state.get("buttonColor")}get buttonTextColor(){return this.state.get("buttonTextColor")}get hintColor(){return this.state.get("hintColor")}get isDark(){return this.backgroundColor===null||A.isColorDark(this.backgroundColor)}get linkColor(){return this.state.get("linkColor")}get secondaryBackgroundColor(){return this.state.get("secondaryBackgroundColor")}get textColor(){return this.state.get("textColor")}}function E(s){return s<0?0:s}class w{constructor(t,e,o,r,n=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=n,this.state=new _({height:E(t),isExpanded:r,stableHeight:E(o),width:E(e)},this.ee)}static async request(t={}){const{timeout:e=1e3,...o}=t,{is_expanded:r,is_state_stable:n,...c}=await a.request("web_app_request_viewport","viewport_changed",{...o,timeout:e});return{...c,isExpanded:r,isStateStable:n}}static sync(t){a.on("viewport_changed",e=>{const{height:o,width:r,is_expanded:n,is_state_stable:c}=e,h=E(o);t.state.set({height:h,isExpanded:n,width:E(r),stableHeight:c?h:void 0})})}static async synced(t={}){const{height:e,isExpanded:o,width:r}=await this.request(t),n=new w(e,r,e,o,t.postEvent);return this.sync(n),n}get height(){return this.state.get("height")}get stableHeight(){return this.state.get("stableHeight")}get isExpanded(){return this.state.get("isExpanded")}get width(){return this.state.get("width")}expand(){this.state.set("isExpanded",!0),this.postEvent("web_app_expand")}get isStable(){return this.stableHeight===this.height}}function V(s){const t=document.createElement("a");if(t.href=s,t.protocol!=="http:"&&t.protocol!=="https:")throw Error(`URL protocol is not supported by OS, or link has not allowed protocol: ${t.protocol}`);return t.href}class tt{constructor(t,e,o,r,n,c=a.postEvent){i(this,"ee",new f.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));i(this,"supports");i(this,"supportsParam");this.currentVersion=o,this.currentPlatform=r,this.createRequestId=n,this.postEvent=c,this.state=new _({backgroundColor:e,headerColor:t},this.ee),this.supports=C(o,{openInvoice:"web_app_open_invoice",readTextFromClipboard:"web_app_read_text_from_clipboard",setHeaderColor:"web_app_set_header_color",setBackgroundColor:"web_app_set_background_color",requestPhoneAccess:"web_app_request_phone",requestWriteAccess:"web_app_request_write_access"}),this.supportsParam=dt(o,{"setHeaderColor.color":["web_app_set_header_color","color"],"openLink.tryInstantView":["web_app_open_link","try_instant_view"]})}get backgroundColor(){return this.state.get("backgroundColor")}get colorScheme(){return A.isColorDark(this.backgroundColor)?"dark":"light"}close(){this.postEvent("web_app_close")}get headerColor(){return this.state.get("headerColor")}isVersionAtLeast(t){return R.compareVersions(t,this.version)>=0}openLink(t,e){const o=V(t);if(!a.supports("web_app_open_link",this.version)){window.open(o,"_blank");return}return this.postEvent("web_app_open_link",{url:o,...typeof e=="boolean"?{try_instant_view:e}:{}})}openTelegramLink(t){const{hostname:e,pathname:o,search:r}=new URL(V(t));if(e!=="t.me")throw new Error(`URL has not allowed hostname: ${e}. Only "t.me" is allowed`);if(!a.supports("web_app_open_tg_link",this.version)){window.location.href=t;return}return this.postEvent("web_app_open_tg_link",{path_full:o+r})}async openInvoice(t){const{hostname:e,pathname:o}=new URL(V(t));if(e!=="t.me")throw new Error(`Incorrect hostname: ${e}`);const r=o.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);if(r===null)throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/slug" or "/$slug"');const[,,n]=r;return(await a.request("web_app_open_invoice",{slug:n},"invoice_closed",{postEvent:this.postEvent,capture:({slug:h})=>n===h})).status}get platform(){return this.currentPlatform}ready(){this.postEvent("web_app_ready")}async readTextFromClipboard(){const{data:t=null}=await a.request("web_app_read_text_from_clipboard",{req_id:this.createRequestId()},"clipboard_text_received",{postEvent:this.postEvent});return t}async requestPhoneAccess(){const{status:t}=await a.request("web_app_request_phone","phone_requested",{postEvent:this.postEvent});return t}async requestWriteAccess(){const{status:t}=await a.request("web_app_request_write_access","write_access_requested",{postEvent:this.postEvent});return t}sendData(t){const{size:e}=new Blob([t]);if(e===0||e>4096)throw new Error(`Passed data has incorrect size: ${e}`);this.postEvent("web_app_data_send",{data:t})}setHeaderColor(t){this.postEvent("web_app_set_header_color",A.isRGB(t)?{color:t}:{color_key:t}),this.state.set("headerColor",t)}setBackgroundColor(t){this.postEvent("web_app_set_background_color",{color:t}),this.state.set("backgroundColor",t)}get version(){return this.currentVersion}}class q extends Error{constructor(t,e){super(`Method "${t}" is not supported in the Mini Apps version ${e}.`),Object.setPrototypeOf(this,q.prototype)}}class B extends Error{constructor(t,e,o){super(`Parameter "${e}" in method "${t}" is not supported in the Mini Apps version ${o}.`),Object.setPrototypeOf(this,B.prototype)}}function et(s,t){document.documentElement.style.setProperty(s,t)}function b(s,t){t!==null&&et(s,t)}function F(s,t){et(s,`${t}px`)}function _t(s){const{backgroundColor:t,buttonTextColor:e,secondaryBackgroundColor:o,hintColor:r,buttonColor:n,linkColor:c,textColor:h}=s;b("--tg-theme-bg-color",t),b("--tg-theme-button-color",n),b("--tg-theme-button-text-color",e),b("--tg-theme-hint-color",r),b("--tg-theme-link-color",c),b("--tg-theme-secondary-bg-color",o),b("--tg-theme-text-color",h)}function ft(s,t){const{backgroundColor:e,secondaryBackgroundColor:o}=t,{backgroundColor:r,headerColor:n}=s;b("--tg-bg-color",r),b("--tg-header-color",n==="bg_color"?e:o)}function mt(s){const t=()=>_t(s);s.on("changed",t),t()}function wt(s,t){const e=()=>ft(s,t);t.on("changed",e),s.on("backgroundColorChanged",e),s.on("headerColorChanged",e),e()}function Ct(s){const t=()=>{F("--tg-viewport-height",s.height)},e=()=>{F("--tg-viewport-stable-height",s.stableHeight)};s.on("heightChanged",t),s.on("stableHeightChanged",e),t(),e()}function vt(s){return typeof s=="boolean"?s?{themeParams:!0,viewport:!0,webApp:!0}:{}:s}function st(s){return`telegram-mini-apps-${s}`}function S(s,t){sessionStorage.setItem(st(s),JSON.stringify(t))}function x(s){const t=sessionStorage.getItem(st(s));return t?JSON.parse(t):null}function Et(s,t,e){const{isVisible:o=!1}=s?x("back-button")||{}:{},r=new K(o,t,e);return r.on("isVisibleChanged",()=>{S("back-button",{isVisible:r.isVisible})}),r}function yt(s,t){const{isConfirmationNeeded:e=!1}=s?x("closing-behavior")||{}:{},o=new j(e,t);return o.on("isConfirmationNeededChanged",()=>S("closing-behavior",{isConfirmationNeeded:o.isConfirmationNeeded})),o}function kt(s,t,e,o){const{backgroundColor:r=t,isEnabled:n=!1,isVisible:c=!1,isProgressVisible:h=!1,textColor:p=e,text:l=""}=s?x("main-button")||{}:{},u=new Z(r,n,c,h,l,p,o),d=()=>S("main-button",{backgroundColor:u.backgroundColor,isEnabled:u.isEnabled,isVisible:u.isVisible,isProgressVisible:u.isProgressVisible,text:u.text,textColor:u.textColor});return u.on("backgroundColorChanged",d),u.on("isEnabledChanged",d),u.on("isVisibleChanged",d),u.on("isProgressVisibleChanged",d),u.on("textColorChanged",d),u.on("textChanged",d),u}function St(s,t){return s?(e,o)=>{if(!a.supports(e,t))throw new q(e,t);return R.isRecord(o)&&a.detectSupportParams(e,o).forEach(r=>{if(!a.supports(e,r,t))throw new B(e,r,t)}),a.postEvent(e,o)}:a.postEvent}function xt(){let s=0;return()=>(s+=1,s.toString())}function Vt(s){const t=new k(s);return k.sync(t),t}async function Pt(s,t,e){const{height:o=window.innerHeight,stableHeight:r=window.innerHeight,width:n=window.innerWidth,isExpanded:c=!1}=s?x("viewport")||{}:{},p=t==="macos"||t==="web"?(()=>{const u=new w(o,n,r,c,e);return w.sync(u),u})():await w.synced({postEvent:e}),l=()=>S("viewport",{height:p.height,isExpanded:p.isExpanded,stableHeight:p.stableHeight,width:p.width});return p.on("heightChanged",l),p.on("isExpandedChanged",l),p.on("stableHeightChanged",l),p.on("widthChanged",l),p}function qt(s,t,e,o,r,n){const{backgroundColor:c=t,headerColor:h="bg_color"}=s?x("web-app")||{}:{},p=new tt(h,c,e,o,r,n),l=()=>S("web-app",{backgroundColor:p.backgroundColor,headerColor:p.headerColor});return p.on("backgroundColorChanged",l),p.on("headerColorChanged",l),p}function $(){let s;try{return y.parse(window.location.hash.slice(1))}catch(e){s=e}const t=y.retrieveFromStorage();if(t)return t;throw new Error("Unable to extract launch params",{cause:s})}function Bt(){return window.performance.getEntriesByType("navigation").some(s=>s.type==="reload")}function Ot(s,t){var e,o;return s?((e=s.initData)==null?void 0:e.hash)===((o=t.initData)==null?void 0:o.hash):!1}async function Tt(s={}){const{checkCompat:t=!0,cssVars:e=!1,acceptScrollbarStyle:o=!0,acceptCustomStyles:r=o,targetOrigin:n,debug:c=!1,launchParams:h}=s;c&&a.setDebug(c),typeof n=="string"&&a.setTargetOrigin(n);const p=y.retrieveFromStorage(),l=h instanceof URLSearchParams||typeof h=="string"?y.parse(h):$();y.saveToStorage(l);const u=Bt()||Ot(p,l),{initData:d,initDataRaw:ot,version:m,platform:H,themeParams:D}=l,{backgroundColor:rt="#ffffff",buttonColor:nt="#000000",buttonTextColor:it="#ffffff"}=D,L=xt(),g=St(t,m),O=Vt(D),M=qt(u,rt,m,H,L,g),{themeParams:at,viewport:ct,webApp:ht}=vt(e);ht&&wt(M,O),at&&mt(O);const N=await Pt(u,H,g);if(ct&&Ct(N),r&&a.isIframe()){const v=document.createElement("style");v.id="telegram-custom-styles",document.head.appendChild(v),a.on("set_custom_style",I=>{v.innerHTML=I}),g("iframe_ready")}const T={backButton:Et(u,m,g),closingBehavior:yt(u,g),cloudStorage:new J(m,L,g),haptic:new Q(m,g),mainButton:kt(u,nt,it,g),popup:new X(m,g),postEvent:g,qrScanner:new Y(m,g),themeParams:O,viewport:N,webApp:M};if(d!==void 0){const{authDate:v,hash:I,...ut}=d;T.initData=new G(v,I,ut),T.initDataRaw=ot}return T}function It(s={}){return R.withTimeout(Tt(s),s.timeout||1e3)}function At(){try{return $(),!0}catch{return!1}}exports.BackButton=K;exports.ClosingBehaviour=j;exports.CloudStorage=J;exports.HapticFeedback=Q;exports.InitData=G;exports.MainButton=Z;exports.MethodNotSupportedError=q;exports.ParameterUnsupportedError=B;exports.Popup=X;exports.QRScanner=Y;exports.ThemeParams=k;exports.Viewport=w;exports.WebApp=tt;exports.formatURL=V;exports.init=It;exports.isTWA=At;exports.retrieveLaunchParams=$;
2
2
  //# sourceMappingURL=index.cjs.map