import{a as e,s as t}from"./useAuthor-K0SVHIbr.js";import{m as n}from"./esm-0y5H0J9W.js";import{s as r}from"./utils-1bOINbOx.js";import{t as i}from"./sha256-BgZ4MAII.js";import{n as a,r as o}from"./customEmoji-kpim4Htm.js";function s(e){if(!e)return 0;let t=e.toLowerCase(),n=t.lastIndexOf(`1`);if(n<2)return 0;let r=t.substring(0,n).match(/(\d+)([munp]?)$/);if(!r)return 0;let i=parseInt(r[1],10);if(isNaN(i)||i<=0)return 0;switch(r[2]){case`m`:return i*1e8;case`u`:return i*1e5;case`n`:return i*100;case`p`:return i/10;default:return i*1e11}}var c=100000000000n,l={m:1000n,u:1000000n,n:1000000000n,p:1000000000000n},u=/^ln(bcrt|bc|tbs|tb|sb)(?:(\d+)([munp])?)?$/,d=1,f=13,p=19,m=23,h=6,g=104,_=7,v=3600;function y(e){let t=new Uint8Array(Math.floor(e.length*5/8)),n=0,r=0,i=0;for(let a of e)for(n=n<<5|a,r+=5;r>=8;)r-=8,t[i++]=n>>r&255;return t}function b(e){let t=0;for(let n of e)t=t*32+n;return t}function x(e,t){if(!e)return null;let n=BigInt(e),r=t?l[t]:1n,i=n*c;if(i%r!==0n)throw Error(`Invalid BOLT11 invoice: amount is not a whole number of millisatoshis`);let a=i/r;if(a>BigInt(2**53-1))throw Error(`Invalid BOLT11 invoice: amount is implausibly large`);return Number(a)}function S(e){let t=e.trim().replace(/^lightning:/i,``).toLowerCase();if(!t.startsWith(`ln`))throw Error(`Invalid BOLT11 invoice: missing "ln" prefix`);let{prefix:i,words:a}=n.decode(t,!1),o=u.exec(i);if(!o)throw Error(`Invalid BOLT11 invoice: unrecognized prefix`);let[,s,c,l]=o,S=x(c,l);if(a.length<111)throw Error(`Invalid BOLT11 invoice: truncated`);let C=b(a.slice(0,_)),w=a.slice(_,a.length-g),T={amountMsat:S,network:s,timestamp:C,expiry:v};for(let e=0;e+3<=w.length;){let t=w[e],n=w[e+1]*32+w[e+2],i=e+3,a=i+n;if(a>w.length)throw Error(`Invalid BOLT11 invoice: tagged field overruns the invoice`);let o=w.slice(i,a);switch(e=a,t){case d:n===52&&(T.paymentHash=r(y(o)));break;case m:n===52&&(T.descriptionHash=r(y(o)));break;case p:n===53&&(T.payee=r(y(o)));break;case f:T.description=new TextDecoder().decode(y(o));break;case h:T.expiry=b(o)}}return T}function C(e,t){let n;try{n=S(e)}catch(e){throw Error(`Lightning service returned an unreadable invoice: ${e instanceof Error?e.message:`unknown error`}`)}if(n.amountMsat===null)throw Error(`Lightning service returned an invoice with no amount, which would let it charge any sum. Payment cancelled.`);if(n.amountMsat!==t)throw Error(`Lightning service returned an invoice for ${Math.round(n.amountMsat/1e3)} sats, but ${Math.round(t/1e3)} sats was requested. Payment cancelled.`);return{...n,amountMsat:n.amountMsat}}function w(e,t){if(!e.descriptionHash)return!0;let n=new TextEncoder;return t.some(t=>r(i(n.encode(t)))===e.descriptionHash)}function T(e){let t=e.tags.find(([e])=>e===`amount`);if(t?.[1]){let e=parseInt(t[1],10);if(!isNaN(e)&&e>0)return e}let n=e.tags.find(([e])=>e===`description`);if(n?.[1])try{let e=JSON.parse(n[1]).tags?.find(([e])=>e===`amount`);if(e?.[1]){let t=parseInt(e[1],10);if(!isNaN(t)&&t>0)return t}}catch{}let r=e.tags.find(([e])=>e===`bolt11`);if(r?.[1]){let e=s(r[1]);if(e>0)return e}return 0}function E(e){let t=e.tags.find(([e])=>e===`P`);if(t?.[1])return t[1];let n=e.tags.find(([e])=>e===`description`);if(n?.[1])try{let e=JSON.parse(n[1]);if(e.pubkey)return e.pubkey}catch{}return``}function D(e){let t=e.tags.find(([e])=>e===`description`);if(t?.[1])try{return JSON.parse(t[1]).content||``}catch{}return``}function O(n){let{nostr:r}=e();return t({queryKey:[`event-interactions`,n??``],queryFn:async({signal:e})=>{if(!n)return{reposts:[],quotes:[],reactions:[],zaps:[]};let t=AbortSignal.timeout(5e3),i=AbortSignal.any([e,t]),s=await r.query([{kinds:[6,16,7,9735],"#e":[n],limit:50},{kinds:[1],"#q":[n],limit:20}],{signal:i}),c=s.filter(e=>e.kind!==1||e.tags.some(([e,t])=>e===`e`&&t===n)),l=s.filter(e=>e.kind===1&&e.tags.some(([e,t])=>e===`q`&&t===n)),u=[],d=[],f=[],p=[];for(let e of c)switch(e.kind){case 6:case 16:u.push({eventId:e.id,pubkey:e.pubkey,createdAt:e.created_at});break;case 7:{let t=e.content.trim(),n=t===`+`||t===``?`👍`:t,r=o(n),i=r?a(n,e.tags):void 0;if(r&&!i)break;f.push({eventId:e.id,pubkey:e.pubkey,emoji:n,emojiUrl:i,createdAt:e.created_at});break}case 9735:{let t=T(e),n=E(e);t>0&&n&&p.push({eventId:e.id,senderPubkey:n,amountSats:Math.floor(t/1e3),message:D(e),createdAt:e.created_at});break}}for(let e of l)d.push({pubkey:e.pubkey,eventId:e.id,content:e.content,createdAt:e.created_at});return u.sort((e,t)=>t.createdAt-e.createdAt),d.sort((e,t)=>t.createdAt-e.createdAt),f.sort((e,t)=>t.createdAt-e.createdAt),p.sort((e,t)=>t.amountSats-e.amountSats),{reposts:u,quotes:d,reactions:f,zaps:p}},enabled:!!n,staleTime:6e4})}export{C as a,O as i,D as n,w as o,E as r,T as t};