1import{u as Ee,r as n,s as l,j as e,C as f,e as y,bY as T,bZ as E,B as c,n as ee,b_ as G,b$ as D,c0 as O,y as o,a6 as h,c7 as se,a1 as Ge,a2 as De,a3 as A,a4 as z,E as te,G as ae,H as ne,J as re,N as ie,z as Oe,d as F,T as Ae,b as le,c as de,aj as ze,ah as d,h as Fe,c8 as Me}from"./index-0skl7k4W.js";import{T as ke,a as Ie,b as ce,c as u,d as Je,e as x}from"./table-D6NlX-yh.js";import{U as Le}from"./user-plus-C7DDG6_B.js";const oe=["general","food","gifts","gas","entertainment","groceries","clothing","travel","bills","other"];function $e(){const{user:a}=Ee(),[v,ue]=n.useState([]),[r,p]=n.useState(null),[j,xe]=n.useState([]),[m,me]=n.useState([]),[he,M]=n.useState(!0),[b,k]=n.useState(""),[I,J]=n.useState(""),[pe,L]=n.useState(!1),[_,$]=n.useState(""),[je,q]=n.useState(!1),[S,U]=n.useState(""),[B,ge]=n.useState("general"),[H,P]=n.useState(""),[V,R]=n.useState(""),[W,fe]=n.useState(new Date().toISOString().split("T")[0]),[Y,Z]=n.useState(""),[ye,K]=n.useState(!1);n.useEffect(()=>{a&&N()},[a]),n.useEffect(()=>{r&&(Ne(r.id),w(r.id))},[r]);async function N(){M(!0);const{data:s}=await l.from("spending_groups").select("*").order("created_at",{ascending:!1});if(s){const{data:t}=await l.from("spending_group_members").select("group_id").eq("user_id",a.id),i=new Set((t||[]).map(C=>C.group_id)),g=s.filter(C=>i.has(C.id));ue(g),g.length>0&&!r&&p(g[0])}M(!1)}async function Ne(s){const{data:t}=await l.from("spending_group_members").select("*").eq("group_id",s);xe(t||[])}async function w(s){const{data:t}=await l.from("spending_entries").select("*").eq("group_id",s).order("spent_date",{ascending:!1});me(t||[])}async function ve(){var i;if(!b.trim()||!a)return;const{data:s,error:t}=await l.from("spending_groups").insert({name:b.trim(),description:I.trim()||null,created_by:a.id}).select().single();if(t){d.error("Failed to create group");return}await l.from("spending_group_members").insert({group_id:s.id,user_id:a.id,display_name:((i=a.user_metadata)==null?void 0:i.full_name)||a.email,role:"owner"}),k(""),J(""),L(!1),d.success("Group created!"),N(),p(s)}async function be(){var i;if(!_.trim()||!a)return;const{data:s}=await l.from("spending_groups").select("*").eq("invite_code",_.trim().toLowerCase()).single();if(!s){d.error("Invalid invite code");return}const{error:t}=await l.from("spending_group_members").insert({group_id:s.id,user_id:a.id,display_name:((i=a.user_metadata)==null?void 0:i.full_name)||a.email,role:"member"});if(t){t.code==="23505"?d.error("Already in this group"):d.error("Failed to join");return}$(""),q(!1),d.success(`Joined "${s.name}"!`),N(),p(s)}async function _e(){if(!S||!r||!a)return;const{error:s}=await l.from("spending_entries").insert({group_id:r.id,spent_by:a.id,spent_on:Y||null,amount:parseFloat(S),category:B,store_location:H.trim()||null,note:V.trim()||null,spent_date:W});if(s){d.error("Failed to add entry");return}U(""),P(""),R(""),Z(""),K(!1),d.success("Entry added!"),w(r.id)}async function Se(s){await l.from("spending_entries").delete().eq("id",s),d.success("Entry deleted"),r&&w(r.id)}async function we(s){a&&(await l.from("spending_group_members").delete().eq("group_id",s).eq("user_id",a.id),d.success("Left group"),p(null),N())}function Q(s){const t=j.find(i=>i.user_id===s);return(t==null?void 0:t.display_name)||"Unknown"}function Ce(){const s={};return m.forEach(t=>{s[t.spent_by]=(s[t.spent_by]||0)+Number(t.amount)}),s}const X=m.reduce((s,t)=>s+Number(t.amount),0),Te=Ce();return a?e.jsxs("div",{className:"p-4 md:p-6 max-w-5xl mx-auto space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsx("h1",{className:"text-2xl font-bold text-foreground",children:"Shared Spending"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(T,{open:pe,onOpenChange:L,children:[e.jsx(E,{asChild:!0,children:e.jsxs(c,{size:"sm",children:[e.jsx(ee,{className:"h-4 w-4 mr-1"})," New Group"]})}),e.jsxs(G,{children:[e.jsx(D,{children:e.jsx(O,{children:"Create Spending Group"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(o,{children:"Group Name"}),e.jsx(h,{value:b,onChange:s=>k(s.target.value),placeholder:"e.g. Me & Sarah"})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Description (optional)"}
1),e.jsx(h,{value:I,onChange:s=>J(s.target.value),placeholder:"Track our spending"})]}),e.jsx(c,{onClick:ve,className:"w-full",children:"Create Group"})]})]})]}),e.jsxs(T,{open:je,onOpenChange:q,children:[e.jsx(E,{asChild:!0,children:e.jsxs(c,{size:"sm",variant:"outline",children:[e.jsx(Le,{className:"h-4 w-4 mr-1"})," Join Group"]})}),e.jsxs(G,{children:[e.jsx(D,{children:e.jsx(O,{children:"Join a Group"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(o,{children:"Invite Code"}),e.jsx(h,{value:_,onChange:s=>$(s.target.value),placeholder:"Enter invite code"})]}),e.jsx(c,{onClick:be,className:"w-full",children:"Join"})]})]})]})]})]}),v.length===0&&!he?e.jsx(f,{children:e.jsxs(y,{className:"p-8 text-center space-y-2",children:[e.jsx(se,{className:"h-12 w-12 mx-auto text-muted-foreground"}),e.jsx("p",{className:"text-muted-foreground",children:"No groups yet. Create one or join with an invite code!"})]})}):e.jsxs(e.Fragment,{children:[v.length>1&&e.jsx("div",{className:"flex gap-2 flex-wrap",children:v.map(s=>e.jsx(c,{variant:(r==null?void 0:r.id)===s.id?"default":"outline",size:"sm",onClick:()=>p(s),children:s.name},s.id))}),r&&e.jsxs(Ge,{defaultValue:"entries",className:"w-full",children:[e.jsxs(De,{className:"grid w-full grid-cols-3",children:[e.jsx(A,{value:"entries",children:"Spending"}),e.jsx(A,{value:"summary",children:"Summary"}),e.jsx(A,{value:"members",children:"Members"})]}),e.jsxs(z,{value:"entries",className:"space-y-4",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Group total: ",e.jsxs("span",{className:"font-bold text-foreground",children:["$",X.toFixed(2)]})]}),e.jsxs(T,{open:ye,onOpenChange:K,children:[e.jsx(E,{asChild:!0,children:e.jsxs(c,{size:"sm",children:[e.jsx(ee,{className:"h-4 w-4 mr-1"})," Add Entry"]})}),e.jsxs(G,{children:[e.jsx(D,{children:e.jsx(O,{children:"Add Spending Entry"})}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx(o,{children:"Amount ($)"}),e.jsx(h,{type:"number",step:"0.01",value:S,onChange:s=>U(s.target.value),placeholder:"0.00"})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Spent On (who)"}),e.jsxs(te,{value:Y,onValueChange:Z,children:[e.jsx(ae,{children:e.jsx(ne,{placeholder:"Select person"})}),e.jsx(re,{children:j.filter(s=>s.user_id!==a.id).map(s=>e.jsx(ie,{value:s.user_id,children:s.display_name||"Member"},s.user_id))})]})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Category"}),e.jsxs(te,{value:B,onValueChange:ge,children:[e.jsx(ae,{children:e.jsx(ne,{})}),e.jsx(re,{children:oe.map(s=>e.jsx(ie,{value:s,children:s.charAt(0).toUpperCase()+s.slice(1)},s))})]})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Store / Location"}),e.jsx(h,{value:H,onChange:s=>P(s.target.value),placeholder:"Walmart, Amazon, etc."})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Date"}),e.jsx(h,{type:"date",value:W,onChange:s=>fe(s.target.value)})]}),e.jsxs("div",{children:[e.jsx(o,{children:"Note"}),e.jsx(Oe,{value:V,onChange:s=>R(s.target.value),placeholder:"What was it for?",rows:2})]}),e.jsx(c,{onClick:_e,className:"w-full",children:"Add Entry"})]})]})]})]}),m.length===0?e.jsx(f,{children:e.jsx(y,{className:"p-6 text-center text-muted-foreground",children:"No spending entries yet. Add one!"})}):e.jsx("div",{className:"rounded-md border overflow-x-auto",children:e.jsxs(ke,{children:[e.jsx(Ie,{children:e.jsxs(ce,{children:[e.jsx(u,{children:"Date"}),e.jsx(u,{children:"From"}),e.jsx(u,{children:"To"}),e.jsx(u,{children:"Amount"}),e.jsx(u,{children:"Category"}),e.jsx(u,{className:"hidden md:table-cell",children:"Store"}),e.jsx(u,{className:"hidden md:table-cell",children:"Note"}),e.jsx(u,{})]})}),e.jsx(Je,{children:m.map(s=>e.jsxs(ce,{children:[e.jsx(x,{className:"text-xs",children:s.spent_date}),e.jsx(x,{className:"text-xs",children:Q(s.spent_by)}),e.jsx(x,{className:"text-xs",children:s.spent_on?Q(s.spent_on):"â"}),e.jsxs(x,{className:"font-semibold",children:["$",Number(s.amount).toFixed(2)]}),e.jsx(x,{children:e.jsx(F,{variant:"secondary",className:"text-xs capitalize",children:s.category})}),e.jsx(x,{className:"hidden md:table-cell text-xs text-muted-foreground",children:s.store_location||"â"}),e.jsx(x,{className:"hidden md:table-cell text-xs text-muted-foreground",children:s.note||"â"}),e.jsx(x,{children:s.spent_by===a.id&&e.jsx(c,{variant:"ghost",size:"icon",onClick:()=>Se(s.id),children:e.jsx(Ae,{className:"h-4 w-4 text-destructive"})})})]},s.id))})]})})]}),e.jsx(z,{value:"summary",className:"space-y-4",children:e.jsxs(f,{children:[e.jsx(le,{children:e.jsx(de,{className:"text-lg",children:"Spending Summary"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"text-center p-4 rounded-lg bg-primary/10",children:[e.jsx(ze,{className:"h-6 w-6 mx-auto text-primary mb-1"}),e.jsxs("div",{className:"text-2xl font-bold text-foreground",children:["$",X.toFixed(2)]}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Total Spent"})]}),e.jsxs("div",{className:"text-center p-4 rounded-lg bg-secondary/10",children:[e.jsx(se,{className:"h-6 w-6 mx-auto text-secondary mb-1"}),e.jsx("div",{className:"text-2xl font-bold text-foreground",children:j.length}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Members"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground",children:"Per Person"}),j.map(s=>e.jsxs("div",{className:"flex justify-between items-center p-2 rounded bg-muted/50",children:[e.jsx("span",{className:"text-sm",children:s.display_name||"Member"}),e.jsxs("span",{className:"font-semibold",children:["$",(Te[s.user_id]||0).toFixed(2)]})]}
1,s.user_id))]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground",children:"By Category"}),oe.filter(s=>m.some(t=>t.category===s)).map(s=>{const t=m.filter(i=>i.category===s).reduce((i,g)=>i+Number(g.amount),0);return e.jsxs("div",{className:"flex justify-between items-center p-2 rounded bg-muted/50",children:[e.jsx(F,{variant:"outline",className:"capitalize",children:s}),e.jsxs("span",{className:"font-semibold",children:["$",t.toFixed(2)]})]},s)})]})]})]})}),e.jsx(z,{value:"members",className:"space-y-4",children:e.jsxs(f,{children:[e.jsx(le,{children:e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx(de,{className:"text-lg",children:"Members"}),e.jsxs(c,{variant:"outline",size:"sm",onClick:()=>{navigator.clipboard.writeText(r.invite_code),d.success("Invite code copied!")},children:[e.jsx(Fe,{className:"h-4 w-4 mr-1"})," ",r.invite_code]})]})}),e.jsxs(y,{className:"space-y-2",children:[j.map(s=>e.jsxs("div",{className:"flex justify-between items-center p-3 rounded-lg bg-muted/50",children:[e.jsxs("div",{children:[e.jsx("span",{className:"font-medium text-sm",children:s.display_name||"Member"}),e.jsx(F,{variant:"secondary",className:"ml-2 text-xs",children:s.role})]}),s.user_id===a.id&&s.role!=="owner"&&e.jsxs(c,{variant:"ghost",size:"sm",onClick:()=>we(r.id),children:[e.jsx(Me,{className:"h-4 w-4 mr-1"})," Leave"]})]},s.id)),e.jsx("p",{className:"text-xs text-muted-foreground pt-2",children:"Share the invite code above so others can join this group."})]})]})})]})]})]}):e.jsx(f,{className:"max-w-lg mx-auto mt-8",children:e.jsx(y,{className:"p-8 text-center",children:e.jsx("p",{className:"text-muted-foreground",children:"Sign in to use Shared Spending."})})})}function He(){return e.jsx($e,{})}export{He as SharedSpendingPanel};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.