import{r as i,j as t,bi as k,bj as V,t as w,d as D}from"./vendor-laZsiqLO.js";import{B as _}from"./button-B8f8jW4H.js";import{c as n}from"./utils-RgT4yCq8.js";import{u as S}from"./useTranslation-BW0o-tkY.js";function T({post:o,auth:j,className:N}){const{t:p}=S(),[c,v]=i.useState(o.upvotes_count||0),[f,m]=i.useState(o.downvotes_count||0),[s,a]=i.useState(o.user_vote||null),[x,b]=i.useState(!1),g=async(e,r)=>{if(e.preventDefault(),e.stopPropagation(),!j?.user){w.info(p("_login_required_vote","Please log in to vote on articles."));return}if(x)return;const h=s;let u=c,l=f;s===r?(a(null),r==="upvote"?u--:l--):(a(r),r==="upvote"?(u++,h==="downvote"&&l--):(l++,h==="upvote"&&u--)),v(u),m(l),b(!0);try{const d=await D.post(`/api/posts/${o.id}/vote`,{type:r});v(d.data.upvotes_count),m(d.data.downvotes_count),a(d.data.user_vote)}catch{w.error(p("_vote_failed","Failed to submit vote.")),v(o.upvotes_count),m(o.downvotes_count),a(o.user_vote)}finally{b(!1)}};return t.jsxs("div",{className:n("flex items-center bg-muted/40 border border-border rounded-full p-0.5",N),onClick:e=>{e.preventDefault(),e.stopPropagation()},children:[t.jsxs(_,{variant:"ghost",size:"sm",onClick:e=>g(e,"upvote"),disabled:x,className:n("rounded-full h-8 px-3 gap-1.5 transition-colors",s==="upvote"?"text-blue-600 bg-blue-100 hover:bg-blue-200 dark:bg-blue-900/30 dark:text-blue-400":"text-muted-foreground hover:text-foreground"),children:[t.jsx(k,{className:n("w-4 h-4",s==="upvote"&&"fill-current")}),t.jsx("span",{className:"text-xs font-semibold",children:c>0?c:""})]}),t.jsx("div",{className:"w-[1px] h-4 bg-border mx-0.5"}),t.jsxs(_,{variant:"ghost",size:"sm",onClick:e=>g(e,"downvote"),disabled:x,className:n("rounded-full h-8 px-3 gap-1.5 transition-colors",s==="downvote"?"text-red-600 bg-red-100 hover:bg-red-200 dark:bg-red-900/30 dark:text-red-400":"text-muted-foreground hover:text-foreground"),children:[t.jsx(V,{className:n("w-4 h-4 mt-1",s==="downvote"&&"fill-current")}),t.jsx("span",{className:"text-xs font-semibold",children:f>0?f:""})]})]})}export{T as V};
