"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});async function b(t,e){const r=new TextEncoder,n=e instanceof ArrayBuffer?e:r.encode(e),o=await crypto.subtle.importKey("raw",n,{name:"HMAC",hash:"SHA-256"},!1,["sign","verify"]);return await crypto.subtle.sign("HMAC",o,r.encode(t))}async function m(t,e){const r=await b(e,"WebAppData"),n=await b(t,r);return Array.prototype.map.call(new Uint8Array(n),i=>i.toString(16).padStart(2,"0")).join("")}function U(t){const e={};for(const r in t){const n=t[r];n!==void 0&&(e[r]=n)}return e}function S(t){return t?JSON.stringify({added_to_attachment_menu:t.addedToAttachmentMenu,allows_write_to_pm:t.allowsWriteToPm,first_name:t.firstName,id:t.id,is_bot:t.isBot,is_premium:t.isPremium,language_code:t.languageCode,last_name:t.lastName,photo_url:t.photoUrl,username:t.username}):void 0}function P({chat:t,receiver:e,user:r,...n}){var o;return new URLSearchParams(U({auth_date:n.authDate?(+n.authDate/1e3|0).toString():void 0,can_send_after:(o=n.canSendAfter)==null?void 0:o.toString(),chat:t?JSON.stringify({id:t.id,type:t.type,title:t.title,photo_url:t.type,username:t.username}):void 0,chat_instance:n.chatInstance,chat_type:n.chatType||void 0,hash:n.hash,query_id:n.queryId,receiver:S(e),start_param:n.startParam||void 0,user:S(r)}))}async function A(t,e,r){const n=P({...t,authDate:r}),o=[...n.entries()].map(([i,c])=>`${i}=${c}`).sort();return n.append("hash",await m(o.join(` `),e)),n.toString()}async function T(t,e,r={}){let n,o;const i=[];if(new URLSearchParams(typeof t=="string"||t instanceof URLSearchParams?t:P(t)).forEach((s,h)=>{if(h==="hash"){o=s;return}if(h==="auth_date"){const g=parseInt(s,10);if(Number.isNaN(g))throw new TypeError('"auth_date" should present integer');n=new Date(g*1e3)}i.push(`${h}=${s}`)}),!o)throw new Error('"hash" is empty or not found');if(!n)throw new Error('"auth_date" is empty or not found');const{expiresIn:c=86400}=r;if(c>0&&+n+c*1e3new w(t,!1,e)}const u=d(t=>{if(typeof t=="boolean")return t;const e=String(t);if(e==="1"||e==="true")return!0;if(e==="0"||e==="false")return!1;throw l()},"boolean");function E(t,e){const r={};for(const n in t){const o=t[n];if(!o)continue;let i,c;if(typeof o=="function"||"parse"in o)i=n,c=typeof o=="function"?o:o.parse.bind(o);else{const{type:s}=o;i=o.from||n,c=typeof s=="function"?s:s.parse.bind(s)}try{const s=c(e(i));s!==void 0&&(r[n]=s)}catch(s){throw _(D,`Unable to parse field "${n}"`,s)}}return r}function N(t){let e=t;if(typeof e=="string"&&(e=JSON.parse(e)),typeof e!="object"||e===null||Array.isArray(e))throw l();return e}function p(t,e){return new w(r=>{const n=N(r);return E(t,o=>n[o])},!1,e)}const f=d(t=>{if(typeof t=="number")return t;if(typeof t=="string"){const e=Number(t);if(!Number.isNaN(e))return e}throw l()},"number");function x(t){return/^#[\da-f]{6}$/i.test(t)}function R(t){return/^#[\da-f]{3}$/i.test(t)}function O(t){const e=t.replace(/\s/g,"").toLowerCase();if(x(e))return e;if(R(e)){let n="#";for(let o=0;o<3;o+=1)n+=e[1+o].repeat(2);return n}const r=e.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/)||e.match(/^rgba\((\d{1,3}),(\d{1,3}),(\d{1,3}),\d{1,3}\)$/);if(!r)throw new Error(`Value "${t}" does not satisfy any of known RGB formats.`);return r.slice(1).reduce((n,o)=>{const i=parseInt(o,10).toString(16);return n+(i.length===1?"0":"")+i},"#")}const a=d(t=>{if(typeof t=="string"||typeof t=="number")return t.toString();throw l()},"string"),q=d(t=>O(a().parse(t)),"rgb");p({req_id:a(),data:t=>t===null?t:a().optional().parse(t)}),p({req_id:a(),result:t=>t,error:a().optional()}),p({slug:a(),status:a()}),p({status:a()}),p({data:a().optional()}),p({theme_params:t=>{const e=q().optional();return Object.entries(N(t)).reduce((r,[n,o])=>(r[n]=e.parse(o),r),{})}}),p({height:f(),width:t=>t==null?window.innerWidth:f().parse(t),is_state_stable:u(),is_expanded:u()}),p({status:a()});const I=d(t=>t instanceof Date?t:new Date(f().parse(t)*1e3),"Date");function C(t,e){return new w(r=>{if(typeof r!="string"&&!(r instanceof URLSearchParams))throw l();const n=typeof r=="string"?new URLSearchParams(r):r;return E(t,o=>{const i=n.get(o);return i===null?void 0:i})},!1,e)}const j=p({id:f(),type:a(),title:a(),photoUrl:{type:a().optional(),from:"photo_url"},username:a().optional()},"Chat").optional(),v=p({addedToAttachmentMenu:{type:u().optional(),from:"added_to_attachment_menu"},allowsWriteToPm:{type:u().optional(),from:"allows_write_to_pm"},firstName:{type:a(),from:"first_name"},id:f(),isBot:{type:u().optional(),from:"is_bot"},isPremium:{type:u().optional(),from:"is_premium"},languageCode:{type:a().optional(),from:"language_code"},lastName:{type:a().optional(),from:"last_name"},photoUrl:{type:a().optional(),from:"photo_url"},username:a().optional()},"User").optional();function H(){return C({authDate:{type:I(),from:"auth_date"},canSendAfter:{type:f().optional(),from:"can_send_after"},chat:j,chatInstance:{type:a().optional(),from:"chat_instance"},chatType:{type:a().optional(),from:"chat_type"},hash:a(),queryId:{type:a().optional(),from:"query_id"},receiver:v,startParam:{type:a().optional(),from:"start_param"},user:v},"InitData")}function L(t){return H().parse(t)}exports.parse=L;exports.sign=A;exports.signData=m;exports.validate=T; //# sourceMappingURL=index.cjs.map