import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{En as t,Tt as n,k as r,ki as i}from"./lucide-icons-BjbcRgLk.js";import{t as a}from"./jsx-runtime-By8HlURe.js";import{t as o}from"./dist-DPmmj29U.js";import{h as s}from"./useQuery-CQCUTxAw.js";import{s as c,t as l}from"./useCurrentUser-boOMG5Xx.js";import{t as u}from"./utils-BFSTDHdM.js";import{t as d}from"./useNostrPublish-D1pQ9KV-.js";import{n as f}from"./appRelays-Clm5NPIe.js";import{r as p}from"./customEmoji-kpim4Htm.js";import{i as m}from"./CustomEmoji-I2Dzbi7x.js";import{n as h,r as g}from"./haptics-Cp_uF-bV.js";import{i as _,r as v,t as y}from"./popover-CpFQm40Y.js";import{s as b}from"./ComposeDialogContent-DiIapF7b.js";import{A as x,d as S,m as C}from"./EmbeddedPost-DITW2flc.js";import{p as w}from"./extraKinds-CF0LI2P1.js";import{t as T}from"./formatNumber-D1ols1S-.js";import{n as E}from"./NoteMoreMenu-3p8z2oYh.js";import{n as D,t as O}from"./useRepostStatus-B31cmi_e.js";var k=e(i(),1),A=a(),j=24,M=8,N=Array.from({length:M},(e,t)=>{let n=t/M*Math.PI*2-Math.PI/2+Math.PI/M;return{x:Math.cos(n)*j,y:Math.sin(n)*j,color:t%2==0?`#ec4899`:`#f9a8d4`}}),P=800;function F({eventId:e,eventPubkey:n,eventKind:r,reactedEvent:i,reactionCount:a=0,className:c,filledHeart:f=!1}){let{user:b}=l(),{nostr:S}=o(),{mutate:w}=d(),E=s(),[O,j]=(0,k.useState)(!1),M=(0,k.useRef)(null),F=(0,k.useRef)(!1),I=(0,k.useRef)(!1),L=D(e),R=!!L,[z,B]=(0,k.useState)(null),V=(0,k.useCallback)(e=>{window.matchMedia(`(prefers-reduced-motion: reduce)`).matches||B({emoji:e&&!p(e)?e:void 0})},[]);(0,k.useEffect)(()=>{if(!z)return;let e=setTimeout(()=>B(null),P);return()=>clearTimeout(e)},[z]);let H=(0,k.useCallback)(async t=>{if(t.stopPropagation(),!b)return;let n=await S.query([{kinds:[7],authors:[b.pubkey],"#e":[e],limit:1}]);if(n.length===0)return;let r=n[0].id,i=E.getQueryData([`user-reaction`,e]),a=E.getQueryData([`event-stats`,e]);E.setQueryData([`user-reaction`,e],null),a&&E.setQueryData([`event-stats`,e],{...a,reactions:Math.max(0,a.reactions-1)}),w({kind:5,content:``,tags:[[`e`,r],[`k`,`7`]]},{onSuccess:()=>{setTimeout(()=>{E.invalidateQueries({queryKey:[`event-stats`,e]}),E.invalidateQueries({queryKey:[`event-interactions`,e]}),E.invalidateQueries({queryKey:[`user-reaction`,e]})},3e3)},onError:()=>{E.setQueryData([`user-reaction`,e],i),a&&E.setQueryData([`event-stats`,e],a)}})},[b,S,e,w,E]),U=(0,k.useCallback)(()=>{b&&(R||F.current||(M.current&&=(clearTimeout(M.current),null),j(!0)))},[b,R]),W=(0,k.useCallback)(()=>{I.current||(M.current=setTimeout(()=>{j(!1)},150))},[]);return(0,A.jsxs)(y,{open:O,onOpenChange:e=>{e&&F.current||(e||(I.current=!1),j(e))},children:[(0,A.jsx)(_,{asChild:!0,children:(0,A.jsxs)(`button`,{className:u(`flex items-center gap-1.5 p-2 rounded-full transition-colors focus:outline-none`,`text-muted-foreground hover:text-pink-500 hover:bg-pink-500/10`,c,R&&`text-pink-500`),title:`React`,onClick:e=>{if(e.stopPropagation(),b){if(R){h(),H(e);return}F.current||j(e=>!e)}},onDoubleClick:t=>{if(t.stopPropagation(),!b||R)return;g(),V(`❤️`),j(!1);let a=E.getQueryData([`event-stats`,e]);E.setQueryData([`user-reaction`,e],{content:`❤️`}),a&&E.setQueryData([`event-stats`,e],{...a,reactions:a.reactions+1}),w({kind:7,content:`❤️`,tags:[[`e`,e],[`p`,n],[`k`,String(r)]]},{onSuccess:()=>{i&&x(S,i),setTimeout(()=>{E.invalidateQueries({queryKey:[`event-stats`,e]}),E.invalidateQueries({queryKey:[`event-interactions`,e]}),E.invalidateQueries({queryKey:[`user-reaction`,e]})},3e3)},onError:()=>{E.setQueryData([`user-reaction`,e],null),a&&E.setQueryData([`event-stats`,e],a)}})},onMouseEnter:U,onMouseLeave:W,children:[(0,A.jsxs)(`span`,{className:`relative flex items-center justify-center`,children:[(0,A.jsx)(`span`,{className:u(`flex items-center justify-center`,z&&`motion-safe:animate-reaction-pop`),children:f?(0,A.jsx)(t,{className:`size-6`,fill:R?`currentColor`:`none`}):R&&L?(0,A.jsx)(m,{emoji:L,className:`h-5 w-5 object-contain leading-none translate-y-px`}):(0,A.jsx)(t,{className:`size-5`})}),z&&(0,A.jsxs)(`span`,{"aria-hidden":!0,className:`pointer-events-none absolute inset-0 flex items-center justify-center motion-reduce:hidden`,children:[(0,A.jsx)(`span`,{className:`absolute size-6 rounded-full border-2 border-pink-500/60 animate-reaction-halo`}),N.map((e,t)=>(0,A.jsx)(`span`,{className:u(`absolute animate-reaction-spark select-none`,!z.emoji&&`size-1.5 rounded-full`),style:{fontSize:z.emoji?12:void 0,lineHeight:z.emoji?1:void 0,backgroundColor:z.emoji?void 0:e.color,"--spark-x":`${e.x}px`,"--spark-y":`${e.y}px`},children:z.emoji},t))]})]}),a>0&&(0,A.jsx)(`span`,{className:u(`text-sm tabular-nums`,R&&`text-pink-500`),children:T(a)})]})}),(0,A.jsx)(v,{className:`w-auto p-0 border-0 bg-transparent shadow-none`,side:`top`,align:`start`,onClick:e=>e.stopPropagation(),onOpenAutoFocus:e=>e.preventDefault(),onMouseEnter:U,onMouseLeave:W,children:(0,A.jsx)(C,{eventId:e,eventPubkey:n,eventKind:r,reactedEvent:i,onReacted:V,onExpandChange:e=>{I.current=e},onClose:()=>{I.current=!1,F.current=!0,j(!1),setTimeout(()=>{F.current=!1},300)}})})]})}function I({event:e,children:t}){let[i,a]=(0,k.useState)(!1),[u,p]=(0,k.useState)(!1),{user:m}=l(),{nostr:g}=o(),{mutate:C}=d(),{mutate:T}=E(),D=O(e.id),j=s(),{toast:M}=c(),N=!!D,P=()=>{if(!m){M({title:`Please log in to repost`,variant:`destructive`});return}h();let t=j.getQueryData([`event-stats`,e.id]);t&&j.setQueryData([`event-stats`,e.id],{...t,reposts:t.reposts+1}),j.setQueryData([`user-repost`,e.id],`optimistic`);let n=b(e.kind),r=[[`e`,e.id,f],[`p`,e.pubkey]];if(n===16&&(r.push([`k`,String(e.kind)]),e.kind>=3e4&&e.kind<4e4)){let t=e.tags.find(([e])=>e===`d`)?.[1]??``;r.push([`a`,`${e.kind}:${e.pubkey}:${t}`,f])}C({kind:n,content:``,created_at:Math.floor(Date.now()/1e3),tags:r},{onSuccess:()=>{M({title:`Reposted!`}),a(!1),x(g,e),setTimeout(()=>{j.invalidateQueries({queryKey:[`event-stats`,e.id]}),j.invalidateQueries({queryKey:[`event-interactions`,e.id]}),j.invalidateQueries({queryKey:[`user-repost`,e.id]})},3e3)},onError:()=>{M({title:`Failed to repost`,variant:`destructive`}),t&&j.setQueryData([`event-stats`,e.id],t),j.setQueryData([`user-repost`,e.id],null)}})},F=()=>{if(!m||!D)return;h();let t=j.getQueryData([`event-stats`,e.id]);t&&j.setQueryData([`event-stats`,e.id],{...t,reposts:Math.max(0,t.reposts-1)});let n=j.getQueryData([`user-repost`,e.id]);j.setQueryData([`user-repost`,e.id],null),T({eventId:D,eventKind:b(e.kind)},{onSuccess:()=>{M({title:`Repost removed`}),a(!1),setTimeout(()=>{j.invalidateQueries({queryKey:[`event-stats`,e.id]}),j.invalidateQueries({queryKey:[`event-interactions`,e.id]}),j.invalidateQueries({queryKey:[`user-repost`,e.id]})},3e3)},onError:()=>{M({title:`Failed to remove repost`,variant:`destructive`}),t&&j.setQueryData([`event-stats`,e.id],t),j.setQueryData([`user-repost`,e.id],n)}})},I=()=>{a(!1),p(!0)},L=(0,A.jsxs)(`div`,{className:`w-full`,children:[N?(0,A.jsxs)(`button`,{onClick:e=>{e.stopPropagation(),F()},className:`flex items-center gap-3 w-full px-4 py-3 text-[15px] text-accent hover:bg-secondary/60 transition-colors`,children:[(0,A.jsx)(r,{className:`size-5`}),(0,A.jsx)(`span`,{children:`Undo repost`})]}):(0,A.jsxs)(`button`,{onClick:e=>{e.stopPropagation(),P()},className:`flex items-center gap-3 w-full px-4 py-3 text-[15px] text-foreground hover:bg-secondary/60 transition-colors`,children:[(0,A.jsx)(w,{className:`size-5`}),(0,A.jsx)(`span`,{children:`Repost`})]}),(0,A.jsxs)(`button`,{onClick:e=>{e.stopPropagation(),I()},className:`flex items-center gap-3 w-full px-4 py-3 text-[15px] text-foreground hover:bg-secondary/60 transition-colors`,children:[(0,A.jsx)(n,{className:`size-5`}),(0,A.jsx)(`span`,{children:`Quote post`})]})]});return(0,A.jsxs)(A.Fragment,{children:[(0,A.jsxs)(y,{open:i,onOpenChange:a,children:[(0,A.jsx)(_,{asChild:!0,onClick:e=>e.stopPropagation(),children:typeof t==`function`?t(N):t}),(0,A.jsx)(v,{className:`w-48 p-0 rounded-xl overflow-hidden`,align:`start`,side:`top`,onClick:e=>e.stopPropagation(),children:L})]}),(0,A.jsx)(S,{quotedEvent:e,open:u,onOpenChange:p})]})}export{F as n,I as t};