// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0-or-later
// @source: https://gitlab.com/soapbox-pub/ditto/-/tree/f0a3877c
import{r as e}from"./rolldown-runtime-QTnfLwEv.js";import{Wn as t,qi as n}from"./lucide-icons-BdRF0moN.js";import{t as r}from"./jsx-runtime-By8HlURe.js";import{h as i}from"./useQuery-rCAlChE8.js";import{o as a,t as o}from"./useCurrentUser-C6OJJlWo.js";import{t as s}from"./utils-BFSTDHdM.js";import{t as c}from"./useNostrPublish-CrMjnYm4.js";import{i as l,r as u,t as d}from"./popover-Dw5E1auH.js";import{E as f,c as p}from"./blobbi-CLChx8JU.js";import{a as m,l as h,n as g,p as ee,t as _}from"./blobbi-shop-items-gin9pdH4.js";import{f as v,m as y,n as b,p as x,t as S}from"./blobbi-action-utils-CeIPzrFd.js";import{t as C}from"./useBlobbiInteractions-BR0C9tjk.js";import{t as w}from"./ItemCarousel-DJoTWUZ6.js";var T=e(n(),1),E=r(),D=e=>g(e)?.effect,O=`blobbi-view`,k=[{inventory:`feed`,interaction:`feed`},{inventory:`play`,interaction:`play`},{inventory:`clean`,interaction:`clean`},{inventory:`medicine`,interaction:`medicate`},{inventory:`boost`,interaction:`boost`}],te={feed:`hunger`,play:`happiness`,clean:`hygiene`,medicine:`health`,boost:`energy`},A=1200,j={step:`actions`};function M({event:e,source:n=O,onInteractionSuccess:r,companion:g,className:M}){let{user:N}=o(),{mutateAsync:P}=c(),F=i(),I=(0,T.useMemo)(()=>g===void 0?f(e):g,[e,g]),{interactions:L,isLoading:R,isError:z}=C(I??null),B=!R&&!z?L.length:0,V=(0,T.useMemo)(()=>{if(!I)return;let e=L.length>0?L:void 0;return h(I,void 0,e,D).stats},[I,L]),H=(0,T.useMemo)(()=>!I||!V?[]:k.filter(({inventory:e})=>{if(I.stage===`egg`&&e===`boost`||I.stage===`egg`&&e===`feed`||I.stage===`egg`&&e===`play`)return!1;let t=te[e];return(V[t]??100)<m.warning}),[I,V]),[U,W]=(0,T.useState)(!1),[G,K]=(0,T.useState)(j);(0,T.useEffect)(()=>{G.step===`carousel`&&(H.some(e=>e.inventory===G.action)||K(j))},[G,H]);let q=(0,T.useRef)(null),J=(0,T.useRef)(null);(0,T.useEffect)(()=>()=>{J.current&&clearTimeout(J.current)},[]);let Y=(0,T.useCallback)(e=>{W(e),e||(K(j),q.current=null,J.current&&=(clearTimeout(J.current),null))},[]),X=(0,T.useCallback)(e=>{J.current&&clearTimeout(J.current),J.current=setTimeout(()=>{K({step:`carousel`,action:e})},A)},[]),Z=(0,T.useCallback)(e=>{K({step:`carousel`,action:e})},[]),ne=(0,T.useCallback)(()=>{K(j)},[]),re=(0,T.useCallback)(async e=>{if(!I||!N)return;let t=G.step===`carousel`?G.action:void 0,i=k.find(e=>e.inventory===t);if(!i)return;K({step:`pending`,action:i.inventory,itemId:e}),q.current=e;let o=ee({ownerPubkey:I.event.pubkey,blobbiDTag:I.d,action:i.interaction,source:n,itemId:e});try{await P(o);let e=p(I.event.pubkey,I.d);F.invalidateQueries({queryKey:[`blobbi-interactions`,e]}),F.invalidateQueries({queryKey:[`blobbi-activity-history`,e]}),K({step:`success`,action:i.inventory}),r?.(i.inventory),X(i.inventory)}catch(e){a({title:`Interaction failed`,description:e instanceof Error?e.message:`Unknown error`,variant:`destructive`}),K({step:`carousel`,action:i.inventory})}},[I,N,G,P,F,X,n,r]),Q=G.step===`carousel`||G.step===`pending`?G.action:void 0,$=(0,T.useMemo)(()=>{if(!Q||!I)return[];let e=b[Q],t=I.stage===`egg`,n=[];for(let r of _())r.type===e&&(r.id===`med_shell_repair`&&!t||t&&(Q===`medicine`&&!y(r.effect)||Q===`clean`&&!x(r.effect)&&!v(r.effect))||n.push({id:r.id,icon:(0,E.jsx)(`span`,{children:r.icon}),label:r.name}));return n},[Q,I]);return!N||!I?null:(0,E.jsxs)(d,{open:U,onOpenChange:Y,children:[(0,E.jsx)(l,{asChild:!0,children:(0,E.jsxs)(`button`,{type:`button`,className:s(`flex items-center gap-1 sm:gap-1.5 p-1.5 sm:p-2 rounded-full transition-colors`,U?`text-pink-500 bg-pink-500/10`:`text-muted-foreground hover:text-pink-500 hover:bg-pink-500/10`,M),title:`Interact with Blobbi`,onClick:e=>e.stopPropagation(),children:[(0,E.jsx)(t,{className:`size-[18px] sm:size-5`}),B>0&&(0,E.jsx)(`span`,{className:`text-sm tabular-nums`,children:B})]})}),(0,E.jsxs)(u,{className:`w-auto p-2 max-w-[17rem]`,side:`top`,align:`center`,onClick:e=>e.stopPropagation(),onOpenAutoFocus:e=>e.preventDefault(),children:[G.step===`actions`&&(H.length===0?(0,E.jsx)(`p`,{className:`text-xs text-muted-foreground text-center py-3`,children:`All good! No needs right now.`}):(0,E.jsx)(`div`,{className:`grid grid-cols-2 gap-1.5`,children:H.map(({inventory:e})=>{let t=S[e];return(0,E.jsxs)(`button`,{type:`button`,onClick:()=>Z(e),className:s(`flex items-center gap-1.5 px-3 py-2 rounded-lg`,`text-sm font-medium transition-all duration-150`,`bg-muted/50 hover:bg-muted active:scale-95`),children:[(0,E.jsx)(`span`,{className:`text-base leading-none`,children:t.icon}),(0,E.jsx)(`span`,{children:t.label})]},e)})})),(G.step===`carousel`||G.step===`pending`)&&(0,E.jsxs)(`div`,{className:`flex flex-col items-center gap-2`,children:[(0,E.jsxs)(`div`,{className:`flex items-center gap-2 w-full`,children:[(0,E.jsx)(`button`,{type:`button`,onClick:ne,disabled:G.step===`pending`,className:`text-xs text-muted-foreground hover:text-foreground transition-colors`,children:`← Back`}),(0,E.jsxs)(`span`,{className:`text-sm font-medium ml-auto`,children:[Q&&S[Q].icon,` `,Q&&S[Q].label]})]}),$.length>0?(0,E.jsx)(w,{items:$,onUse:re,activeItemId:G.step===`pending`?G.itemId:null,disabled:G.step===`pending`,initialItemId:q.current}):(0,E.jsx)(`p`,{className:`text-xs text-muted-foreground text-center py-3`,children:`No items available.`})]}),G.step===`success`&&(0,E.jsxs)(`div`,{className:`flex items-center justify-center gap-1.5 py-2`,children:[(0,E.jsx)(`span`,{className:`text-base`,children:S[G.action].icon}),(0,E.jsx)(`span`,{className:`text-sm font-medium text-emerald-600 dark:text-emerald-400`,children:`Sent!`})]})]})]})}export{M as BlobbiSocialActions};
// @license-end
