"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let R=class Q extends Error{constructor(t,r,n){super(typeof r=="object"?r.message:r||t,{cause:typeof r=="object"?r.cause:n}),this.type=t,Object.setPrototypeOf(this,Q.prototype)}};function ee(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Ne(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}const De="ERR_INVALID_VALUE",Ce="ERR_UNEXPECTED_VALUE",Oe="ERR_UNEXPECTED_TYPE",te="ERR_PARSE";function re(e,t){const r={};for(const n in e){const o=e[n];if(!o)continue;let s,a;typeof o=="function"?(s=n,a=o):[s,a]=o;try{const i=a(t(s));i!==void 0&&(r[n]=i)}catch(i){throw new R(te,`Parser for "${n}" property failed${s===n?"":`. Source field: "${s}"`}`,i)}}return r}function ne(e){let t=e;if(typeof t=="string")try{t=JSON.parse(t)}catch(r){throw new R(De,{cause:r})}if(typeof t!="object"||!t||Array.isArray(t))throw new R(Ce);return t}function d(e,t){return r=>{const n=o=>{if(!(r&&o===void 0))try{return t(o)}catch(s){throw new R(te,{message:`"${e}" transformer failed to parse the value`,cause:s})}};return Object.assign(n,{isValid(o){try{return n(o),!0}catch{return!1}}})}}function b(e,t){return d(t||"object",r=>{const n=ne(r);return re(e,o=>n[o])})}function T(e){throw new R(Oe,`Unexpected value received: ${JSON.stringify(e)}`)}const $=d("boolean",e=>{if(typeof e=="boolean")return e;const t=String(e);if(t==="1"||t==="true")return!0;if(t==="0"||t==="false")return!1;T(e)}),_=d("string",e=>{if(typeof e=="string"||typeof e=="number")return e.toString();T(e)}),P=d("number",e=>{if(typeof e=="number")return e;if(typeof e=="string"){const t=Number(e);if(!Number.isNaN(t))return t}T(e)}),Ue=d("date",e=>e instanceof Date?e:new Date(P()(e)*1e3));function oe(e,t){return d(t||"searchParams",r=>{typeof r!="string"&&!(r instanceof URLSearchParams)&&T(r);const n=typeof r=="string"?new URLSearchParams(r):r;return re(e,o=>{const s=n.get(o);return s===null?void 0:s})})}function L(e){for(const t in e)e[t]=[ee(t),e[t]];return e}const je=e=>{const t=P(),r=P(!0),n=_(),o=_(!0),s=$(!0),a=b(L({addedToAttachmentMenu:s,allowsWriteToPm:s,firstName:n,id:t,isBot:s,isPremium:s,languageCode:o,lastName:o,photoUrl:o,username:o}),"User")(!0);return oe(L({authDate:Ue(),canSendAfter:r,chat:b(L({id:t,type:n,title:n,photoUrl:o,username:o}),"Chat")(!0),chatInstance:o,chatType:o,hash:n,queryId:o,receiver:a,startParam:o,user:a}),"initData")(e)};function ke(e){return/^#[\da-f]{6}$/i.test(e)}function Le(e){return/^#[\da-f]{3}$/i.test(e)}function Me(e){const t=e.replace(/\s/g,"").toLowerCase();if(ke(t))return t;if(Le(t)){let n="#";for(let o=0;o<3;o+=1)n+=t[1+o].repeat(2);return n}const r=t.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/)||t.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);if(!r)throw new Error(`Value "${e}" does not satisfy any of known RGB formats.`);return r.slice(1).reduce((n,o)=>{const s=parseInt(o,10).toString(16);return n+(s.length===1?"0":"")+s},"#")}const xe=d("rgb",e=>Me(_()(e))),We=d("themeParams",e=>{const t=xe(!0);return Object.entries(ne(e)).reduce((r,[n,o])=>(r[Ne(n)]=t(o),r),{})});function se(e){return JSON.stringify(Object.fromEntries(Object.entries(e).map(([t,r])=>[ee(t),r])))}const Ie=e=>{const t=_(),r=_(!0),n=$(!0);return oe({botInline:["tgWebAppBotInline",n],initData:["tgWebAppData",je(!0)],initDataRaw:["tgWebAppData",r],platform:["tgWebAppPlatform",t],showSettings:["tgWebAppShowSettings",n],startParam:["tgWebAppStartParam",r],themeParams:["tgWebAppThemeParams",We()],version:["tgWebAppVersion",t]},"launchParams")(e)};function qe(e){const{initDataRaw:t,startParam:r,showSettings:n,botInline:o}=e,s=new URLSearchParams;return s.set("tgWebAppPlatform",e.platform),s.set("tgWebAppThemeParams",se(e.themeParams)),s.set("tgWebAppVersion",e.version),t&&s.set("tgWebAppData",t),r&&s.set("tgWebAppStartParam",r),typeof n=="boolean"&&s.set("tgWebAppShowSettings",n?"1":"0"),typeof o=="boolean"&&s.set("tgWebAppBotInline",o?"1":"0"),s.toString()}const ae=b({eventType:_(),eventData:e=>e},"miniAppsMessage"),ie=d("fn",e=>{if(typeof e=="function")return e;T(e)});function He(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}const Fe=b({TelegramWebviewProxy:b({postEvent:ie()})()});function I(e){return Fe().isValid(e)}function ce(){try{return window.self!==window.top}catch{return!0}}var Je=Object.defineProperty,Be=(e,t,r)=>t in e?Je(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,ue=(e,t,r)=>Be(e,typeof t!="symbol"?t+"":t,r);class l extends Error{constructor(t,r,n){super(typeof r=="object"?r.message:r||t,{cause:typeof r=="object"?r.cause:n}),this.type=t,Object.setPrototypeOf(this,l.prototype)}}function N(e,t,r){return e.addEventListener(t,r),()=>e.removeEventListener(t,r)}function U(...e){const t=e.flat(1);return[t.push.bind(t),()=>{t.forEach(r=>{r()})}]}function Ve(e,t){return e instanceof l&&e.type===t}function j(e){return t=>Ve(t,e)}const q="ERR_ABORTED",H="ERR_CANCELED",F="ERR_TIMED_OUT";function x(e){return new l(q,{cause:e})}const Ge=j(F),Ke=j(q),Ze=j(H);function z(e,t){return e.reject=t.reject,e}class g extends Promise{constructor(t,r){let n,o;typeof t=="function"?(n=t,o=r):o=t;let s,a;super((i,c)=>{o||(o={});const{abortSignal:u}=o;if(u&&u.aborted)return c(x(u.reason));const[f,w]=U(),h=m=>(...$e)=>(w(),m(...$e)),S=new AbortController,{signal:A}=S;a=h(m=>{S.abort(m),c(m)}),s=h(i),u&&f(N(u,"abort",()=>{a(x(u.reason))}));const{timeout:k}=o;if(k){const m=setTimeout(()=>{a(new l(F,`Timeout reached: ${k}ms`))},k);f(()=>{clearTimeout(m)})}n&&n(s,a,A)}),ue(this,"reject"),this.reject=a}static withFn(t,r){return new g((n,o,s)=>{try{const a=t(s);return a instanceof Promise?a.then(n,o):n(a)}catch(a){o(a)}},r)}static resolve(t){return new g(r=>{r(t)})}static reject(t){return new g((r,n)=>{n(t)})}cancel(){this.reject(new l(H))}catch(t){return this.then(void 0,t)}finally(t){return z(super.finally(t),this)}then(t,r){return z(super.then(t,r),this)}}function X(e,t){return e.resolve=t.resolve,e}class y extends g{constructor(t,r){let n,o;typeof t=="function"?(n=t,o=r):o=t;let s;super((a,i,c)=>{s=a,n&&n(a,i,c)},o),ue(this,"resolve"),this.resolve=s}static withFn(t,r){return new y((n,o,s)=>g.withFn(t,{abortSignal:s}).then(n,o),r)}static resolve(t){return new y(r=>{r(t)})}static reject(t){return new y((r,n)=>{n(t)})}catch(t){return this.then(void 0,t)}finally(t){return X(super.finally(t),this)}then(t,r){return X(super.then(t,r),this)}}function ze(e,t){return new g(r=>{setTimeout(r,e)},{abortSignal:t})}function pe(e){return`tapps/${e}`}function fe(e,t){sessionStorage.setItem(pe(e),JSON.stringify(t))}function _e(e){const t=sessionStorage.getItem(pe(e));try{return t?JSON.parse(t):void 0}catch{}}function Xe(e){return e.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`)}function Ye(e){return e.replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function Qe(e){return e.replace(/_[a-z]/g,t=>t[1].toUpperCase())}function le(e,t){t||(t={});const{textColor:r,bgColor:n,shouldLog:o=!0}=t;function s(a,...i){if(!o||typeof o=="function"&&!o())return;const c="font-weight:bold;padding:0 5px;border-radius:5px";console[a](`%c${Intl.DateTimeFormat("en-GB",{hour:"2-digit",minute:"2-digit",second:"2-digit",fractionalSecondDigits:3,timeZone:"UTC"}).format(new Date)}%c / %c${e}`,`${c};background-color: lightblue;color:black`,"",`${c};${r?`color:${r};`:""}${n?`background-color:${n}`:""}`,...i)}return[function(...a){s("log",...a)},function(...a){s("error",...a)}]}function et(e,t){document.documentElement.style.setProperty(e,t)}function tt(e){document.documentElement.style.removeProperty(e)}function rt(e,t){t()}function E(e,t){t||(t={});const r=t.equals||Object.is;let n=[],o=e;const s=u=>{if(!r(o,u)){const f=o;o=u,rt(c,()=>{[...n].forEach(([w,h])=>{w(u,f),h&&i(w,!0)})})}};function a(u){const f=typeof u!="object"?{once:u}:u;return{once:f.once||!1,signal:f.signal||!1}}const i=(u,f)=>{const w=a(f),h=n.findIndex(([S,A])=>S===u&&A.once===w.once&&A.signal===w.signal);h>=0&&n.splice(h,1)},c=Object.assign(function(){return nt(c),o},{destroy(){n=[]},set:s,reset(){s(e)},sub(u,f){return n.push([u,a(f)]),()=>i(u,f)},unsub:i,unsubAll(){n=n.filter(u=>u[1].signal)}});return c}const M=[];function nt(e){M.length&&M[M.length-1].add(e)}const J=E(!1),[B,ot]=le("Bridge",{bgColor:"#9147ff",textColor:"white",shouldLog:J}),st={clipboard_text_received:b({req_id:_(),data:e=>e===null?e:_(!0)(e)},"clipboard_text_received"),custom_method_invoked:b({req_id:_(),result:e=>e,error:_(!0)},"custom_method_invoked"),popup_closed:d("popup_closed",e=>e?b({button_id:t=>t==null?void 0:_()(t)})()(e):{}),viewport_changed:b({height:P(),width:e=>e==null?window.innerWidth:P()(e),is_state_stable:$(),is_expanded:$()},"viewport_changed")};function at(e){const t=window,[,r]=U(N(t,"resize",()=>{e(["viewport_changed",{width:window.innerWidth,height:window.innerHeight,is_state_stable:!0,is_expanded:!0}])}),N(t,"message",n=>{if(n.source!==t.parent)return;let o;try{o=ae()(n.data)}catch{return}const{eventType:s,eventData:a}=o,i=st[s];try{const c=i?i()(a):a;B("Event received:",c?{eventType:s,eventData:c}:{eventType:s}),e([s,c])}catch(c){ot([`An error occurred processing the "${s}" event from the Telegram application.`,"Please, file an issue here:","https://github.com/Telegram-Mini-Apps/telegram-apps/issues/new/choose"].join(` `),o,c)}}));return r}const D=E(),C=E();function be(){return C()||C.set(at(D.set)),D}const v=E({});function ge(e){let t=v()[e];return t||(t=E(void 0,{equals(){return!1}}),be().sub(r=>{r&&r[0]===e&&t.set(r[1])}),v.set({...v(),[e]:t})),t}function de(e,t,r){return ge(e).sub(t,r)}const we="ERR_METHOD_UNSUPPORTED",he="ERR_RETRIEVE_LP_FAILED",me="ERR_METHOD_PARAMETER_UNSUPPORTED",Ee="ERR_UNKNOWN_ENV",ye="ERR_INVOKE_CUSTOM_METHOD_RESPONSE",V=E("https://web.telegram.org");function G(e,t){B("Posting event:",t?{eventType:e,eventData:t}:{eventType:e});const r=window;if(I(r)){r.TelegramWebviewProxy.postEvent(e,JSON.stringify(t));return}const n=JSON.stringify({eventType:e,eventData:t});if(ce())return r.parent.postMessage(n,V());const{external:o}=r;if(b({notify:ie()})().isValid(o)){o.notify(n);return}throw new l(Ee)}function K(e,t,r){r||(r={});const{capture:n}=r,[o,s]=U();return new g(a=>{(Array.isArray(t)?t:[t]).forEach(i=>{o(de(i,c=>{(!n||(Array.isArray(t)?n({event:i,payload:c}):n(c)))&&a(c)}))}),(r.postEvent||G)(e,r.params)},r).finally(s)}function Z(e){return Ie()(e)}function ve(e){return Z(e.replace(/^[^?#]*[?#]/,"").replace(/[?#]/g,"&"))}function it(){return ve(window.location.href)}function ct(){const e=performance.getEntriesByType("navigation")[0];if(!e)throw new Error("Unable to get first navigation entry.");return ve(e.name)}const ut="launchParams";function pt(){return Z(_e(ut)||"")}function Re(e){fe("launchParams",qe(e))}function Pe(e){return e instanceof Error?e.message+(e.cause?` ${Pe(e.cause)}`:""):JSON.stringify(e)}function Te(){const e=[];for(const t of[it,ct,pt])try{const r=t();return Re(r),r}catch(r){e.push(r)}throw new l(he,["Unable to retrieve launch parameters from any known source. Perhaps, you have opened your app outside Telegram?","📖 Refer to docs for more information:","https://docs.telegram-mini-apps.com/packages/telegram-apps-sdk/environment","Collected errors:",...e.map(t=>`— ${Pe(t)}`)].join(` `))}function ft(e){if(e==="simple")try{return Te(),!0}catch{return!1}return g.withFn(async()=>{if(I(window))return!0;try{return await K("web_app_request_theme","theme_changed",{timeout:100}),!0}catch{return!1}},e)}function O(e,t){window.dispatchEvent(new MessageEvent("message",{data:JSON.stringify({eventType:e,eventData:t}),source:window.parent}))}function _t(e,t){if(typeof t=="string")try{const{eventType:r}=ae()(t);r==="web_app_request_theme"&&O("theme_changed",{theme_params:JSON.parse(se(e))}),r==="web_app_request_viewport"&&O("viewport_changed",{width:window.innerWidth,height:window.innerHeight,is_state_stable:!0,is_expanded:!0})}catch{}}function lt(e){var n;const t=typeof e=="string"?Z(e):e;Re(t);const r=(n=window.TelegramWebviewProxy)==null?void 0:n.postEvent;window.TelegramWebviewProxy={postEvent(o,s){_t(t.themeParams,JSON.stringify({eventType:o,eventData:s})),r==null||r(o,s)}},B("Environment was mocked by the mockTelegramEnv function")}function bt(){[["TelegramGameProxy_receiveEvent"],["TelegramGameProxy","receiveEvent"],["Telegram","WebView","receiveEvent"]].forEach(e=>{let t=window;e.forEach((r,n,o)=>{if(n===o.length-1){t[r]=O;return}r in t||(t[r]={}),t=t[r]})})}function gt(){["TelegramGameProxy_receiveEvent","TelegramGameProxy","Telegram"].forEach(e=>{delete window[e]})}function dt(e,t,r){ge(e).unsub(t,r)}function wt(e,t){return be().sub(e,t)}function ht(e,t){D.unsub(e,t)}function Se(e){return({req_id:t})=>t===e}function Y(e){return e.split(".").map(Number)}function Ae(e,t){const r=Y(e),n=Y(t),o=Math.max(r.length,n.length);for(let s=0;si?1:-1}return 0}function p(e,t){return Ae(e,t)<=0}function W(e,t,r){if(typeof r=="string"){if(e==="web_app_open_link"){if(t==="try_instant_view")return p("6.4",r);if(t==="try_browser")return p("7.6",r)}if(e==="web_app_set_header_color"&&t==="color")return p("6.9",r);if(e==="web_app_close"&&t==="return_back")return p("7.6",r);if(e==="web_app_setup_main_button"&&t==="has_shine_effect")return p("7.10",r)}switch(e){case"web_app_open_tg_link":case"web_app_open_invoice":case"web_app_setup_back_button":case"web_app_set_background_color":case"web_app_set_header_color":case"web_app_trigger_haptic_feedback":return p("6.1",t);case"web_app_open_popup":return p("6.2",t);case"web_app_close_scan_qr_popup":case"web_app_open_scan_qr_popup":case"web_app_read_text_from_clipboard":return p("6.4",t);case"web_app_switch_inline_query":return p("6.7",t);case"web_app_invoke_custom_method":case"web_app_request_write_access":case"web_app_request_phone":return p("6.9",t);case"web_app_setup_settings_button":return p("6.10",t);case"web_app_biometry_get_info":case"web_app_biometry_open_settings":case"web_app_biometry_request_access":case"web_app_biometry_request_auth":case"web_app_biometry_update_token":return p("7.2",t);case"web_app_setup_swipe_behavior":return p("7.7",t);case"web_app_share_to_story":return p("7.8",t);case"web_app_setup_secondary_button":case"web_app_set_bottom_bar_color":return p("7.10",t);default:return["iframe_ready","iframe_will_reload","web_app_close","web_app_data_send","web_app_expand","web_app_open_link","web_app_ready","web_app_request_theme","web_app_request_viewport","web_app_setup_main_button","web_app_setup_closing_behavior"].includes(e)}}function mt(e,t){t||(t="strict");const r=typeof t=="function"?t:n=>{const{method:o,version:s}=n;let a,i;if("param"in n?(a=`Parameter "${n.param}" of "${o}" method is unsupported in Mini Apps version ${s}`,i=me):(a=`Method "${o}" is unsupported in Mini Apps version ${s}`,i=we),t==="strict")throw new l(i,a);return console.warn(a)};return(n,o)=>W(n,e)?He(o)&&n==="web_app_set_header_color"&&"color"in o&&!W(n,"color",e)?r({version:e,method:n,param:"color"}):G(n,o):r({version:e,method:n})}function Et(e,t,r,n){return K("web_app_invoke_custom_method","custom_method_invoked",{...n||{},params:{method:e,params:t,req_id:r},capture:Se(r)}).then(({result:o,error:s})=>{if(s)throw new l(ye,s);return o})}function yt(e){e.unsubAll(),e.reset()}function vt(){var e;(e=C())==null||e(),[...Object.values(v()),v,D,C,V,J].forEach(yt)}exports.$debug=J;exports.$targetOrigin=V;exports.CancelablePromise=g;exports.ERR_ABORTED=q;exports.ERR_CANCELED=H;exports.ERR_CUSTOM_METHOD_ERR_RESPONSE=ye;exports.ERR_METHOD_PARAMETER_UNSUPPORTED=me;exports.ERR_METHOD_UNSUPPORTED=we;exports.ERR_RETRIEVE_LP_FAILED=he;exports.ERR_TIMED_OUT=F;exports.ERR_UNKNOWN_ENV=Ee;exports.EnhancedPromise=y;exports.TypedError=l;exports.addEventListener=N;exports.camelToKebab=Xe;exports.camelToSnake=Ye;exports.captureSameReq=Se;exports.compareVersions=Ae;exports.createAbortError=x;exports.createCbCollector=U;exports.createLogger=le;exports.createPostEvent=mt;exports.createTypedErrorPredicate=j;exports.defineEventHandlers=bt;exports.deleteCssVar=tt;exports.emitMiniAppsEvent=O;exports.getStorageValue=_e;exports.hasWebviewProxy=I;exports.invokeCustomMethod=Et;exports.isAbortError=Ke;exports.isCanceledError=Ze;exports.isIframe=ce;exports.isTMA=ft;exports.isTimeoutError=Ge;exports.mockTelegramEnv=lt;exports.off=dt;exports.on=de;exports.postEvent=G;exports.removeEventHandlers=gt;exports.request=K;exports.resetPackageState=vt;exports.retrieveLaunchParams=Te;exports.setCssVar=et;exports.setStorageValue=fe;exports.sleep=ze;exports.snakeToCamel=Qe;exports.subscribe=wt;exports.supports=W;exports.unsubscribe=ht; //# sourceMappingURL=index.cjs.map