import{r,j as e}from"./vendor-react-9M-xu2bR.js";import{c as w}from"./infrastructure-n1j_TN6w.js";import{B as p,aa as b,G as v}from"./ui-components-BxvihT61.js";import"./framework-BM176iet.js";import{b as n}from"./vendor-ui-utils-3tkeUVaX.js";import{a as C}from"./index-CTBPkbPG.js";import{s as L}from"./systemObjects-DSpePIIy.js";import{ba as P,m as S,S as k,L as A,l as _}from"./vendor-icons-ndICq8DC.js";import"./vendor-i18n-E3-WitRV.js";import"./vendor-radix-C6qchjxv.js";import"./vendor-charts-BLNOCZWl.js";import"./vendor-objectstack-YKtcdalX.js";import"./vendor-zod-DxQVy2_o.js";import"./plugins-core-qwgXbAqa.js";import"./vendor-dndkit-CX1oDZqy.js";import"./plugins-views-CveFN6PV.js";import"./data-adapter-rvE3xy2B.js";import"./ui-layout-C6KWQtRO.js";const x=L.find(o=>o.name==="sys_permission"),u=x.views[0].columns;function W(){const{user:o}=w(),c=o?.role==="admin",a=C(),[h,f]=r.useState([]),[j,m]=r.useState(!0),[l,g]=r.useState(""),i=r.useCallback(async()=>{if(a){m(!0);try{const s=await a.find("sys_permission");f(s.data||[])}catch{n.error("Failed to load permissions")}finally{m(!1)}}},[a]);r.useEffect(()=>{i()},[i]);const y=r.useCallback(async()=>{if(a)try{await a.create("sys_permission",{name:"New Permission",description:"",resource:"",action:"read"}),n.success("Permission created"),i()}catch{n.error("Failed to create permission")}},[a,i]),N=r.useCallback(async s=>{if(a)try{await a.delete("sys_permission",s),n.success("Permission deleted"),i()}catch{n.error("Failed to delete permission")}},[a,i]),d=h.filter(s=>{if(!l)return!0;const t=l.toLowerCase();return(s.name||"").toLowerCase().includes(t)||(s.resource||"").toLowerCase().includes(t)||(s.action||"").toLowerCase().includes(t)||(s.description||"").toLowerCase().includes(t)});return e.jsxs("div",{className:"flex flex-col gap-4 p-4 sm:p-6",children:[e.jsxs("div",{className:"flex flex-col sm:flex-row sm:items-center justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0 flex items-center gap-2",children:[e.jsx(P,{className:"h-5 w-5 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl sm:text-2xl font-bold tracking-tight",children:"Permissions"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Manage permission rules and assignments"})]})]}),c&&e.jsxs(p,{onClick:y,"data-testid":"add-permission-btn",children:[e.jsx(S,{className:"h-4 w-4 mr-2"}),"Add Permission"]})]}),e.jsxs("div",{className:"relative max-w-sm",children:[e.jsx(k,{className:"pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground"}),e.jsx(b,{placeholder:"Search permissions...",value:l,onChange:s=>g(s.target.value),className:"pl-8","data-testid":"permission-search-input"})]}),j?e.jsxs("div",{className:"flex items-center gap-2 py-8 justify-center text-muted-foreground",children:[e.jsx(A,{className:"h-5 w-5 animate-spin"}),e.jsx("span",{children:"Loading permissions..."})]}):d.length===0?e.jsx("p",{className:"text-muted-foreground text-center py-8",children:"No permissions found."}):e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm","data-testid":"permissions-table",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b text-left",children:[u.map(s=>e.jsx("th",{className:"py-2 px-3 font-medium text-muted-foreground capitalize",children:x.fields.find(t=>t.name===s)?.label||s},s)),c&&e.jsx("th",{className:"py-2 px-3 font-medium text-muted-foreground",children:"Actions"})]})}),e.jsx("tbody",{children:d.map(s=>e.jsxs("tr",{className:"border-b hover:bg-muted/50",children:[u.map(t=>e.jsx("td",{className:"py-2 px-3",children:t==="action"?e.jsx(v,{variant:"outline",children:s[t]}):e.jsx("span",{className:"truncate",children:s[t]??"—"})},t)),c&&e.jsx("td",{className:"py-2 px-3",children:e.jsx(p,{variant:"ghost",size:"icon",onClick:()=>N(s.id),children:e.jsx(_,{className:"h-4 w-4"})})})]},s.id))})]})})]})}export{W as PermissionManagementPage};
