import{iv as v,fH as e,bU as c,ck as j,aD as P,jG as C,cz as x,cI as l,cQ as V,bt as B,i$ as H,bL as b,iG as N,iC as _,gH as G,iY as Q,hi as q,io as z,ip as W,j9 as $,ja as X,bo as Y,h0 as Z,ap as J,aZ as S,r as K,h1 as ee,e5 as se,e6 as L,es as re,fo as I}from"./strapi-ye6TfKul.js";import{u as ie}from"./useAdminRoles-C08Egt77.js";import{P as te}from"./Permissions-eQguL5Ly.js";import"./groupBy-BW-FsOq9.js";import"./_baseEach-BHFmNlG9.js";const ae=({disabled:t,role:s,values:p,errors:r,onChange:n,onBlur:m})=>{const{formatMessage:i}=v();return e.jsx(c,{background:"neutral0",padding:6,shadow:"filterShadow",hasRadius:!0,children:e.jsxs(j,{direction:"column",alignItems:"stretch",gap:4,children:[e.jsxs(j,{justifyContent:"space-between",children:[e.jsxs(c,{children:[e.jsx(c,{children:e.jsx(P,{fontWeight:"bold",children:s?s.name:i({id:"global.details",defaultMessage:"Details"})})}),e.jsx(c,{children:e.jsx(P,{textColor:"neutral500",variant:"pi",children:s?s.description:i({id:"Settings.roles.form.description",defaultMessage:"Name and description of the role"})})})]}),e.jsx(C,{disabled:!0,variant:"secondary",children:i({id:"Settings.roles.form.button.users-with-role",defaultMessage:"{number, plural, =0 {# users} one {# user} other {# users}} with this role"},{number:s.usersCount})})]}),e.jsxs(x.Root,{gap:4,children:[e.jsx(x.Item,{xs:12,col:6,direction:"column",alignItems:"stretch",children:e.jsxs(l.Root,{name:"name",error:r.name&&i({id:r.name}),required:!0,children:[e.jsx(l.Label,{children:i({id:"global.name",defaultMessage:"Name"})}),e.jsx(V,{disabled:t,onChange:n,onBlur:m,value:p.name||"",type:"text"}),e.jsx(l.Error,{})]})}),e.jsx(x.Item,{xs:12,col:6,direction:"column",alignItems:"stretch",children:e.jsxs(l.Root,{name:"description",error:r.description&&i({id:r.description}),children:[e.jsx(l.Label,{children:i({id:"global.description",defaultMessage:"Description"})}),e.jsx(B,{disabled:t,onChange:n,onBlur:m,value:p.description}),e.jsx(l.Error,{})]})})]})]})})},oe=se().shape({name:L().required(re.required.id),description:L().optional()}),ne=()=>{const{toggleNotification:t}=N(),{formatMessage:s}=v(),r=_("/settings/roles/:id")?.params.id,n=G.useRef(null),{trackUsage:m}=Q(),{_unstableFormatAPIError:i,_unstableFormatValidationErrors:R}=q(),{isLoading:A,data:y}=z({role:r??""}),{roles:w,isLoading:E,refetch:k}=ie({id:r},{refetchOnMountOrArgChange:!0}),d=w[0]??{},{data:T,isLoading:D}=W({id:r},{skip:!r,refetchOnMountOrArgChange:!0}),[F]=$(),[O]=X();if(!r)return e.jsx(Y,{to:"/settings/roles"});const U=async(h,g)=>{try{const{permissionsToSend:u,didUpdateConditions:f}=n.current?.getPermissions()??{},a=await F({id:r,...h});if("error"in a){I(a.error)&&a.error.name==="ValidationError"?g.setErrors(R(a.error)):t({type:"danger",message:i(a.error)});return}if(d.code!=="strapi-super-admin"&&u){const o=await O({id:a.data.id,permissions:u});if("error"in o){I(o.error)&&o.error.name==="ValidationError"?g.setErrors(R(o.error)):t({type:"danger",message:i(o.error)});return}f&&m("didUpdateConditions")}n.current?.setFormAfterSubmit(),await k(),t({type:"success",message:s({id:"notification.success.saved"})})}catch{t({type:"danger",message:s({id:"notification.error",defaultMessage:"An error occurred"})})}},M=!E&&d.code==="strapi-super-admin";return A||E||D||!y?e.jsx(b.Loading,{}):e.jsxs(Z,{children:[e.jsx(b.Title,{children:s({id:"Settings.PageTitle",defaultMessage:"Settings - {name}"},{name:"Roles"})}),e.jsx(J,{enableReinitialize:!0,initialValues:{name:d.name??"",description:d.description??""},onSubmit:U,validationSchema:oe,validateOnChange:!1,children:({handleSubmit:h,values:g,errors:u,handleChange:f,handleBlur:a,isSubmitting:o})=>e.jsxs("form",{onSubmit:h,children:[e.jsx(S.Header,{primaryAction:e.jsx(C,{type:"submit",startIcon:e.jsx(ee,{}),disabled:d.code==="strapi-super-admin",loading:o,fullWidth:!0,children:s({id:"global.save",defaultMessage:"Save"})}),title:s({id:"Settings.roles.edit.title",defaultMessage:"Edit a role"}),subtitle:s({id:"Settings.roles.create.description",defaultMessage:"Define the rights given to the role"}),navigationAction:e.jsx(c,{display:{initial:"none",medium:"block"},children:e.jsx(K,{fallback:"../roles"})})}),e.jsx(S.Content,{children:e.jsxs(j,{direction:"column",alignItems:"stretch",gap:6,children:[e.jsx(ae,{disabled:M,errors:u,values:g,onChange:f,onBlur:a,role:d}),e.jsx(c,{shadow:"filterShadow",hasRadius:!0,children:e.jsx(te,{isFormDisabled:M,permissions:T,ref:n,layout:y})})]})})]})})]})},ge=()=>{const t=H(s=>s.admin_app.permissions.settings?.roles.update);return e.jsx(b.Protect,{permissions:t,children:e.jsx(ne,{})})};export{ne as EditPage,ge as ProtectedEditPage};
