import{a as e,n as t,t as n}from"./jsx-runtime-B-hcVAMW.js";import{r}from"./api-Duu96wkE.js";import{E as i,H as a,U as o,j as s,k as c}from"./index-DyQEyT-0.js";import{t as l}from"./Badge-BGqYAYKM.js";import{t as u}from"./ErrorState-BkSpMSl3.js";import{t as d}from"./Field-o9dlLBxx.js";import{i as f,o as p,r as m}from"./hooks-BlgImpkY.js";var h=e(t(),1),g=n();function _(){let{name:e}=o(),t=e===void 0,n=a(),{can:_}=c(),v=m(),y=f(t?void 0:e),b=p(t?void 0:e),[x,S]=(0,h.useState)(``),[C,w]=(0,h.useState)(``),[T,E]=(0,h.useState)(!0),[D,O]=(0,h.useState)({}),[k,A]=(0,h.useState)(null),j=y.data,[M,N]=(0,h.useState)(null);j&&M!==j.name&&(N(j.name),S(j.label),w(j.description??``),E(j.is_active),O(j.permissions??{}));let P=v.data?.scopes??[],F=`branch`,I=(0,h.useMemo)(()=>Object.keys(D).length,[D]);if(v.isPending||!t&&y.isPending)return(0,g.jsx)(i,{label:`Loading permissions`});if(v.error)return(0,g.jsx)(u,{error:v.error,onRetry:()=>v.refetch()});if(y.error)return(0,g.jsx)(u,{error:y.error,onRetry:()=>y.refetch()});let L=t?!_(`role.create`):!_(`role.update`),R=(e,t)=>{O(n=>{let r={...n};return t?r[e]=n[e]??F:delete r[e],r})},z=(e,t)=>{O(n=>({...n,[e]:t}))},B=(e,t)=>{O(n=>{let r={...n};return e.forEach(e=>{t?r[e]=n[e]??F:delete r[e]}),r})};return(0,g.jsxs)(`form`,{onSubmit:async e=>{e.preventDefault(),A(null);try{let e=await b.mutateAsync({label:x,description:C||null,is_active:T,permissions:D});n(`/app/settings/roles/${e.name}`)}catch(e){A(r(e).message)}},className:`space-y-6`,children:[(0,g.jsxs)(`header`,{className:`flex flex-wrap items-end justify-between gap-4`,children:[(0,g.jsxs)(`div`,{children:[(0,g.jsx)(`h1`,{className:`text-xl font-semibold text-slate-900`,children:t?`New role`:j?.label}),(0,g.jsxs)(`p`,{className:`mt-1 flex items-center gap-2 text-sm text-slate-500`,children:[!t&&(0,g.jsx)(`span`,{className:`font-mono text-xs`,children:j?.name}),j?.is_system&&(0,g.jsx)(l,{tone:`info`,children:`System role`}),(0,g.jsxs)(`span`,{children:[I,` permission`,I===1?``:`s`,` granted`]})]})]}),(0,g.jsxs)(`div`,{className:`flex gap-2`,children:[(0,g.jsx)(s,{type:`button`,variant:`secondary`,onClick:()=>n(`/app/settings/roles`),children:`Back`}),!L&&(0,g.jsx)(s,{type:`submit`,loading:b.isPending,children:t?`Create role`:`Save changes`})]})]}),k&&(0,g.jsx)(u,{error:Error(k)}),(0,g.jsxs)(`section`,{className:`grid gap-5 rounded-xl border border-slate-200 bg-white p-6 sm:grid-cols-2`,children:[(0,g.jsx)(d,{label:`Role name`,value:x,disabled:L,onChange:e=>S(e.target.value),hint:t?`The key is generated from this name and cannot change later.`:void 0}),(0,g.jsx)(d,{label:`Description`,value:C,disabled:L,onChange:e=>w(e.target.value)}),(0,g.jsxs)(`label`,{className:`flex items-center gap-2 text-sm text-slate-700`,children:[(0,g.jsx)(`input`,{type:`checkbox`,checked:T,disabled:L,onChange:e=>E(e.target.checked),className:`size-4 rounded border-slate-300 text-brand-600 focus:ring-brand-600`}),`Active — an inactive role grants nothing`]})]}),(0,g.jsxs)(`section`,{className:`space-y-4`,children:[(0,g.jsx)(`h2`,{className:`text-sm font-semibold text-slate-900`,children:`Permission matrix`}),v.data?.modules.map(e=>{let t=e.permissions.map(e=>e.name),n=t.length>0&&t.every(e=>e in D);return(0,g.jsxs)(`div`,{className:`overflow-hidden rounded-xl border border-slate-200 bg-white`,children:[(0,g.jsxs)(`div`,{className:`flex items-center justify-between gap-4 border-b border-slate-200 bg-slate-50 px-4 py-3`,children:[(0,g.jsx)(`h3`,{className:`text-sm font-medium text-slate-900`,children:e.label}),(0,g.jsxs)(`label`,{className:`flex items-center gap-2 text-xs text-slate-600`,children:[(0,g.jsx)(`input`,{type:`checkbox`,checked:n,disabled:L,onChange:e=>B(t,e.target.checked),className:`size-4 rounded border-slate-300 text-brand-600 focus:ring-brand-600`}),`Select all`]})]}),(0,g.jsx)(`ul`,{className:`divide-y divide-slate-100`,children:e.permissions.map(e=>{let t=e.name in D;return(0,g.jsxs)(`li`,{className:`flex flex-wrap items-center justify-between gap-3 px-4 py-2.5`,children:[(0,g.jsxs)(`label`,{className:`flex min-w-0 flex-1 items-center gap-3 text-sm text-slate-700`,children:[(0,g.jsx)(`input`,{type:`checkbox`,checked:t,disabled:L,onChange:t=>R(e.name,t.target.checked),className:`size-4 shrink-0 rounded border-slate-300 text-brand-600 focus:ring-brand-600`}),(0,g.jsxs)(`span`,{className:`truncate`,children:[e.label,(0,g.jsx)(`span`,{className:`ml-2 font-mono text-xs text-slate-400`,children:e.name})]})]}),(0,g.jsxs)(`select`,{"aria-label":`Scope for ${e.label}`,value:D[e.name]??``,disabled:L||!t,onChange:t=>z(e.name,t.target.value),className:`rounded-lg border-0 py-1.5 pl-3 pr-8 text-sm text-slate-700 ring-1 ring-inset ring-slate-300 focus:ring-2 focus:ring-brand-600 disabled:bg-slate-50 disabled:text-slate-400`,children:[(0,g.jsx)(`option`,{value:``,disabled:!0,children:`No access`}),P.map(e=>(0,g.jsx)(`option`,{value:e.value,children:e.label},e.value))]})]},e.name)})})]},e.module)})]})]})}export{_ as default};