// @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{Dr as t,_n as n,a as r,or as i,qi as a,zi as o}from"./lucide-icons-BdRF0moN.js";import{t as s}from"./jsx-runtime-By8HlURe.js";import{t as c}from"./useLocalStorage-DLDvi0Ge.js";import{o as l,r as u}from"./react-intl-eZirkCBg.js";import{o as d,t as f}from"./useCurrentUser-C6OJJlWo.js";import{t as p}from"./utils-BFSTDHdM.js";import{t as m}from"./useNostrPublish-CrMjnYm4.js";import{n as h}from"./chunk-62JRHF6Z-yJW_vgm4.js";import{i as g,r as _,t as v}from"./popover-Dw5E1auH.js";import{t as y}from"./skeleton-CVls4GW9.js";import{M as b,a as x,j as S,o as C,v as w}from"./blobbi-CLChx8JU.js";import{S as T,b as E,n as D,t as O,x as k}from"./useStatusReaction-L-HlqtEq.js";import{b as A,u as j,x as M}from"./blobbi-shop-items-gin9pdH4.js";import{t as N}from"./BlobbiStageVisual-Cyhy_5-y.js";import{_ as P,h as F}from"./blobbi-action-utils-CeIPzrFd.js";import{i as I,r as L,t as R}from"./useBlobbiUseInventoryItem-B5QuVTwd.js";var z=e(a(),1),B=s(),V={sm:{wrapper:`flex flex-col items-center gap-3 py-6`,icon:`size-10 text-muted-foreground/30`,name:`text-sm font-semibold`,description:`text-xs text-muted-foreground`,button:`flex items-center gap-2 px-4 py-2 rounded-full text-white text-xs font-semibold transition-all hover:-translate-y-0.5 hover:scale-105 active:scale-95`,buttonIcon:`size-3.5`,buttonLabel:e=>`Bring home`,descriptionText:e=>`Out exploring with you`},md:{wrapper:`flex flex-col items-center justify-center gap-6 text-center`,icon:`size-16 text-muted-foreground/30`,name:``,description:`text-muted-foreground text-sm`,button:`flex items-center justify-center gap-2.5 px-8 py-3.5 rounded-full text-white font-semibold transition-all duration-300 ease-out hover:-translate-y-0.5 hover:scale-105 hover:brightness-110 active:scale-95`,buttonIcon:`size-5`,buttonLabel:e=>`Bring ${e} home`,descriptionText:e=>`${e} is out exploring right now.`}};function H({name:e,size:t=`md`,isUpdating:r,onBringHome:a}){let o=V[t];return(0,B.jsxs)(`div`,{className:o.wrapper,children:[(0,B.jsx)(i,{className:o.icon}),t===`sm`&&(0,B.jsx)(`span`,{className:o.name,children:e}),(0,B.jsx)(`p`,{className:o.description,children:o.descriptionText(e)}),(0,B.jsxs)(`button`,{onClick:a,disabled:r,className:p(o.button,r&&`opacity-50 pointer-events-none`),style:{background:`linear-gradient(135deg, #8b5cf6, #ec4899, #f59e0b)`},children:[r?(0,B.jsx)(n,{className:p(o.buttonIcon,`animate-spin`)}):(0,B.jsx)(i,{className:o.buttonIcon}),(0,B.jsx)(`span`,{children:o.buttonLabel(e)})]})]})}var U={hunger:{itemId:`food_apple`,action:`feed`},happiness:{itemId:`toy_ball`,action:`play`},health:{itemId:`med_vitamins`,action:`medicine`},hygiene:{itemId:`hyg_soap`,action:`clean`},energy:`sleep`},W={hunger:`orange`,happiness:`yellow`,health:`green`,hygiene:`blue`,energy:`violet`},G={hunger:`feed`,happiness:`play`,health:`medicine`,hygiene:`clean`};function K(){let e=l(),{user:n}=f(),{companions:r,isLoading:i,updateCompanionEvent:a}=k(void 0,n?.pubkey),{profile:o,updateProfileEvent:s,invalidate:p}=T(),{fetchFreshBlobbiBeforeAction:g}=I(n?.pubkey),{mutateAsync:_}=m(),v=r,O=(0,z.useMemo)(()=>{let e={};for(let t of v)e[t.d]=t;return e},[v]),[j,M]=c(n?.pubkey?w(n.pubkey):`blobbi:selected:d:none`,null),N=(0,z.useMemo)(()=>!v||v.length===0?null:j&&O[j]?O[j]:v[0],[v,O,j]),P=(0,z.useCallback)(async()=>!N||!o?null:g({companion:N,profile:o,updateProfileEvent:s,updateCompanionEvent:a}),[N,o,g,s,a]),{mutateAsync:F,isPending:L}=R({companion:N,profile:o,ensureCanonicalBeforeAction:P,updateCompanionEvent:a,updateProfileEvent:s}),[V,H]=(0,z.useState)(!1),U=(0,z.useCallback)(async()=>{if(!n?.pubkey||!N)return;let t=N.state===`sleeping`,r=t?`active`:`sleeping`;H(!0);try{let n=await P();if(!n)return;let i=Math.floor(Date.now()/1e3),o=A({stage:n.companion.stage,state:n.companion.state,stats:n.companion.stats,lastDecayAt:n.companion.lastDecayAt,now:i}),s=i.toString(),c=D(n.companion)??{},l=S(n.allTags,{state:r,hunger:o.stats.hunger.toString(),happiness:o.stats.happiness.toString(),health:o.stats.health.toString(),hygiene:o.stats.hygiene.toString(),energy:o.stats.energy.toString(),...c,last_interaction:s,last_decay_at:s}),u=n.companion.event,f=await _({kind:x,content:n.content,tags:l,prev:u});a(f),d({title:t?e.formatMessage({id:`widgets.blobbi.wokeUp`,defaultMessage:`Woke up!`}):e.formatMessage({id:`widgets.blobbi.resting`,defaultMessage:`Resting...`})})}catch{d({title:e.formatMessage({id:`widgets.blobbi.actionFailed`,defaultMessage:`Action failed`}),variant:`destructive`})}finally{H(!1)}},[n?.pubkey,N,P,_,a,e]),[W,G]=(0,z.useState)(!1),K=N?o?.currentCompanion===N.d:!1,{companion:J}=E(),Y=N?J?.d===N.d:!1,X=(0,z.useCallback)(async()=>{if(!(!o||!N)){G(!0);try{let t=await P();if(!t)return;let n;n=K?b(t.profileAllTags,{}).filter(e=>e[0]!==`current_companion`):b(t.profileAllTags.filter(e=>e[0]!==`current_companion`),{current_companion:N.d});let r=t.profileEvent,i=await _({kind:C,content:r.content,tags:n,prev:r});s(i),p(),d({title:K?e.formatMessage({id:`widgets.blobbi.companionUnset`,defaultMessage:`Companion unset`}):e.formatMessage({id:`widgets.blobbi.companionSet`,defaultMessage:`Companion set!`}),description:K?e.formatMessage({id:`widgets.blobbi.companionUnsetDesc`,defaultMessage:`{name} is no longer your companion`},{name:N.name}):e.formatMessage({id:`widgets.blobbi.companionSetDesc`,defaultMessage:`{name} is now your companion`},{name:N.name})})}catch{d({title:e.formatMessage({id:`widgets.blobbi.companionUpdateFailed`,defaultMessage:`Failed to update companion`}),variant:`destructive`})}finally{G(!1)}}},[o,N,K,P,_,s,p,e]),Z=L||V;return n?i?(0,B.jsxs)(`div`,{className:`flex flex-col items-center gap-3 py-4`,children:[(0,B.jsx)(y,{className:`size-24 rounded-full`}),(0,B.jsx)(`div`,{className:`flex items-center justify-center gap-1.5 pt-2`,children:Array.from({length:5}).map((e,t)=>(0,B.jsx)(y,{className:`size-9 rounded-full`},t))})]}):N?(0,B.jsx)(q,{companion:N,allCompanions:v,onSelectCompanion:M,onUseItem:F,onRest:U,isActionPending:Z,isCurrentCompanion:K,isActiveFloatingCompanion:Y,isUpdatingCompanion:W,onToggleCompanion:X,currentCompanionD:o?.currentCompanion}):(0,B.jsxs)(h,{to:`/blobbi`,className:`flex flex-col items-center gap-2 py-4 hover:bg-secondary/40 rounded-lg transition-colors`,children:[(0,B.jsx)(`div`,{className:`size-16 rounded-2xl bg-primary/10 flex items-center justify-center`,children:(0,B.jsx)(t,{className:`size-8 text-primary`})}),(0,B.jsx)(`span`,{className:`text-sm font-medium text-primary`,children:(0,B.jsx)(u,{id:`widgets.blobbi.hatch`,defaultMessage:`Hatch your Blobbi`})}),(0,B.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:(0,B.jsx)(u,{id:`widgets.blobbi.hatchDesc`,defaultMessage:`Get your virtual pet companion`})})]}):(0,B.jsxs)(h,{to:`/blobbi`,className:`flex flex-col items-center gap-2 py-4 hover:bg-secondary/40 rounded-lg transition-colors`,children:[(0,B.jsx)(`div`,{className:`size-16 rounded-2xl bg-primary/10 flex items-center justify-center`,children:(0,B.jsx)(t,{className:`size-8 text-primary`})}),(0,B.jsx)(`span`,{className:`text-xs text-muted-foreground`,children:(0,B.jsx)(u,{id:`widgets.blobbi.loginPrompt`,defaultMessage:`Log in to hatch your Blobbi`})})]})}function q({companion:e,allCompanions:t,onSelectCompanion:a,onUseItem:s,onRest:c,isActionPending:d,isCurrentCompanion:f,isActiveFloatingCompanion:m,isUpdatingCompanion:y,onToggleCompanion:b,currentCompanionD:x}){let S=l(),C=j(e)?.stats??{hunger:100,happiness:100,health:100,hygiene:100,energy:100},{recipe:w,recipeLabel:T}=O({stats:C,enabled:e.state!==`sleeping`}),E=e.stage,D=M(E).filter(e=>{if(e===`energy`)return E!==`egg`;let t=G[e];return t?F(E,t):!1}),k=(0,z.useCallback)(async e=>{let t=U[e];if(t)if(t===`sleep`)await c();else try{await s(t)}catch{}},[s,c]),[A,I]=(0,z.useState)(!1),R=t.length>1,V=(0,z.useRef)(null),K=(0,z.useCallback)(e=>{if(V.current&&e&&e.removeEventListener(`wheel`,V.current),!e){V.current=null;return}let t=t=>{t.deltaY!==0&&(t.preventDefault(),t.stopPropagation(),e.scrollBy({left:t.deltaY}))};V.current=t,e.addEventListener(`wheel`,t,{passive:!1})},[]);return m?(0,B.jsx)(H,{name:e.name,size:`sm`,isUpdating:y,onBringHome:b}):(0,B.jsxs)(`div`,{className:`relative flex flex-col items-center gap-3 py-3`,children:[(0,B.jsxs)(`div`,{className:`absolute top-2 right-1 flex flex-col items-center gap-1`,children:[(0,B.jsx)(`button`,{onClick:b,disabled:y||d,className:p(`size-7 rounded-full flex items-center justify-center transition-colors`,f?`text-emerald-500 bg-emerald-500/10 hover:bg-emerald-500/20`:`text-violet-500 bg-violet-500/10 hover:bg-violet-500/20`,(y||d)&&`opacity-40 pointer-events-none`),title:f?S.formatMessage({id:`widgets.blobbi.withYou`,defaultMessage:`With you`}):S.formatMessage({id:`widgets.blobbi.takeAlong`,defaultMessage:`Take along`}),children:y?(0,B.jsx)(n,{className:`size-3.5 animate-spin`}):(0,B.jsx)(i,{className:`size-3.5`})}),R&&(0,B.jsxs)(v,{open:A,onOpenChange:I,children:[(0,B.jsx)(g,{asChild:!0,children:(0,B.jsx)(`button`,{className:`size-7 rounded-full flex items-center justify-center transition-colors text-muted-foreground bg-muted/50 hover:bg-muted hover:text-foreground`,title:S.formatMessage({id:`widgets.blobbi.switch`,defaultMessage:`Switch Blobbi`}),children:(0,B.jsx)(o,{className:`size-3.5`})})}),(0,B.jsxs)(_,{side:`left`,align:`start`,className:`w-auto p-3`,children:[(0,B.jsxs)(`div`,{className:`flex items-center justify-between mb-2`,children:[(0,B.jsx)(`p`,{className:`text-xs font-medium text-muted-foreground`,children:(0,B.jsx)(u,{id:`widgets.blobbi.switch`,defaultMessage:`Switch Blobbi`})}),(0,B.jsx)(`button`,{onClick:()=>I(!1),"aria-label":S.formatMessage({id:`widgets.common.close`,defaultMessage:`Close`}),className:`size-5 rounded-full flex items-center justify-center text-muted-foreground hover:text-foreground hover:bg-muted transition-colors`,children:(0,B.jsx)(r,{className:`size-3`})})]}),(0,B.jsxs)(`div`,{className:`relative max-w-[18rem]`,children:[(0,B.jsx)(`div`,{ref:K,className:`flex items-center gap-3 overflow-x-auto py-1 px-2 scrollbar-thin`,children:t.map(t=>{let n=t.d===e.d,r=t.d===x;return(0,B.jsxs)(`button`,{onClick:()=>{a(t.d),I(!1)},"aria-label":S.formatMessage({id:`widgets.blobbi.switchTo`,defaultMessage:`Switch to {name}`},{name:t.name}),className:p(`flex-shrink-0 flex flex-col items-center gap-1 transition-all duration-200 hover:-translate-y-0.5 hover:scale-105 active:scale-95`,n&&`opacity-50 pointer-events-none`),disabled:n,children:[(0,B.jsxs)(`div`,{className:`relative`,children:[(0,B.jsx)(N,{companion:t,size:`sm`}),r&&(0,B.jsx)(`div`,{className:`absolute -bottom-0.5 -right-0.5 size-4 rounded-full bg-background ring-1 ring-background flex items-center justify-center`,children:(0,B.jsx)(i,{className:`size-2.5 text-emerald-500`})})]}),(0,B.jsx)(`span`,{className:`text-[10px] font-medium text-muted-foreground max-w-[4rem] truncate`,children:t.name})]},t.d)})}),(0,B.jsx)(`div`,{className:`pointer-events-none absolute inset-y-0 right-0 w-6 bg-gradient-to-l from-popover to-transparent`})]})]})]})]}),(0,B.jsx)(h,{to:`/blobbi`,className:`relative hover:scale-105 transition-transform`,children:(0,B.jsx)(N,{companion:e,size:`lg`,animated:!0,lookMode:`follow-pointer`,recipe:w,recipeLabel:T})}),(0,B.jsx)(h,{to:`/blobbi`,className:`text-sm font-semibold hover:text-primary transition-colors`,children:e.name}),(0,B.jsx)(`div`,{className:`flex items-center justify-center gap-1.5 px-2`,children:D.map(e=>{let t=P({stage:E,stat:e,value:C[e]??100});return(0,B.jsx)(L,{stat:e,value:C[e],color:W[e],careState:t.careState,segments:{filled:t.filled,max:t.max},size:`sm`,onClick:()=>k(e),disabled:d},e)})})]})}export{K as BlobbiWidget}; // @license-end