1import{g2 as Oe,p as Pe,cd as $e,bQ as gt,n as He,q as de,T as ue,r as n,cf as xt,ap as se,j as e,cl as ht,bC as Ue,g3 as ft,cO as Ye,d2 as ye,a3 as bt,bG as me,cC as Ve,g4 as yt,cm as Ct,cn as vt,bS as Fe,co as jt,cp as _t,cq as Nt,cs as wt,cu as At,ct as Tt,cA as Mt,a4 as U,ai as K,a as Et,g5 as It,o as Dt,cT as Rt,cU as St,B as kt,g6 as Ot,bW as ze,v as qe,C as Ft,I as fe,g7 as zt,g8 as We,F as qt,_ as Gt,$ as be,a0 as Lt,c1 as Bt,i as Pt,cB as $t,g9 as Ht,a9 as Qe,bc as Ut,ck as Yt,bT as Vt,H as Wt,O as Qt,ga as Kt,fZ as Xt,d3 as Ge,f9 as Zt,f8 as Jt,cX as es,ac as Le,d0 as ts,c as ss}from"./index-B0X-vWe1.js";import{L as as,b as os}from"./ListSearchInput-D7O0ig3X.js";import{P as ns}from"./PageHeader-CvfFVIhQ.js";import{u as rs,C as is}from"./useCardCounting-Crob6rbW.js";import{u as ls}from"./useInitialMonthFallback-xOuG42Aw.js";import"./month-bills-BZ4JAgdd.js";import"./bill-cycle-status-goElCCfB.js";try{let a=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{},d=new a.Error().stack;d&&(a._sentryDebugIds=a._sentryDebugIds||{},a._sentryDebugIds[d]="b433ee5b-616b-445e-a121-6181e2941844",a._sentryDebugIdIdentifier="sentry-dbid-b433ee5b-616b-445e-a121-6181e2941844")}catch{}const cs="#6b7280";function Ke(a){return a!=null&&a.iconColorHex?`#${a.iconColorHex}`:cs}function Xe(a){return Oe[a??"Tag"]??Oe.Tag}function ce({icon:a,label:d,danger:i=!1,disabled:D=!1,onClick:p}){return e.jsxs("button",{type:"button",onClick:p,disabled:D,className:U("flex w-full cursor-pointer items-center gap-3 border-b border-border py-3.5 text-left transition-opacity last:border-b-0 hover:opacity-80",D&&"opacity-50"),children:[e.jsx("span",{className:U("inline-flex size-8 flex-none items-center justify-center rounded-[9px] border border-border",i?"bg-destructive-soft":"bg-surface-sunken"),children:e.jsx(a,{size:15,className:i?"text-destructive":"text-foreground-muted","aria-hidden":!0})}),e.jsx("span",{className:U("flex-1 text-[13px] font-semibold",i?"text-destructive":"text-foreground"),children:d}),e.jsx(me,{size:15,className:"text-foreground-subtle","aria-hidden":!0})]})}function ds({open:a,onOpenChange:d,category:i,iconNameById:D,onSelectCategory:p,onViewTransactions:R,onEdit:Y,onCreateSubCategory:S}){var B,Q,X;const{localUser:k}=Pe(),{guardWrite:l}=$e(),A=gt(),{allCategories:O}=He(),g=de({tableName:ue.ARCHIVED_CATEGORY}),[L,j]=n.useState(!1),[x,F]=n.useState(!1);xt(L&&!x,()=>{j(!1),W()});const _=!((B=i._raw)!=null&&B.user_id),N=!_,M=((Q=i._raw)==null?void 0:Q.category_id)??null,h=n.useMemo(()=>M?(O??[]).find(u=>u.id===M)??null:null,[O,M]),y=n.useMemo(()=>(Array.isArray(g)?g:[]).find(u=>{var f;return((f=u._raw)==null?void 0:f.category_id)===i.id})??null,[g,i]),E=!!y,V=n.useMemo(()=>{const u=new Set((Array.isArray(g)?g:[]).map(f=>{var q;return(q=f._raw)==null?void 0:q.category_id}));return(O??[]).filter(f=>{var q;return((q=f._raw)==null?void 0:q.category_id)===i.id&&!u.has(f.id)}).sort((f,q)=>f.title.localeCompare(q.title))},[O,g,i]),ae=Ke(i),z=Xe(D.get(((X=i._raw)==null?void 0:X.icon_id)??"")),W=async()=>{if(!(!k||x)){F(!0);try{E&&y?(await k.unarchiveCategory(y),se.success(`"${i.title}" voltou pra lista.`)):(await k.archiveCategory(i),se.success(`"${i.title}" foi arquivada.`)),d(!1)}catch(u){const f=u instanceof Error?u.message:"Não foi possÃvel arquivar essa categoria.";se.error(f)}finally{F(!1)}}};return e.jsxs(e.Fragment,{children:[e.jsxs(ht,{open:a,onOpenChange:d,width:420,title:h?e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx("button",{type:"button","aria-label":`Voltar para ${h.title}`,onClick:()=>{K("tick"),p(h)},className:"-ml-1.5 flex size-[26px] flex-none cursor-pointer items-center justify-center rounded-sm text-foreground-muted transition-colors hover:bg-surface-sunken",children:e.jsx(yt,{size:16})}),"Subcategoria"]}):"Categoria",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(Ue,{hue:ae,size:40,children:e.jsx(z,{size:18})}),e.jsxs("div",{className:"flex min-w-0 flex-1 flex-col",children:[e.jsx("span",{className:"truncate text-[19px] leading-tight font-extrabold",children:i.title}),e.jsx("span",{className:"mt-0.5 text-[12px] font-medium text-foreground-muted",children:_?"Categoria padrão do sistema":"Categoria criada por você"})]})]}),e.jsxs("div",{className:"rounded-[14px] border border-border bg-surface-card px-4 py-0.5",children:[e.jsx(ce,{icon:ft,label:"Ver transações",onClick:()=>R(i)}),N?e.jsx(ce,{icon:Ye,label:"Editar",onClick:l(()=>Y(i))}):null,_?E?e.jsx(ce,{icon:ye,label:"Restaurar",disabled:x,onClick:l(()=>void W())}):e.jsx(ce,{icon:ye,label:"Arquivar",danger:!0,disabled:x,onClick:l(()=>j(!0))}):null]}),M?null:e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx(bt,{className:"px-1",children:"Subcategorias"}),e.jsxs("div",{className:"rounded-[14px] border border-border bg-surface-card px-4 py-0.5",children:[V.map(u=>e.jsxs("button",{type:"button",onClick:()=>{K("tick"),p(u)},className:"flex w-full cursor-pointer items-center gap-2.5 border-b border-border py-3 text-left transition-opacity last:border-b-0 hover:opacity-80",children:[e.jsx("span",{className:"size-[5px] flex-none rounded-full",style:{backgroundColor:ae}}),e.jsx("span",{className:"flex-1 truncate text-[13px] leading-tight font-medium text-foreground-muted",children:u.title}),e.jsx(me,{size:13,className:"flex-none text-foreground-subtle","aria-hidden":!0})]},u.id)),e.jsxs("button",{type:"button",onClick:l(()=>S(i)),className:"flex w-full cursor-pointer items-center gap-2.5 border-b border-border py-3 text-left transition-opacity last:border-b-0 hover:opacity-80",children:[e.jsx(Ve,{size:15,className:"flex-none text-primary","aria-hidden":!0}),e.jsx("span",{className:"flex-1 text-[13px] leading-tight font-semibold text-primary",children:"Nova subcategoria"})]})]})]})]}),e.jsx(Ct,{open:L,onOpenChange:j,children:e.jsxs(vt,{children:[A?null:e.jsx(Fe,{className:"absolute top-4 right-4",children:"esc"}),e.jsxs(jt,{children:[e.jsx(_t,{children:"Arquivar categoria?"}),e.jsx(Nt,{className:"whitespace-pre-line",children:`Você tem certeza que deseja arquivar a categoria "${i.title}"? 2 3Esta categoria será ocultada das suas listas, mas você poderá restaurá-la a qualquer momento através do botão de arquivos na tela de categorias.`})]}),e.jsxs(wt,{children:[e.jsx(At,{children:"Cancelar"}),e.jsxs(Tt,{type:"button",variant:"destructive",onClick:()=>{j(!1),W()}
3,children:["Arquivar",e.jsx(Fe,{"aria-hidden":!0,className:"hidden border-white/20 bg-white/15 text-white pointer-fine:inline-flex",children:Mt})]})]})]})})]})}const te=4,Be=new Set([Ge.TRANSFER_CATEGORY_ID,Ge.CREDIT_CARD_CATEGORY_ID]);function us(){const a=Et({from:"/categories"}),d=It({from:"/categories"}),{localUser:i}=Pe(),{moneyVisibility:D}=Dt(),{allCategories:p,filterTransactions:R,dataVersion:Y}=He(),S=rs(),k=new Date,l=d.month??k.getMonth()+1,A=d.year??k.getFullYear(),O=n.useCallback(t=>{a({search:s=>({...s,month:t.month,year:t.year}),replace:!0})},[a]);Rt({month:l,year:A,onChange:O}),St(l,A,te-1+is);const[g,L]=n.useState(d.q??""),j=n.useRef(!1);n.useEffect(()=>{if(!j.current)return;const t=setTimeout(()=>{j.current=!1,a({search:s=>({...s,q:g.trim()||void 0}),replace:!0})},as);return()=>clearTimeout(t)},[g,a]),n.useEffect(()=>{j.current||L(d.q??"")},[d.q]),ls({skip:d.month!=null||d.year!=null,onFallback:O});const x=de({tableName:ue.ARCHIVED_CATEGORY}),F=de({tableName:ue.ICON}),_=de({tableName:ue.ACCOUNT}),N=n.useMemo(()=>new Set((Array.isArray(_)?_:[]).filter(t=>t.type===kt.CREDIT).map(t=>t.id)),[_]),M=n.useMemo(()=>{const t=new Map;for(const s of Array.isArray(F)?F:[])s.iconName&&t.set(s.id,s.iconName);return t},[F]),h=n.useMemo(()=>new Set((Array.isArray(x)?x:[]).map(t=>{var s;return(s=t._raw)==null?void 0:s.category_id})),[x]),y=n.useMemo(()=>(p??[]).filter(t=>!h.has(t.id)),[p,h]),E=n.useMemo(()=>Ot(y),[y]),V=n.useMemo(()=>y.filter(t=>E.get(t.id)===t.id),[y,E]),ae=n.useMemo(()=>{const t=new Map;for(const s of y){const r=ze(s);r&&t.set(r,[...t.get(r)??[],s.title])}return t},[y]),{expenseCards:z,incomeCards:W,gateTotalsById:B}=n.useMemo(()=>{const t=[],s=[],r=new Map;let T=0,w=0;const J=(o,v,b)=>{const m=Kt(v),C=m&&E.has(m)?E.get(m):"__none__",G=o.get(C)??{total:0,count:0};G.total+=b,G.count+=1,o.set(C,G)},ee=(o,v)=>{let b=0;for(const m of Wt(v,N,!0)){const C=m.type===Qt.CREDIT?-Math.abs(m.amount):Math.abs(m.amount);J(o,m,C),b+=C}return b};for(let o=te-1;o>=0;o--){const v=new Date(A,l-1-o,1),[b,m]=qe(v.getMonth(),v.getFullYear()),C=S({rows:R({startDate:Ft(b),endDate:m,hiddenFromResults:!1,isCashFlow:!1}),range:[b,m]}).rows.filter(c=>fe(c,N)),G=new Map,I=new Map,$=ee(G,C);for(const c of zt(C,N)){const H=Math.abs(c.amount);J(I,c,H),o===0&&(w+=H)}o===0&&(T=$,ee(r,R({startDate:b,endDate:m,hiddenFromResults:!1,isCashFlow:!1}).filter(c=>fe(c,N)))),t.push(G),s.push(I)}const le=(o,v)=>o.reduce((b,m)=>{var C;return b+(((C=m.get(v))==null?void 0:C.total)??0)},0),Ie=(o,v,b)=>{const m=o[te-1],C=(I,$)=>{const c=m.get($),H=o.map((ut,mt)=>{var ke;const pt=te-1-mt,Se=new Date(A,l-1-pt,1);return{month:Se.getMonth()+1,year:Se.getFullYear(),total:((ke=ut.get($))==null?void 0:ke.total)??0}});return{category:I,monthTotal:(c==null?void 0:c.total)??0,share:v>0?((c==null?void 0:c.total)??0)/v:0,transactionCount:(c==null?void 0:c.count)??0,bars:H}},G=V.filter(b).map(I=>C(I,I.id));return le(o,"__none__")>0&&G.push(C(null,"__none__")),G.sort((I,$)=>{var c,H;return $.monthTotal-I.monthTotal||(((c=I.category)==null?void 0:c.id)??"__none__").localeCompare(((H=$.category)==null?void 0:H.id)??"__none__")})},De=(o,v)=>{var b;return((b=o[te-1].get(v))==null?void 0:b.total)??0},Re=o=>ze(o)!=null;return{expenseCards:Ie(t,T,o=>Re(o)?De(t,o.id)>0:le(t,o.id)>0||le(s,o.id)===0&&!Be.has(o.id)),incomeCards:Ie(s,w,o=>Re(o)?De(s,o.id)>0:le(s,o.id)>0),gateTotalsById:r}},[V,E,R,N,S,l,A,Y]),Q=g.trim().length>0,X=t=>(t.category?[t.category.title,...ae.get(t.category.id)??[]]:["Sem categoria"]).some(s=>Xt(s,g)),u=Q?z.filter(X):z,f=Q?W.filter(X):W,[q,oe]=n.useState(null),[Ze,Ce]=n.useState(null),[ve,Z]=n.useState(!1),[je,Je]=n.useState(!1),[_e,pe]=n.useState(null),[et,ge]=n.useState(!1),tt=We(l-1),[Ne,we]=qe(l-1,A),xe=n.useMemo(()=>{const t=R({startDate:Ne,endDate:we,hiddenFromResults:!1,isCashFlow:!1}).filter(qt);return t.some(s=>fe(s,N))?0:t.length},[R,Y,N,l,A]),Ae={accountId:(Array.isArray(_)?_:[]).map(t=>t.id),hiddenFromResults:!1,startDate:Ne,endDate:we,isCashFlow:!1,page:void 0},st=t=>{a({to:"/transactions",search:s=>({...s,...Ae,categoryId:[t.id],noCategoryOnly:void 0})})},at=()=>{a({to:"/transactions",search:t=>({...t,...Ae,categoryId:void 0,noCategoryOnly:!0})})},ne=n.useRef(null),ot=t=>{ne.current&&(clearTimeout(ne.current),ne.current=null),pe(t),ge(!0)},re=()=>{ge(!1),ne.current=setTimeout(()=>pe(null),200)},{gated:nt}=Gt(),ie=nt&&z.length>be,rt=n.useMemo(()=>{const t=z.map(s=>{var r;return((r=s.category)==null?void 0:r.id)??"__none__"}).sort((s,r)=>{var T,w;return(((T=B.get(r))==null?void 0:T.total)??0)-(((w=B.get(s))==null?void 0:w.total)??0)||s.localeCompare(r)});return new Map(t.map((s,r)=>[s,r]))},[z,B]),it=z.some(t=>{var s;return t.category!==null&&(((s=B.get(t.category.id))==null?void 0:s.total)??0)>0}),Te=t=>{var s;return ie&&(it?(rt.get(((s=t.category)==null?void 0:s.id)??"__none__")??be)>=be:t.category!==null&&!Be.has(t.category.id))};Lt("categories_page",ie);const{freeGate:lt,openFreeGate:ct,closeFreeGate:dt}=Bt(),{guardWrite:he}=$e(),Me=z.filter(t=>Te(t)&&t.monthTotal>0),P=ie?{lockedAmountCents:Math.round(Me.reduce((t,s)=>t+s.monthTotal,0)*100),lockedCount:Me.length}:void 0,Ee=(t,s)=>e.jsx("div",{className:"grid content-start grid-cols-1 gap-3.5 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4",children:t.map(r=>{var w,J,ee;const T=s==="expense"&&Te(r);return e.jsx(ms,{card:r,section:s,iconName:M.get(((J=(w=r.category)==null?void 0:w._raw)==null?void 0:J.icon_id)??""),moneyVisibility:D,gatedStats:T,onOpen:()=>
3{if(T){ct("categories",P);return}r.category?ot(r.category):at()},onEdit:he(()=>{r.category&&(oe(r.category),Z(!0))})},((ee=r.category)==null?void 0:ee.id)??"__none__")})});return e.jsxs(e.Fragment,{children:[e.jsx(ns,{actions:e.jsxs(Pt,{size:"sm",onClick:he(()=>{oe(null),Z(!0)}),children:[e.jsx(Ve,{size:14}),"Nova categoria"]})}),e.jsxs("div",{className:"flex min-w-0 flex-col gap-3.5",children:[e.jsx(os,{value:g,onChange:t=>{j.current=!0,L(t)},placeholder:"Buscar categoria ou subcategoria...",className:"sm:max-w-sm"}),e.jsxs("span",{className:"-mt-1 text-[12.5px] text-foreground-muted",children:["suas saÃdas e entradas de ",tt," em cada categoria â clique para ver detalhes e subcategorias"]}),xe>0&&e.jsx("span",{className:"-mt-1.5 text-[12px] text-foreground-subtle",children:xe===1?"1 lançamento agendado ainda não conta aqui â ele entra quando for efetivado.":`${xe} lançamentos agendados ainda não contam aqui â eles entram quando forem efetivados.`}),Q&&u.length===0&&f.length===0&&e.jsx($t,{title:"Nenhuma categoria encontrada.",description:`Nada bate com â${g.trim()}â â tente outro nome de categoria ou subcategoria.`}),u.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"px-1 text-[13px] font-bold",children:"SaÃdas por categoria"}),Ee(u,"expense"),ie&&e.jsx("div",{className:"flex justify-center",children:e.jsx(Ht,{surface:"categories_page",lockedAmountCents:P==null?void 0:P.lockedAmountCents,lockedCount:P==null?void 0:P.lockedCount})})]}),f.length>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"px-1 pt-1 text-[13px] font-bold",children:"Entradas por categoria"}),Ee(f,"income")]}),e.jsx("span",{className:"px-1 text-[11px] text-foreground-subtle",children:"as barrinhas mostram os últimos 4 meses · transferências entre suas próprias contas não entram na soma"}),Array.isArray(x)&&x.length>0&&e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsxs("button",{type:"button",onClick:()=>{K("toggle"),Je(t=>!t)},className:"flex w-fit cursor-pointer items-center gap-1.5 px-1 text-[12px] font-semibold text-foreground-muted hover:text-foreground",children:[e.jsx(ye,{size:13}),x.length," ",x.length===1?"categoria arquivada":"categorias arquivadas",e.jsx(me,{size:13,className:U("transition-transform",je&&"rotate-90")})]}),je&&e.jsx(Qe,{className:"flex flex-col gap-0 p-0",children:x.map(t=>{var T;const s=(T=t._raw)==null?void 0:T.category_id,r=(p??[]).find(w=>w.id===s);return r?e.jsxs("div",{className:"flex items-center gap-2.5 border-b border-border px-4 py-2.5 last:border-b-0",children:[e.jsx("span",{className:"flex-1 truncate text-[12.5px] font-semibold text-foreground-muted",children:r.title}),e.jsx("button",{type:"button",onPointerDown:()=>K("press"),onPointerUp:()=>K("release"),onClick:he(async()=>{try{await(i==null?void 0:i.unarchiveCategory(t)),se.success(`"${r.title}" voltou pra lista.`)}catch(w){Ut(w),se.error("Não deu pra restaurar â tente de novo.")}}),className:"cursor-pointer text-[12px] font-bold text-primary-soft-foreground hover:underline",children:"restaurar"})]},t.id):null})})]}),_e&&e.jsx(ds,{open:et,onOpenChange:t=>{t?ge(!0):re()},category:_e,iconNameById:M,onSelectCategory:pe,onViewTransactions:t=>{re(),st(t)},onEdit:t=>{re(),oe(t),Z(!0)},onCreateSubCategory:t=>{re(),Ce(t),Z(!0)}}),e.jsx(Yt,{gate:lt,onClose:dt}),ve&&e.jsx(Vt,{open:ve,onOpenChange:t=>{Z(t),t||setTimeout(()=>{oe(null),Ce(null)},200)},category:q,createSubCategory:Ze})]})]})}function ms({card:a,section:d,iconName:i,moneyVisibility:D,gatedStats:p=!1,onOpen:R,onEdit:Y}){var F,_,N,M;const[S,k]=n.useState(null),l=d==="income",A=Xe(i),O=Math.max(...a.bars.map(h=>h.total),1),g=!((_=(F=a.category)==null?void 0:F._raw)!=null&&_.user_id),L=((N=a.category)==null?void 0:N.title)??"Sem categoria",j=l?"var(--money-in)":"var(--primary)",x=l?Zt:Jt;return e.jsxs(Qe,{interactive:!0,onClick:()=>{K("tick"),R()},className:"group flex cursor-pointer flex-col gap-2.5 px-[18px] py-4",children:[e.jsxs("div",{className:"flex items-center gap-2.5",children:[a.category?e.jsx(Ue,{hue:Ke(a.category),size:32,className:"rounded-[10px]",children:e.jsx(A,{size:16})}):e.jsx("div",{className:U("flex size-8 flex-none items-center justify-center rounded-[10px]",l?"text-money-in":"text-money-out"),style:{background:`color-mix(in srgb, ${l?"var(--money-in)":"var(--money-out)"} 12%, transparent)`},children:e.jsx(x,{size:16})}),e.jsx("span",{className:"flex-1 truncate text-[13.5px] font-bold",children:L}),!g&&e.jsx("button",{type:"button","aria-label":`Editar ${L}`,onClick:h=>{h.stopPropagation(),Y()},className:"hidden size-6 flex-none cursor-pointer items-center justify-center rounded-md text-foreground-subtle hover:bg-surface-sunken hover:text-foreground group-hover:flex",children:e.jsx(Ye,{size:12.5})}),e.jsx(me,{size:14,className:"text-foreground-subtle"})]}),e.jsxs("div",{"aria-hidden":p||void 0,className:U("flex items-end gap-3",p&&"pointer-events-none blur-[7px] select-none"),children:[e.jsxs("div",{className:"flex min-w-0 flex-1 flex-col gap-px",children:[e.jsx("span",{className:U("dz-money text-[19px] font-extrabold",l&&"text-money-in"),children:p?es:Le(a.monthTotal,"BRL",D)}),e.jsx("span",{className:"text-[10.5px] text-foreground-subtle",children:p?"â¢â¢% · â¢â¢ transações":`${Math.round(a.share*100)}% · ${a.transactionCount===1?"1 transação":`${a.transactionCount} transações`}`})]}),e.jsxs("div",{className:"relative flex h-[34px] flex-none items-end gap-[3px]",children:[!p&&S!==null&&a.bars[S]&&e.jsxs("span",{className:"dz-money absolute right-0 bottom-[42px] z-10 rounded-lg bg-foreground px-2.5 py-[5px] text-[11px] font-semibold whitespace-nowrap text-background shadow-[0_6px_18px_rgba(9,9,11,.25)]",children:[(M=We(a.bars[S].month-1))==null?void 0:M.slice(0,3)," ","·"," ",Le(a.bars[S].total,"BRL",D)]}),a.bars.map((h,y)=>{const E=y===a.bars.length-1,V=p?Math.round(ts(y)/100*34):h.total>0?Math.max(Math.round(h.total/O*34),4):2;return e.jsx("div",{onMouseEnter:()=>k(y),onMouseLeave:()=>
3k(null),className:"w-[9px] cursor-pointer rounded-t-[3px] rounded-b-[1.5px]",style:{height:V,background:E?j:`color-mix(in srgb, ${j} 28%, transparent)`}},`${h.year}-${h.month}`)})]})]})]})}const Cs=ss("/categories")({component:us});export{Cs as Route};
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.