import{aA as F,j as e,P as p,a$ as L,w as T,a as k,ak as D,r as B,b as N,v as q,b0 as V,b1 as G,b2 as H,b3 as _,at as O,b4 as Q,aE as U,b5 as z,B as b,b6 as W,e as u,D as y,E as J,a8 as l,T as R,H as g,M as d,ay as K,b7 as X,aF as P,aG as M,aI as E,k as Y}from"./strapi-D_Y4QZhh.js";import{P as Z}from"./Permissions-Dhve40bL.js";import{f as ee}from"./index-rFbexmEd.js";import"./groupBy-DlVuwzo5.js";import"./_baseEach-xZsTMLD4.js";import"./index-BRVyLNfZ.js";const se=U().shape({name:P().required(M.required.id),description:P().required(M.required.id)}),re=()=>{const{id:s}=L(),{toggleNotification:o}=T(),{formatMessage:r}=k(),S=D(),m=B.useRef(null),{trackUsage:h}=N(),{_unstableFormatAPIError:f,_unstableFormatValidationErrors:x}=q(),{isLoading:w,currentData:j}=V({role:s??""}),{currentData:C,isLoading:v}=G({id:s},{skip:!s,refetchOnMountOrArgChange:!0}),[I]=H(),[$]=_(),A=async(c,t)=>{try{h(s?"willDuplicateRole":"willCreateNewRole");const a=await I(c);if("error"in a){E(a.error)&&a.error.name==="ValidationError"?t.setErrors(x(a.error)):o({type:"danger",message:f(a.error)});return}const{permissionsToSend:n}=m.current?.getPermissions()??{};if(a.data.id&&Array.isArray(n)&&n.length>0){const i=await $({id:a.data.id,permissions:n});if("error"in i){E(i.error)&&i.error.name==="ValidationError"?t.setErrors(x(i.error)):o({type:"danger",message:f(i.error)});return}}o({type:"success",message:r({id:"Settings.roles.created",defaultMessage:"created"})}),S(`../roles/${a.data.id.toString()}`,{replace:!0})}catch{o({type:"danger",message:r({id:"notification.error",defaultMessage:"An error occurred"})})}};return w&&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:A,validationSchema:se,validateOnChange:!1,children:({values:c,errors:t,handleReset:a,handleChange:n,isSubmitting:i})=>e.jsx(z,{children:e.jsxs(e.Fragment,{children:[e.jsx(b.Header,{primaryAction:e.jsxs(u,{gap:2,children:[e.jsx(y,{variant:"secondary",onClick:()=>{a(),m.current?.resetForm()},children:r({id:"app.components.Button.reset",defaultMessage:"Reset"})}),e.jsx(y,{type:"submit",loading:i,startIcon:e.jsx(J,{}),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(W,{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(ae,{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,{col:6,direction:"column",alignItems:"stretch",children:e.jsxs(d.Root,{name:"name",error:t.name&&r({id:t.name}),required:!0,children:[e.jsx(d.Label,{children:r({id:"global.name",defaultMessage:"Name"})}),e.jsx(K,{onChange:n,value:c.name}),e.jsx(d.Error,{})]})}),e.jsx(g.Item,{col:6,direction:"column",alignItems:"stretch",children:e.jsxs(d.Root,{name:"description",error:t.description&&r({id:t.description}),children:[e.jsx(d.Label,{children:r({id:"global.description",defaultMessage:"Description"})}),e.jsx(X,{onChange:n,value:c.description})]})})]})]})}),e.jsx(l,{shadow:"filterShadow",hasRadius:!0,children:e.jsx(Z,{isFormDisabled:!1,ref:m,permissions:C,layout:j})})]})})]})})})]})},ae=Y.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=F(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};
