1import{c as ye,r as u,j as e,ak as L,bd as U,bt as fe,z as be,cP as Z,cQ as J,w as l,cR as ee,cS as P,cT as se,b as Pe,bW as T,bX as D,bY as O,P as Ne,av as Te,R as De,a_ as Oe,U as ae,cU as Ae,ct as ne,a3 as k,a4 as S,a5 as w,a6 as V,a7 as te,a8 as A,aL as re,aM as ie,aN as oe,aO as le,aP as F,cV as ce,cW as de,V as xe,aU as Fe,cX as ue,cY as me,ae as he,aa as z,cZ as Ie,c_ as ze}from"./index-ietnvNu-.js";import{P as Re}from"./page-header-DFq68XPy.js";import{P as Me}from"./page-skeleton-D3M5Rc1E.js";import{E as qe}from"./editorial-BM528JAB.js";import{S as Ve}from"./segmented-control-BgH2eF6T.js";import{E as je}from"./ellipsis-BJkZ7Xdz.js";import{T as Le,a as $e,b as B,c as K,d as He,e as j}from"./table-CdogKBWw.js";import{a as pe}from"./sortable-header-0PVPYl5D.js";import{u as Be}from"./useTableSort-QRXC9v_z.js";import{P as ge}from"./pencil-CD9Qpmqs.js";import{A as ve}from"./archive-Drcpdana.js";import{C as Ke}from"./confirm-dialog-5fdFrSkL.js";import"./skeleton-CjV1AjFQ.js";import"./page-container--5qh_TAc.js";import"./arrow-up-BFqjDY9J.js";import"./alert-dialog-DRWHNpMt.js";const Ce=ye("ArchiveRestore",[["rect",{width:"20",height:"5",x:"2",y:"3",rx:"1",key:"1wp1u1"}],["path",{d:"M4 8v11a2 2 0 0 0 2 2h2",key:"tvwodi"}],["path",{d:"M20 8v11a2 2 0 0 1-2 2h-2",key:"1gkqxj"}],["path",{d:"m9 15 3-3 3 3",key:"1pd0qc"}],["path",{d:"M12 12v9",key:"192myk"}]]),_=v=>({INCOME:"Receita",EXPENSE:"Despesa",BOTH:"Ambos"})[v]||v;function Ue({categories:v,onEdit:c,onAddSubcategory:C,onArchive:m,onRestore:E,showArchived:d}){const[p,f]=u.useState(new Set),b=n=>{f(o=>{const x=new Set(o);return x.has(n)?x.delete(n):x.add(n),x})};return e.jsx("div",{className:"divide-y divide-border",children:v.map(n=>{const o=(n.children||[]).length>0,x=p.has(n.id);return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-3 p-3 min-h-[52px]",children:[e.jsx("span",{className:"flex h-10 w-10 shrink-0 items-center justify-center rounded-full",style:{backgroundColor:`${n.color}20`},children:e.jsx(L,{icon:n.icon,color:n.color,className:"h-[18px] w-[18px]"})}),e.jsxs("button",{type:"button",className:"flex-1 min-w-0 text-left",onClick:()=>o&&b(n.id),children:[e.jsx("p",{className:"text-sm font-medium truncate",children:n.name}),e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(U,{variant:"secondary",className:"text-[10px] px-1.5 py-0",children:_(n.kind)}),o&&e.jsxs("span",{className:"text-[10px] text-muted-foreground",children:[n.children.length," sub"]})]})]}),o&&e.jsx("button",{type:"button",className:"p-1",onClick:()=>b(n.id),children:x?e.jsx(fe,{className:"h-4 w-4 text-muted-foreground"}):e.jsx(be,{className:"h-4 w-4 text-muted-foreground"})}),e.jsxs(Z,{children:[e.jsx(J,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",className:"h-8 w-8 shrink-0",children:e.jsx(je,{className:"h-4 w-4"})})}),e.jsx(ee,{align:"end",children:d?e.jsx(P,{onClick:()=>E(n.id),children:"Reativar"}):e.jsxs(e.Fragment,{children:[e.jsx(P,{onClick:()=>c(n),children:"Editar"}),e.jsx(P,{onClick:()=>C(n),children:"Adicionar subcategoria"}),e.jsx(se,{}),e.jsx(P,{className:"text-destructive",onClick:()=>m(n.id),children:"Arquivar"})]})})]})]}),x&&n.children?.map(i=>e.jsxs("div",{className:"flex items-center gap-3 p-3 pl-10 min-h-[48px] bg-muted/20",children:[e.jsx("span",{className:"flex h-8 w-8 shrink-0 items-center justify-center rounded-full",style:{backgroundColor:`${i.color}20`},children:e.jsx(L,{icon:i.icon,color:i.color,className:"h-4 w-4"})}),e.jsx("div",{className:"flex-1 min-w-0",children:e.jsx("p",{className:"text-sm truncate",children:i.name})}),e.jsxs(Z,{children:[e.jsx(J,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",className:"h-8 w-8 shrink-0",children:e.jsx(je,{className:"h-4 w-4"})})}),e.jsx(ee,{align:"end",children:d?e.jsx(P,{onClick:()=>E(i.id),children:"Reativar"}):e.jsxs(e.Fragment,{children:[e.jsx(P,{onClick:()=>c(i),children:"Editar"}),e.jsx(se,{}),e.jsx(P,{className:"text-destructive",onClick:()=>m(i.id),children:"Arquivar"})]})})]})]},i.id))]},n.id)})})}function _e({categories:v,onEdit:c,onAddSubcategory:C,onArchive:m,onRestore:E,isArchiving:d,showArchived:p}){const[f,b]=u.useState(new Set),{sortedData:n,sortKey:o,sortDirection:x,toggleSort:i}=Be(v,{defaultKey:"name",defaultDirection:"asc"}),I=a=>{b(t=>{const h=new Set(t);return h.has(a)?h.delete(a):h.add(a),h})};return e.jsxs(Le,{children:[e.jsx($e,{children:e.jsxs(B,{children:[e.jsx(pe,{label:"Categoria",sortKey:"name",currentSortKey:o,currentDirection:x,onSort:i}),e.jsx(pe,{label:"Tipo",sortKey:"kind",currentSortKey:o,currentDirection:x,onSort:i}),e.jsx(K,{children:"Cor"}),e.jsx(K,{children:"Ãcone"}),e.jsx(K,{className:"text-right",children:"Ações"})]})}),e.jsx(He,{children:n.map(a=>{const t=(a.children||[]).length>0,h=f.has(a.id);return e.jsxs(u.Fragment,{children:[e.jsxs(B,{"data-testid":`category-item-${a.id}`,className:Pe(t&&"cursor-pointer"),onClick:()=>t&&I(a.id),children:[e.jsx(j,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[t&&(h?e.jsx(fe,{className:"h-4 w-4 text-muted-foreground shrink-0"}):e.jsx(be,{className:"h-4 w-4 text-muted-foreground shrink-0"})),e.jsx("span",{className:"font-medium",children:a.name}),t&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:[a.children.length," sub"]})]})}),e.jsx(j,{children:e.jsx(U,{variant:"secondary",className:"text-xs",children:_(a.kind)})}),e.jsx(j,{children:e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"h-3 w-3 rounded-full",style:{backgroundColor:a.color}})})}),e.jsx(j,{children:e.jsx("span",{className:"flex h-9 w-9 shrink-0 items-center justify-center rounded-lg",style:{backgroundColor:`${a.color}20`},children:e.jsx(L,{icon:a.icon,color:a.color,className:"h-4 w-4"})})}),e.jsx(j,{className:"text-right",children:e.jsx("div",{className:"flex justify-end gap-1",onClick:r=>r.stopPropagation(),children:p?e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>E(a.id),disabled:d,children:e.jsx(Ce,{className:"h-4 w-4 text-success"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Reativar"})]}):e.jsxs(e.Fragment,{children:[e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>C(a),"data-testid":`button-add-subcategory-${a.id}`,children:e.jsx(Ne,{className:"h-4 w-4"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Adicionar subcategoria"})]}),e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>c(a),"data-testid":`button-edit-category-${a.id}`,children:e.jsx(ge,{className:"h-4 w-4"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Editar"})]}),e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>m(a.id),disabled:d,"data-testid":`button-delete-category-${a.id}`,children:e.jsx(ve,{className:"h-4 w-4 text-destructive"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Arquivar"})]})]})})})]}),h&&a.children?.map(r=>e.jsxs(B,{className:"bg-muted/20 hover:bg-muted/40 border-l-2 border-l-primary/30","data-testid":`subcategory-item-${r.id}`,children:[e.jsx(j,{children:e.jsx("span",{className:"pl-8 text-sm",children:r.name})}),e.jsx(j,{children:e.jsx(U,{variant:"secondary",className:"text-xs",children:_(r.kind)})}),e.jsx(j,{children:e.jsx("div",{className:"flex items-center gap-2",children:e.jsx("span",{className:"h-3 w-3 rounded-full",style:{backgroundColor:r.color}})})}),e.jsx(j,{children:e.jsx("span",{className:"flex h-9 w-9 shrink-0 items-center justify-center rounded-lg",style:{backgroundColor:`${r.color}20`}
1,children:e.jsx(L,{icon:r.icon,color:r.color,className:"h-4 w-4"})})}),e.jsx(j,{className:"text-right",children:e.jsx("div",{className:"flex justify-end gap-1",children:p?e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>E(r.id),disabled:d,children:e.jsx(Ce,{className:"h-4 w-4 text-success"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Reativar"})]}):e.jsxs(e.Fragment,{children:[e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>c(r),"data-testid":`button-edit-category-${r.id}`,children:e.jsx(ge,{className:"h-4 w-4"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Editar"})]}),e.jsxs(T,{delayDuration:150,children:[e.jsx(D,{asChild:!0,children:e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>m(r.id),disabled:d,"data-testid":`button-delete-category-${r.id}`,children:e.jsx(ve,{className:"h-4 w-4 text-destructive"})})}),e.jsx(O,{side:"top",sideOffset:6,className:"pointer-events-none",children:"Arquivar"})]})]})})})]},r.id))]},a.id)})})]})}function cs(){const[v,c]=u.useState(!1),[C,m]=u.useState(null),[E,d]=u.useState(!1),[p,f]=u.useState(null),[b,n]=u.useState("all"),[o,x]=u.useState("active"),[i,I]=u.useState(null),a=Te(),{toast:t}=De(),{categoriesTree:h,isLoading:r,isTreeLoading:ke,createCategory:R,createSubcategory:M,updateCategory:q,deleteCategory:X,restoreCategory:Y}=Oe(),g=ae({resolver:he(Ie),defaultValues:{name:"",icon:me,color:ue,kind:"BOTH"}}),N=ae({resolver:he(ze),defaultValues:{name:"",customizeVisual:!1,icon:"",color:""}}),$=s=>{s?(m(s),g.reset({name:s.name,icon:s.icon,color:s.color,kind:s.kind})):(m(null),g.reset({name:"",icon:me,color:ue,kind:"BOTH"})),c(!0)},G=s=>{f(s),N.reset({name:"",customizeVisual:!1,icon:"",color:""}),d(!0)},W=s=>{I(s)},Se=()=>{i&&X.mutate(i,{onSuccess:()=>{I(null),t({title:"Categoria arquivada"})},onError:s=>{I(null),z(t,"Erro ao arquivar",s)}})},Q=s=>{Y.mutate(s,{onSuccess:()=>t({title:"Categoria reativada"}),onError:y=>z(t,"Erro ao reativar",y)})},we=s=>{C?q.mutate({...s,id:C.id},{onSuccess:()=>{c(!1),m(null),g.reset(),t({title:"Categoria atualizada"})},onError:y=>z(t,"Erro ao atualizar",y)}):R.mutate(s,{onSuccess:()=>{c(!1),g.reset(),t({title:"Categoria criada"})},onError:y=>z(t,"Erro ao criar categoria",y)})},Ee=s=>{p&&M.mutate({parentId:p.id,name:s.name,icon:s.customizeVisual?s.icon:void 0,color:s.customizeVisual?s.color:void 0},{onSuccess:()=>{d(!1),f(null),N.reset(),t({title:"Subcategoria criada"})},onError:y=>z(t,"Erro ao criar subcategoria",y)})},H=u.useMemo(()=>Ae(h||[],{kind:b,status:o}),[h,b,o]);return r||ke?e.jsx(Me,{showAction:!0,cards:1,listRows:6}):e.jsxs(qe,{children:[e.jsx(Re,{title:"Categorias",actions:e.jsxs(ne,{open:v,onOpenChange:c,title:C?"Editar Categoria":"Nova Categoria",trigger:e.jsxs(l,{onClick:()=>$(),"data-testid":"button-add-category",children:[e.jsx(Ne,{className:"h-4 w-4 mr-2"}),"Nova Categoria"]}),form:g,onSubmit:we,isSubmitting:R.isPending||q.isPending,contentClassName:"sm:max-w-md",footer
1:e.jsxs("div",{className:"flex gap-3 pt-4",children:[e.jsx(l,{type:"button",variant:"outline",onClick:()=>{c(!1),m(null)},className:"flex-1",children:"Cancelar"}),e.jsxs(l,{type:"submit",className:"flex-1",disabled:R.isPending||q.isPending,"data-testid":"button-submit-category",children:[(R.isPending||q.isPending)&&e.jsx(xe,{className:"h-4 w-4 mr-2 animate-spin"}),C?"Salvar":"Criar"]})]}),children:[e.jsx(k,{control:g.control,name:"name",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Nome"}),e.jsx(V,{children:e.jsx(te,{placeholder:"Ex: Mercado, Uber...","data-testid":"input-category-name",...s})}),e.jsx(A,{})]})}),e.jsx(k,{control:g.control,name:"kind",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Tipo"}),e.jsxs(re,{onValueChange:s.onChange,value:s.value,children:[e.jsx(V,{children:e.jsx(ie,{"data-testid":"select-category-kind",children:e.jsx(oe,{})})}),e.jsxs(le,{children:[e.jsx(F,{value:"BOTH",children:"Ambos"}),e.jsx(F,{value:"INCOME",children:"Apenas Receitas"}),e.jsx(F,{value:"EXPENSE",children:"Apenas Despesas"})]})]}),e.jsx(A,{})]})}),e.jsx(k,{control:g.control,name:"icon",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Ãcone"}),e.jsx(ce,{value:s.value,onChange:s.onChange,testIdPrefix:"icon-option"}),e.jsx(A,{})]})}),e.jsx(k,{control:g.control,name:"color",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Cor"}),e.jsx(de,{value:s.value,onChange:s.onChange,testIdPrefix:"color-option"}),e.jsx(A,{})]})})]})}),e.jsxs("section",{className:"mt-8 md:mt-12",children:[e.jsxs("div",{className:"flex flex-col gap-3 pb-6 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs(re,{value:b,onValueChange:n,children:[e.jsx(ie,{className:"w-full sm:w-[180px]",children:e.jsx(oe,{placeholder:"Tipo"})}),e.jsxs(le,{children:[e.jsx(F,{value:"all",children:"Todos os tipos"}),e.jsx(F,{value:"EXPENSE",children:"Despesas"}),e.jsx(F,{value:"INCOME",children:"Receitas"}),e.jsx(F,{value:"BOTH",children:"Ambos"})]})]}),e.jsx(Ve,{value:o,onChange:x,options:[{value:"active",label:"Ativas"},{value:"archived",label:"Arquivadas"},{value:"all",label:"Todas"}]})]}),H.length>0?a?e.jsx(Ue,{categories:H,onEdit:$,onAddSubcategory:G,onArchive:W,onRestore:Q,showArchived:o==="archived"}):e.jsx(_e,{categories:H,onEdit:$,onAddSubcategory:G,onArchive:W,onRestore:Q,isArchiving:X.isPending||Y.isPending,showArchived:o==="archived"}):e.jsx("p",{className:"py-12 text-center text-sm text-muted-foreground tracking-tight",children:"Nenhuma categoria encontrada"})]}),e.jsxs(ne,{open:E,onOpenChange:s=>{s||f(null),d(s)},title:p?`Nova subcategoria em ${p.name}`:"Nova subcategoria",form:N,onSubmit:Ee,isSubmitting:M.isPending,contentClassName:"sm:max-w-md",footer
1:e.jsxs("div",{className:"flex gap-3 pt-4",children:[e.jsx(l,{type:"button",variant:"outline",onClick:()=>{d(!1),f(null)},className:"flex-1",children:"Cancelar"}),e.jsxs(l,{type:"submit",className:"flex-1",disabled:M.isPending,"data-testid":"button-submit-subcategory",children:[M.isPending&&e.jsx(xe,{className:"h-4 w-4 mr-2 animate-spin"}),"Criar"]})]}),children:[e.jsx(k,{control:N.control,name:"name",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Nome"}),e.jsx(V,{children:e.jsx(te,{placeholder:"Ex: Restaurantes, Delivery...","data-testid":"input-subcategory-name",...s})}),e.jsx(A,{})]})}),e.jsx(k,{control:N.control,name:"customizeVisual",render:({field:s})=>e.jsxs(S,{className:"flex items-center justify-between rounded-lg border p-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(w,{children:"Personalizar Ãcone e cor"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Se desligado, herda o visual da categoria pai."})]}),e.jsx(V,{children:e.jsx(Fe,{checked:s.value,onCheckedChange:s.onChange})})]})}),N.watch("customizeVisual")&&e.jsxs(e.Fragment,{children:[e.jsx(k,{control:N.control,name:"icon",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Ãcone"}),e.jsx(ce,{value:s.value||"",onChange:s.onChange,testIdPrefix:"subcategory-icon-option"}),e.jsx(A,{})]})}),e.jsx(k,{control:N.control,name:"color",render:({field:s})=>e.jsxs(S,{children:[e.jsx(w,{children:"Cor"}),e.jsx(de,{value:s.value||"",onChange:s.onChange,testIdPrefix:"subcategory-color-option"}),e.jsx(A,{})]})})]})]}),e.jsx(Ke,{open:!!i,onOpenChange:s=>{s||I(null)},title:"Arquivar categoria",description:"Tem certeza que deseja arquivar esta categoria? As transações existentes manterão a categoria, mas ela não aparecerá mais para seleção.",confirmLabel:"Arquivar",onConfirm:Se})]})}export{cs as default};
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.