@tma.js/sdk 0.12.2 → 0.12.3

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.umd.js CHANGED
@@ -1,2 +1,2 @@
1
- (function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@tma.js/launch-params"),require("@tma.js/event-emitter"),require("@tma.js/bridge"),require("@tma.js/parsing"),require("@tma.js/theme-params"),require("@tma.js/colors"),require("@tma.js/utils")):typeof define=="function"&&define.amd?define(["exports","@tma.js/launch-params","@tma.js/event-emitter","@tma.js/bridge","@tma.js/parsing","@tma.js/theme-params","@tma.js/colors","@tma.js/utils"],d):(a=typeof globalThis<"u"?globalThis:a||self,d((a.tmajs=a.tmajs||{},a.tmajs.sdk={}),a.tmajs["launch-params"],a.tmajs["event-emitter"],a.tmajs.bridge,a.tmajs.parsing,a.tmajs["theme-params"],a.tmajs.colors,a.tmajs.utils))})(this,function(a,d,_,c,P,H,O,T){"use strict";var At=Object.defineProperty;var It=(a,d,_)=>d in a?At(a,d,{enumerable:!0,configurable:!0,writable:!0,value:_}):a[d]=_;var i=(a,d,_)=>(It(a,typeof d!="symbol"?d+"":d,_),_);function v(s,t){return e=>c.supports(t[e],s)}function ot(s,t){return e=>{const[n,o]=t[e];return c.supports(n,o,s)}}class b{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 n=!1;if(typeof t=="string")n=this.internalSet(t,e);else for(const o in t)this.internalSet(o,t[o])&&(n=!0);n&&this.emit("changed")}get(t){return this.state[t]}}class D{constructor(t,e,n=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",(t,e)=>{if(t==="click")return c.on("back_button_pressed",e);this.ee.on(t,e)});i(this,"off",(t,e)=>{if(t==="click")return c.off("back_button_pressed",e);this.ee.off(t,e)});i(this,"supports");this.postEvent=n,this.state=new b({isVisible:t},this.ee),this.supports=v(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 L{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({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 rt=P.array().of(P.string());function N(s,t){return s.reduce((e,n)=>(e[n]=t,e),{})}class M{constructor(t,e,n=c.postEvent){i(this,"supports");this.createRequestId=e,this.postEvent=n,this.supports=v(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,n={}){const{result:o,error:r}=await c.request("web_app_invoke_custom_method",{method:t,params:e,req_id:this.createRequestId()},"custom_method_invoked",{...n,postEvent:this.postEvent});if(r)throw new Error(typeof r=="string"?r:`Unknown error: ${JSON.stringify(r)}`);return o}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 rt.parse(e)}async getValues(t,e){if(t.length===0)return N(t,"");const n=P.json(N(t,P.string())),o=await this.invokeCustomMethod("getStorageValues",{keys:t},e);return n.parse(o)}async saveValue(t,e,n){await this.invokeCustomMethod("saveStorageValue",{key:t,value:e},n)}}class W{constructor(t,e=c.postEvent){i(this,"supports");this.postEvent=e,this.supports=v(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 z{constructor(t,e,n={}){i(this,"state");const{chat:o=null,canSendAfter:r=null,chatType:h=null,chatInstance:u=null,user:l=null,queryId:g=null,receiver:p=null,startParam:f=null}=n;this.state=new b({authDate:t,canSendAfter:r===null?null:new Date(t.getTime()+r*1e3),chat:o,chatType:h,chatInstance:u,user:l,queryId:g,receiver:p,startParam:f,hash:e})}get authDate(){return this.state.get("authDate")}get canSendAfter(){return this.state.get("canSendAfter")}get chat(){return this.state.get("chat")}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 U{constructor(t,e,n,o,r,h,u=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",(t,e)=>{if(t==="click")return c.on("main_button_pressed",e);this.ee.on(t,e)});i(this,"off",(t,e)=>{if(t==="click")return c.off("main_button_pressed",e);this.ee.off(t,e)});this.postEvent=u,this.state=new b({backgroundColor:t,isEnabled:e,isVisible:n,isProgressVisible:o,text:r,textColor:h},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 it(s){const t=s.message.trim(),e=(s.title||"").trim(),n=s.buttons||[];let o;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(n.length>3)throw new Error(`Buttons have incorrect size: ${n.length}`);return n.length===0?o=[{type:"close",id:""}]:o=n.map(r=>{const{id:h=""}=r;if(h.length>64)throw new Error(`Button ID has incorrect size: ${h}`);if(r.type===void 0||r.type==="default"||r.type==="destructive"){const u=r.text.trim();if(u.length===0||u.length>64){const l=r.type||"default";throw new Error(`Button text with type "${l}" has incorrect size: ${r.text.length}`)}return{...r,text:u,id:h}}return{...r,id:h}}),{title:e,message:t,buttons:o}}class F{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({isOpened:!1},this.ee),this.supports=v(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 c.request("web_app_open_popup",it(t),"popup_closed",{postEvent:this.postEvent});return e}finally{this.state.set("isOpened",!1)}}}class K{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({isOpened:!1},this.ee),this.supports=v(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 c.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 J(s){const{backgroundColor:t=null,buttonTextColor:e=null,buttonColor:n=null,hintColor:o=null,linkColor:r=null,textColor:h=null,secondaryBackgroundColor:u=null}=s;return{backgroundColor:t,buttonTextColor:e,buttonColor:n,hintColor:o,linkColor:r,textColor:h,secondaryBackgroundColor:u}}class y{constructor(t){i(this,"ee",new _.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 b(J(t),this.ee)}static async request(t={}){const{timeout:e=1e3,...n}=t,o=await c.request("web_app_request_theme","theme_changed",{...n,timeout:e});return H.parse(o.theme_params)}static sync(t){c.on("theme_changed",e=>{t.state.set(J(H.parse(e.theme_params)))})}static async synced(t){const e=await this.request(t),n=new y(e);return this.sync(n),n}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||O.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 k(s){return s<0?0:s}class E{constructor(t,e,n,o,r=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=r,this.state=new b({height:k(t),isExpanded:o,stableHeight:k(n),width:k(e)},this.ee)}static async request(t={}){const{timeout:e=1e3,...n}=t,{is_expanded:o,is_state_stable:r,...h}=await c.request("web_app_request_viewport","viewport_changed",{...n,timeout:e});return{...h,isExpanded:o,isStateStable:r}}static sync(t){c.on("viewport_changed",e=>{const{height:n,width:o,is_expanded:r,is_state_stable:h}=e,u=k(n);t.state.set({height:u,isExpanded:r,width:k(o),stableHeight:h?u:void 0})})}static async synced(t={}){const{height:e,isExpanded:n,width:o}=await this.request(t),r=new E(e,o,e,n,t.postEvent);return this.sync(r),r}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 x(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 Q{constructor(t,e,n,o,r,h=c.postEvent){i(this,"ee",new _.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=n,this.currentPlatform=o,this.createRequestId=r,this.postEvent=h,this.state=new b({backgroundColor:e,headerColor:t},this.ee),this.supports=v(n,{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=ot(n,{"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 O.isColorDark(this.backgroundColor)?"dark":"light"}close(){this.postEvent("web_app_close")}get headerColor(){return this.state.get("headerColor")}isVersionAtLeast(t){return T.compareVersions(t,this.version)>=0}openLink(t,e){const n=x(t);if(!c.supports("web_app_open_link",this.version)){window.open(n,"_blank");return}return this.postEvent("web_app_open_link",{url:n,...typeof e=="boolean"?{try_instant_view:e}:{}})}openTelegramLink(t){const{hostname:e,pathname:n,search:o}=new URL(x(t));if(e!=="t.me")throw new Error(`URL has not allowed hostname: ${e}. Only "t.me" is allowed`);if(!c.supports("web_app_open_tg_link",this.version)){window.location.href=t;return}return this.postEvent("web_app_open_tg_link",{path_full:n+o})}async openInvoice(t){const{hostname:e,pathname:n}=new URL(x(t));if(e!=="t.me")throw new Error(`Incorrect hostname: ${e}`);const o=n.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);if(o===null)throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/slug" or "/$slug"');const[,,r]=o;return(await c.request("web_app_open_invoice",{slug:r},"invoice_closed",{postEvent:this.postEvent,capture:({slug:u})=>r===u})).status}get platform(){return this.currentPlatform}ready(){this.postEvent("web_app_ready")}async readTextFromClipboard(){const{data:t=null}=await c.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 c.request("web_app_request_phone","phone_requested",{postEvent:this.postEvent});return t}async requestWriteAccess(){const{status:t}=await c.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",O.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 B extends Error{constructor(t,e){super(`Method "${t}" is not supported in the Web Apps version ${e}.`),Object.setPrototypeOf(this,B.prototype)}}class j extends Error{constructor(t,e,n){super(`Parameter "${e}" in method "${t}" is not supported in the Web Apps version ${n}.`),Object.setPrototypeOf(this,j.prototype)}}function G(s,t){document.documentElement.style.setProperty(s,t)}function w(s,t){t!==null&&G(s,t)}function Z(s,t){G(s,`${t}px`)}function at(s){const{backgroundColor:t,buttonTextColor:e,secondaryBackgroundColor:n,hintColor:o,buttonColor:r,linkColor:h,textColor:u}=s;w("--tg-theme-bg-color",t),w("--tg-theme-button-color",r),w("--tg-theme-button-text-color",e),w("--tg-theme-hint-color",o),w("--tg-theme-link-color",h),w("--tg-theme-secondary-bg-color",n),w("--tg-theme-text-color",u)}function ct(s,t){const{backgroundColor:e,secondaryBackgroundColor:n}=t,{backgroundColor:o,headerColor:r}=s;w("--tg-bg-color",o),w("--tg-header-color",r==="bg_color"?e:n)}function ht(s){const t=()=>at(s);s.on("changed",t),t()}function ut(s,t){const e=()=>ct(s,t);t.on("changed",e),s.on("backgroundColorChanged",e),s.on("headerColorChanged",e),e()}function pt(s){const t=()=>{Z("--tg-viewport-height",s.height)},e=()=>{Z("--tg-viewport-stable-height",s.stableHeight)};s.on("heightChanged",t),s.on("stableHeightChanged",e),t(),e()}function lt(s){return typeof s=="boolean"?s?{themeParams:!0,viewport:!0,webApp:!0}:{}:s}function X(s){return`telegram-mini-apps-${s}`}function S(s,t){sessionStorage.setItem(X(s),JSON.stringify(t))}function V(s){const t=sessionStorage.getItem(X(s));return t?JSON.parse(t):null}function dt(s,t,e){const{isVisible:n=!1}=s?V("back-button")||{}:{},o=new D(n,t,e);return o.on("isVisibleChanged",()=>{S("back-button",{isVisible:o.isVisible})}),o}function gt(s,t){const{isConfirmationNeeded:e=!1}=s?V("closing-behavior")||{}:{},n=new L(e,t);return n.on("isConfirmationNeededChanged",()=>S("closing-behavior",{isConfirmationNeeded:n.isConfirmationNeeded})),n}function _t(s,t,e,n){const{backgroundColor:o=t,isEnabled:r=!1,isVisible:h=!1,isProgressVisible:u=!1,textColor:l=e,text:g=""}=s?V("main-button")||{}:{},p=new U(o,r,h,u,g,l,n),f=()=>S("main-button",{backgroundColor:p.backgroundColor,isEnabled:p.isEnabled,isVisible:p.isVisible,isProgressVisible:p.isProgressVisible,text:p.text,textColor:p.textColor});return p.on("backgroundColorChanged",f),p.on("isEnabledChanged",f),p.on("isVisibleChanged",f),p.on("isProgressVisibleChanged",f),p.on("textColorChanged",f),p.on("textChanged",f),p}function ft(s,t){return s?(e,n)=>{if(!c.supports(e,t))throw new B(e,t);return T.isRecord(n)&&c.detectSupportParams(e,n).forEach(o=>{if(!c.supports(e,o,t))throw new j(e,o,t)}),c.postEvent(e,n)}:c.postEvent}function mt(){let s=0;return()=>(s+=1,s.toString())}function bt(s){const t=new y(s);return y.sync(t),t}async function wt(s,t,e){const{height:n=window.innerHeight,stableHeight:o=window.innerHeight,width:r=window.innerWidth,isExpanded:h=!1}=s?V("viewport")||{}:{},l=t==="macos"||t==="web"?(()=>{const p=new E(n,r,o,h,e);return E.sync(p),p})():await E.synced({postEvent:e}),g=()=>S("viewport",{height:l.height,isExpanded:l.isExpanded,stableHeight:l.stableHeight,width:l.width});return l.on("heightChanged",g),l.on("isExpandedChanged",g),l.on("stableHeightChanged",g),l.on("widthChanged",g),l}function Ct(s,t,e,n,o,r){const{backgroundColor:h=t,headerColor:u="bg_color"}=s?V("web-app")||{}:{},l=new Q(u,h,e,n,o,r),g=()=>S("web-app",{backgroundColor:l.backgroundColor,headerColor:l.headerColor});return l.on("backgroundColorChanged",g),l.on("headerColorChanged",g),l}function A(){let s;try{return d.parse(window.location.hash.slice(1))}catch(e){s=e}const t=d.retrieveFromStorage();if(t)return t;throw new Error("Unable to extract launch params",{cause:s})}function vt(){return window.performance.getEntriesByType("navigation").some(s=>s.type==="reload")}function Et(s,t){var e,n;return s?((e=s.initData)==null?void 0:e.hash)===((n=t.initData)==null?void 0:n.hash):!1}async function yt(s={}){const{checkCompat:t=!0,cssVars:e=!1,acceptScrollbarStyle:n=!0,acceptCustomStyles:o=n,targetOrigin:r,debug:h=!1,launchParams:u=A()}=s;h&&c.setDebug(h),typeof r=="string"&&c.setTargetOrigin(r);const l=d.retrieveFromStorage(),g=u instanceof URLSearchParams||typeof u=="string"?d.parse(u):u;d.saveToStorage(g);const p=vt()||Et(l,g),{initData:f,initDataRaw:Vt,version:C,platform:Y,themeParams:tt}=g,{backgroundColor:qt="#ffffff",buttonColor:Pt="#000000",buttonTextColor:xt="#ffffff"}=tt,et=mt(),m=ft(t,C),I=bt(tt),st=Ct(p,qt,C,Y,et,m),{themeParams:Bt,viewport:jt,webApp:Ot}=lt(e);Ot&&ut(st,I),Bt&&ht(I);const nt=await wt(p,Y,m);if(jt&&pt(nt),o&&c.isIframe()){const q=document.createElement("style");q.id="telegram-custom-styles",document.head.appendChild(q),c.on("set_custom_style",$=>{q.innerHTML=$}),m("iframe_ready")}const R={backButton:dt(p,C,m),closingBehavior:gt(p,m),cloudStorage:new M(C,et,m),haptic:new W(C,m),mainButton:_t(p,Pt,xt,m),popup:new F(C,m),postEvent:m,qrScanner:new K(C,m),themeParams:I,viewport:nt,webApp:st};if(f!==void 0){const{authDate:q,hash:$,...Tt}=f;R.initData=new z(q,$,Tt),R.initDataRaw=Vt}return R}function kt(s={}){return T.withTimeout(yt(s),s.timeout||1e3)}function St(){try{return A(),!0}catch{return!1}}a.BackButton=D,a.ClosingBehaviour=L,a.CloudStorage=M,a.HapticFeedback=W,a.InitData=z,a.MainButton=U,a.MethodNotSupportedError=B,a.ParameterUnsupportedError=j,a.Popup=F,a.QRScanner=K,a.ThemeParams=y,a.Viewport=E,a.WebApp=Q,a.formatURL=x,a.init=kt,a.isTWA=St,a.retrieveLaunchParams=A,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
1
+ (function(a,d){typeof exports=="object"&&typeof module<"u"?d(exports,require("@tma.js/launch-params"),require("@tma.js/event-emitter"),require("@tma.js/bridge"),require("@tma.js/parsing"),require("@tma.js/theme-params"),require("@tma.js/colors"),require("@tma.js/utils")):typeof define=="function"&&define.amd?define(["exports","@tma.js/launch-params","@tma.js/event-emitter","@tma.js/bridge","@tma.js/parsing","@tma.js/theme-params","@tma.js/colors","@tma.js/utils"],d):(a=typeof globalThis<"u"?globalThis:a||self,d((a.tmajs=a.tmajs||{},a.tmajs.sdk={}),a.tmajs["launch-params"],a.tmajs["event-emitter"],a.tmajs.bridge,a.tmajs.parsing,a.tmajs["theme-params"],a.tmajs.colors,a.tmajs.utils))})(this,function(a,d,_,c,P,H,j,O){"use strict";var It=Object.defineProperty;var At=(a,d,_)=>d in a?It(a,d,{enumerable:!0,configurable:!0,writable:!0,value:_}):a[d]=_;var i=(a,d,_)=>(At(a,typeof d!="symbol"?d+"":d,_),_);function v(s,t){return e=>c.supports(t[e],s)}function ot(s,t){return e=>{const[n,o]=t[e];return c.supports(n,o,s)}}class b{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 n=!1;if(typeof t=="string")n=this.internalSet(t,e);else for(const o in t)this.internalSet(o,t[o])&&(n=!0);n&&this.emit("changed")}get(t){return this.state[t]}}class D{constructor(t,e,n=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",(t,e)=>{if(t==="click")return c.on("back_button_pressed",e);this.ee.on(t,e)});i(this,"off",(t,e)=>{if(t==="click")return c.off("back_button_pressed",e);this.ee.off(t,e)});i(this,"supports");this.postEvent=n,this.state=new b({isVisible:t},this.ee),this.supports=v(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 L{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({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 rt=P.array().of(P.string());function N(s,t){return s.reduce((e,n)=>(e[n]=t,e),{})}class M{constructor(t,e,n=c.postEvent){i(this,"supports");this.createRequestId=e,this.postEvent=n,this.supports=v(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,n={}){const{result:o,error:r}=await c.request("web_app_invoke_custom_method",{method:t,params:e,req_id:this.createRequestId()},"custom_method_invoked",{...n,postEvent:this.postEvent});if(r)throw new Error(typeof r=="string"?r:`Unknown error: ${JSON.stringify(r)}`);return o}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 rt.parse(e)}async getValues(t,e){if(t.length===0)return N(t,"");const n=P.json(N(t,P.string())),o=await this.invokeCustomMethod("getStorageValues",{keys:t},e);return n.parse(o)}async saveValue(t,e,n){await this.invokeCustomMethod("saveStorageValue",{key:t,value:e},n)}}class W{constructor(t,e=c.postEvent){i(this,"supports");this.postEvent=e,this.supports=v(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 z{constructor(t,e,n={}){i(this,"state");const{chat:o=null,canSendAfter:r=null,chatType:h=null,chatInstance:u=null,user:l=null,queryId:g=null,receiver:p=null,startParam:f=null}=n;this.state=new b({authDate:t,canSendAfter:r===null?null:new Date(t.getTime()+r*1e3),chat:o,chatType:h,chatInstance:u,user:l,queryId:g,receiver:p,startParam:f,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 U{constructor(t,e,n,o,r,h,u=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",(t,e)=>{if(t==="click")return c.on("main_button_pressed",e);this.ee.on(t,e)});i(this,"off",(t,e)=>{if(t==="click")return c.off("main_button_pressed",e);this.ee.off(t,e)});this.postEvent=u,this.state=new b({backgroundColor:t,isEnabled:e,isVisible:n,isProgressVisible:o,text:r,textColor:h},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 it(s){const t=s.message.trim(),e=(s.title||"").trim(),n=s.buttons||[];let o;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(n.length>3)throw new Error(`Buttons have incorrect size: ${n.length}`);return n.length===0?o=[{type:"close",id:""}]:o=n.map(r=>{const{id:h=""}=r;if(h.length>64)throw new Error(`Button ID has incorrect size: ${h}`);if(r.type===void 0||r.type==="default"||r.type==="destructive"){const u=r.text.trim();if(u.length===0||u.length>64){const l=r.type||"default";throw new Error(`Button text with type "${l}" has incorrect size: ${r.text.length}`)}return{...r,text:u,id:h}}return{...r,id:h}}),{title:e,message:t,buttons:o}}class F{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({isOpened:!1},this.ee),this.supports=v(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 c.request("web_app_open_popup",it(t),"popup_closed",{postEvent:this.postEvent});return e}finally{this.state.set("isOpened",!1)}}}class K{constructor(t,e=c.postEvent){i(this,"ee",new _.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 b({isOpened:!1},this.ee),this.supports=v(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 c.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 J(s){const{backgroundColor:t=null,buttonTextColor:e=null,buttonColor:n=null,hintColor:o=null,linkColor:r=null,textColor:h=null,secondaryBackgroundColor:u=null}=s;return{backgroundColor:t,buttonTextColor:e,buttonColor:n,hintColor:o,linkColor:r,textColor:h,secondaryBackgroundColor:u}}class y{constructor(t){i(this,"ee",new _.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 b(J(t),this.ee)}static async request(t={}){const{timeout:e=1e3,...n}=t,o=await c.request("web_app_request_theme","theme_changed",{...n,timeout:e});return H.parse(o.theme_params)}static sync(t){c.on("theme_changed",e=>{t.state.set(J(H.parse(e.theme_params)))})}static async synced(t){const e=await this.request(t),n=new y(e);return this.sync(n),n}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||j.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 k(s){return s<0?0:s}class E{constructor(t,e,n,o,r=c.postEvent){i(this,"ee",new _.EventEmitter);i(this,"state");i(this,"on",this.ee.on.bind(this.ee));i(this,"off",this.ee.off.bind(this.ee));this.postEvent=r,this.state=new b({height:k(t),isExpanded:o,stableHeight:k(n),width:k(e)},this.ee)}static async request(t={}){const{timeout:e=1e3,...n}=t,{is_expanded:o,is_state_stable:r,...h}=await c.request("web_app_request_viewport","viewport_changed",{...n,timeout:e});return{...h,isExpanded:o,isStateStable:r}}static sync(t){c.on("viewport_changed",e=>{const{height:n,width:o,is_expanded:r,is_state_stable:h}=e,u=k(n);t.state.set({height:u,isExpanded:r,width:k(o),stableHeight:h?u:void 0})})}static async synced(t={}){const{height:e,isExpanded:n,width:o}=await this.request(t),r=new E(e,o,e,n,t.postEvent);return this.sync(r),r}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 x(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 Q{constructor(t,e,n,o,r,h=c.postEvent){i(this,"ee",new _.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=n,this.currentPlatform=o,this.createRequestId=r,this.postEvent=h,this.state=new b({backgroundColor:e,headerColor:t},this.ee),this.supports=v(n,{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=ot(n,{"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 j.isColorDark(this.backgroundColor)?"dark":"light"}close(){this.postEvent("web_app_close")}get headerColor(){return this.state.get("headerColor")}isVersionAtLeast(t){return O.compareVersions(t,this.version)>=0}openLink(t,e){const n=x(t);if(!c.supports("web_app_open_link",this.version)){window.open(n,"_blank");return}return this.postEvent("web_app_open_link",{url:n,...typeof e=="boolean"?{try_instant_view:e}:{}})}openTelegramLink(t){const{hostname:e,pathname:n,search:o}=new URL(x(t));if(e!=="t.me")throw new Error(`URL has not allowed hostname: ${e}. Only "t.me" is allowed`);if(!c.supports("web_app_open_tg_link",this.version)){window.location.href=t;return}return this.postEvent("web_app_open_tg_link",{path_full:n+o})}async openInvoice(t){const{hostname:e,pathname:n}=new URL(x(t));if(e!=="t.me")throw new Error(`Incorrect hostname: ${e}`);const o=n.match(/^\/(\$|invoice\/)([A-Za-z0-9\-_=]+)$/);if(o===null)throw new Error('Link pathname has incorrect format. Expected to receive "/invoice/slug" or "/$slug"');const[,,r]=o;return(await c.request("web_app_open_invoice",{slug:r},"invoice_closed",{postEvent:this.postEvent,capture:({slug:u})=>r===u})).status}get platform(){return this.currentPlatform}ready(){this.postEvent("web_app_ready")}async readTextFromClipboard(){const{data:t=null}=await c.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 c.request("web_app_request_phone","phone_requested",{postEvent:this.postEvent});return t}async requestWriteAccess(){const{status:t}=await c.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",j.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 B extends Error{constructor(t,e){super(`Method "${t}" is not supported in the Web Apps version ${e}.`),Object.setPrototypeOf(this,B.prototype)}}class T extends Error{constructor(t,e,n){super(`Parameter "${e}" in method "${t}" is not supported in the Web Apps version ${n}.`),Object.setPrototypeOf(this,T.prototype)}}function G(s,t){document.documentElement.style.setProperty(s,t)}function w(s,t){t!==null&&G(s,t)}function Z(s,t){G(s,`${t}px`)}function at(s){const{backgroundColor:t,buttonTextColor:e,secondaryBackgroundColor:n,hintColor:o,buttonColor:r,linkColor:h,textColor:u}=s;w("--tg-theme-bg-color",t),w("--tg-theme-button-color",r),w("--tg-theme-button-text-color",e),w("--tg-theme-hint-color",o),w("--tg-theme-link-color",h),w("--tg-theme-secondary-bg-color",n),w("--tg-theme-text-color",u)}function ct(s,t){const{backgroundColor:e,secondaryBackgroundColor:n}=t,{backgroundColor:o,headerColor:r}=s;w("--tg-bg-color",o),w("--tg-header-color",r==="bg_color"?e:n)}function ht(s){const t=()=>at(s);s.on("changed",t),t()}function ut(s,t){const e=()=>ct(s,t);t.on("changed",e),s.on("backgroundColorChanged",e),s.on("headerColorChanged",e),e()}function pt(s){const t=()=>{Z("--tg-viewport-height",s.height)},e=()=>{Z("--tg-viewport-stable-height",s.stableHeight)};s.on("heightChanged",t),s.on("stableHeightChanged",e),t(),e()}function lt(s){return typeof s=="boolean"?s?{themeParams:!0,viewport:!0,webApp:!0}:{}:s}function X(s){return`telegram-mini-apps-${s}`}function S(s,t){sessionStorage.setItem(X(s),JSON.stringify(t))}function V(s){const t=sessionStorage.getItem(X(s));return t?JSON.parse(t):null}function dt(s,t,e){const{isVisible:n=!1}=s?V("back-button")||{}:{},o=new D(n,t,e);return o.on("isVisibleChanged",()=>{S("back-button",{isVisible:o.isVisible})}),o}function gt(s,t){const{isConfirmationNeeded:e=!1}=s?V("closing-behavior")||{}:{},n=new L(e,t);return n.on("isConfirmationNeededChanged",()=>S("closing-behavior",{isConfirmationNeeded:n.isConfirmationNeeded})),n}function _t(s,t,e,n){const{backgroundColor:o=t,isEnabled:r=!1,isVisible:h=!1,isProgressVisible:u=!1,textColor:l=e,text:g=""}=s?V("main-button")||{}:{},p=new U(o,r,h,u,g,l,n),f=()=>S("main-button",{backgroundColor:p.backgroundColor,isEnabled:p.isEnabled,isVisible:p.isVisible,isProgressVisible:p.isProgressVisible,text:p.text,textColor:p.textColor});return p.on("backgroundColorChanged",f),p.on("isEnabledChanged",f),p.on("isVisibleChanged",f),p.on("isProgressVisibleChanged",f),p.on("textColorChanged",f),p.on("textChanged",f),p}function ft(s,t){return s?(e,n)=>{if(!c.supports(e,t))throw new B(e,t);return O.isRecord(n)&&c.detectSupportParams(e,n).forEach(o=>{if(!c.supports(e,o,t))throw new T(e,o,t)}),c.postEvent(e,n)}:c.postEvent}function mt(){let s=0;return()=>(s+=1,s.toString())}function bt(s){const t=new y(s);return y.sync(t),t}async function wt(s,t,e){const{height:n=window.innerHeight,stableHeight:o=window.innerHeight,width:r=window.innerWidth,isExpanded:h=!1}=s?V("viewport")||{}:{},l=t==="macos"||t==="web"?(()=>{const p=new E(n,r,o,h,e);return E.sync(p),p})():await E.synced({postEvent:e}),g=()=>S("viewport",{height:l.height,isExpanded:l.isExpanded,stableHeight:l.stableHeight,width:l.width});return l.on("heightChanged",g),l.on("isExpandedChanged",g),l.on("stableHeightChanged",g),l.on("widthChanged",g),l}function Ct(s,t,e,n,o,r){const{backgroundColor:h=t,headerColor:u="bg_color"}=s?V("web-app")||{}:{},l=new Q(u,h,e,n,o,r),g=()=>S("web-app",{backgroundColor:l.backgroundColor,headerColor:l.headerColor});return l.on("backgroundColorChanged",g),l.on("headerColorChanged",g),l}function I(){let s;try{return d.parse(window.location.hash.slice(1))}catch(e){s=e}const t=d.retrieveFromStorage();if(t)return t;throw new Error("Unable to extract launch params",{cause:s})}function vt(){return window.performance.getEntriesByType("navigation").some(s=>s.type==="reload")}function Et(s,t){var e,n;return s?((e=s.initData)==null?void 0:e.hash)===((n=t.initData)==null?void 0:n.hash):!1}async function yt(s={}){const{checkCompat:t=!0,cssVars:e=!1,acceptScrollbarStyle:n=!0,acceptCustomStyles:o=n,targetOrigin:r,debug:h=!1,launchParams:u=I()}=s;h&&c.setDebug(h),typeof r=="string"&&c.setTargetOrigin(r);const l=d.retrieveFromStorage(),g=u instanceof URLSearchParams||typeof u=="string"?d.parse(u):u;d.saveToStorage(g);const p=vt()||Et(l,g),{initData:f,initDataRaw:Vt,version:C,platform:Y,themeParams:tt}=g,{backgroundColor:qt="#ffffff",buttonColor:Pt="#000000",buttonTextColor:xt="#ffffff"}=tt,et=mt(),m=ft(t,C),A=bt(tt),st=Ct(p,qt,C,Y,et,m),{themeParams:Bt,viewport:Tt,webApp:jt}=lt(e);jt&&ut(st,A),Bt&&ht(A);const nt=await wt(p,Y,m);if(Tt&&pt(nt),o&&c.isIframe()){const q=document.createElement("style");q.id="telegram-custom-styles",document.head.appendChild(q),c.on("set_custom_style",$=>{q.innerHTML=$}),m("iframe_ready")}const R={backButton:dt(p,C,m),closingBehavior:gt(p,m),cloudStorage:new M(C,et,m),haptic:new W(C,m),mainButton:_t(p,Pt,xt,m),popup:new F(C,m),postEvent:m,qrScanner:new K(C,m),themeParams:A,viewport:nt,webApp:st};if(f!==void 0){const{authDate:q,hash:$,...Ot}=f;R.initData=new z(q,$,Ot),R.initDataRaw=Vt}return R}function kt(s={}){return O.withTimeout(yt(s),s.timeout||1e3)}function St(){try{return I(),!0}catch{return!1}}a.BackButton=D,a.ClosingBehaviour=L,a.CloudStorage=M,a.HapticFeedback=W,a.InitData=z,a.MainButton=U,a.MethodNotSupportedError=B,a.ParameterUnsupportedError=T,a.Popup=F,a.QRScanner=K,a.ThemeParams=y,a.Viewport=E,a.WebApp=Q,a.formatURL=x,a.init=kt,a.isTWA=St,a.retrieveLaunchParams=I,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=index.umd.js.map