import{i$ as A,fH as e,bL as p,iH as k,iG as D,iv as F,iF as T,gH as N,iY as H,hi as V,io as q,ip as U,hy as B,ja as G,h0 as O,ap as Q,ao as _,aZ as b,bU as l,r as z,ck as u,jG as y,h1 as W,aD as R,cz as g,cI as d,cQ as X,bt as Y,e5 as Z,e6 as P,es as M,fo as S,jD as J}from"./strapi-ye6TfKul.js";import{P as K}from"./Permissions-eQguL5Ly.js";import{f as ee}from"./index-DJ1SXY40.js";import"./groupBy-BW-FsOq9.js";import"./_baseEach-BHFmNlG9.js";import"./index-BRVyLNfZ.js";const se=Z().shape({name:P().required(M.required.id),description:P().required(M.required.id)}),re=()=>{const{id:s}=k(),{toggleNotification:o}=D(),{formatMessage:r}=F(),C=T(),m=N.useRef(null),{trackUsage:h}=H(),{_unstableFormatAPIError:f,_unstableFormatValidationErrors:x}=V(),{isLoading:E,currentData:j}=q({role:s??""}),{currentData:w,isLoading:v}=U({id:s},{skip:!s,refetchOnMountOrArgChange:!0}),[I]=B(),[L]=G(),$=async(c,i)=>{try{h(s?"willDuplicateRole":"willCreateNewRole");const t=await I(c);if("error"in t){S(t.error)&&t.error.name==="ValidationError"?i.setErrors(x(t.error)):o({type:"danger",message:f(t.error)});return}const{permissionsToSend:n}=m.current?.getPermissions()??{};if(t.data.id&&Array.isArray(n)&&n.length>0){const a=await L({id:t.data.id,permissions:n});if("error"in a){S(a.error)&&a.error.name==="ValidationError"?i.setErrors(x(a.error)):o({type:"danger",message:f(a.error)});return}}o({type:"success",message:r({id:"Settings.roles.created",defaultMessage:"created"})}),C(`../roles/${t.data.id.toString()}`,{replace:!0})}catch{o({type:"danger",message:r({id:"notification.error",defaultMessage:"An error occurred"})})}};return E&&v||!j?e.jsx(p.Loading,{}):e.jsxs(O,{children:[e.jsx(p.Title,{children:r({id:"Settings.PageTitle",defaultMessage:"Settings - {name}"},{name:"Roles"})}),e.jsx(Q,{initialValues:{name:"",description:`${r({id:"Settings.roles.form.created",defaultMessage:"Created"})} ${ee(new Date,"PPP")}`},onSubmit:$,validationSchema:se,validateOnChange:!1,children:({values:c,errors:i,handleReset:t,handleChange:n,isSubmitting:a})=>e.jsx(_,{children:e.jsxs(e.Fragment,{children:[e.jsx(b.Header,{primaryAction:e.jsxs(u,{width:"100%",gap:2,children:[e.jsx(y,{variant:"secondary",onClick:()=>{t(),m.current?.resetForm()},fullWidth:!0,children:r({id:"app.components.Button.reset",defaultMessage:"Reset"})}),e.jsx(y,{type:"submit",loading:a,startIcon:e.jsx(W,{}),fullWidth:!0,children:r({id:"global.save",defaultMessage:"Save"})})]}),title:r({id:"Settings.roles.create.title",defaultMessage:"Create a role"}),subtitle:r({id:"Settings.roles.create.description",defaultMessage:"Define the rights given to the role"}),navigationAction:e.jsx(l,{display:{initial:"none",medium:"block"},children:e.jsx(z,{fallback:"../roles"})})}),e.jsx(b.Content,{children:e.jsxs(u,{direction:"column",alignItems:"stretch",gap:6,children:[e.jsx(l,{background:"neutral0",padding:6,shadow:"filterShadow",hasRadius:!0,children:e.jsxs(u,{direction:"column",alignItems:"stretch",gap:4,children:[e.jsxs(u,{justifyContent:"space-between",children:[e.jsxs(l,{children:[e.jsx(l,{children:e.jsx(R,{fontWeight:"bold",children:r({id:"global.details",defaultMessage:"Details"})})}),e.jsx(l,{children:e.jsx(R,{variant:"pi",textColor:"neutral600",children:r({id:"Settings.roles.form.description",defaultMessage:"Name and description of the role"})})})]}),e.jsx(te,{children:r({id:"Settings.roles.form.button.users-with-role",defaultMessage:"{number, plural, =0 {# users} one {# user} other {# users}} with this role"},{number:0})})]}),e.jsxs(g.Root,{gap:4,children:[e.jsx(g.Item,{m:6,xs:12,direction:"column",alignItems:"stretch",children:e.jsxs(d.Root,{name:"name",error:i.name&&r({id:i.name}),required:!0,children:[e.jsx(d.Label,{children:r({id:"global.name",defaultMessage:"Name"})}),e.jsx(X,{type:"text",onChange:n,value:c.name}),e.jsx(d.Error,{})]})}),e.jsx(g.Item,{m:6,xs:12,direction:"column",alignItems:"stretch",children:e.jsxs(d.Root,{name:"description",error:i.description&&r({id:i.description}),children:[e.jsx(d.Label,{children:r({id:"global.description",defaultMessage:"Description"})}),e.jsx(Y,{onChange:n,value:c.description})]})})]})]})}),e.jsx(l,{shadow:"filterShadow",hasRadius:!0,children:e.jsx(K,{isFormDisabled:!1,ref:m,permissions:w,layout:j})})]})})]})})})]})},te=J.div`
  border: 1px solid ${({theme:s})=>s.colors.primary200};
  background: ${({theme:s})=>s.colors.primary100};
  padding: ${({theme:s})=>`${s.spaces[2]} ${s.spaces[4]}`};
  color: ${({theme:s})=>s.colors.primary600};
  border-radius: ${({theme:s})=>s.borderRadius};
  font-size: 1.2rem;
  font-weight: bold;
`,ce=()=>{const s=A(o=>o.admin_app.permissions.settings?.roles.create);return e.jsx(p.Protect,{permissions:s,children:e.jsx(re,{})})};export{re as CreatePage,ce as ProtectedCreatePage};
