import{a as e}from"./rolldown-runtime-B0Z9INg1.js";import{$ as t}from"./vendor-charts-iXlq6Rne.js";import{c as n}from"./api-CqQWIBIg.js";import{_ as r}from"./vendor-flow-IzlrcDx6.js";import{a as i,an as a,i as o,yn as s}from"./index-HkVQGc9M.js";import{t as c}from"./Toggle-2QSMSFck.js";import{t as l}from"./Input-DAV_OJ97.js";import{t as u}from"./Textarea-U2S-asBb.js";var d=e(t(),1),f=r();function p({role:e,onClose:t,onSuccess:r}){let p=!!e,[m,h]=(0,d.useState)(e?.name||``),[g,_]=(0,d.useState)(e?.description||``),[v,y]=(0,d.useState)(e?.is_active??!0),[b,x]=(0,d.useState)({}),[S,{isLoading:C}]=a(),[w,{isLoading:T}]=s(),E=C||T;return(0,f.jsx)(i,{isOpen:!0,onClose:t,title:p?`Edit Role`:`Create New Role`,description:p?`Update role name and description.`:`Define a new role. You can configure permissions after creation.`,size:`md`,footer:(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(o,{variant:`outline`,onClick:t,children:`Cancel`}),(0,f.jsx)(o,{onClick:async()=>{if(!m.trim()){x({name:`Role name is required.`});return}try{p&&e?(await w({id:e.id,body:{name:m.trim(),description:g.trim(),is_active:v}}).unwrap(),n.success(`Role updated`)):(await S({name:m.trim(),description:g.trim(),is_active:v}).unwrap(),n.success(`Role created`)),r()}catch(e){let t=e.data?.errors;if(t){let e={};for(let[n,r]of Object.entries(t))Array.isArray(r)&&(e[n]=r[0]);x(e)}else n.error(p?`Failed to update role`:`Failed to create role`)}},isLoading:E,children:p?`Save Changes`:`Create Role`})]}),children:(0,f.jsxs)(`div`,{className:`space-y-4`,children:[(0,f.jsx)(l,{label:`Role Name`,value:m,onChange:e=>{h(e.target.value),x({})},placeholder:`e.g., Sales Manager`,error:b.name,disabled:e?.is_system,hint:e?.is_system?`System role names cannot be changed.`:void 0}),(0,f.jsx)(u,{label:`Description`,value:g,onChange:e=>_(e.target.value),placeholder:`Describe what this role is for...`,rows:3}),(0,f.jsx)(c,{checked:v,onChange:y,label:`Active`,description:`Inactive roles cannot be assigned to users.`})]})})}export{p as t};