import{bJ as I,a$ as v,w as z,a as O,v as R,ab as $,bM as q,r as L,bN as w,j as u,P as b,bO as T,ad as G}from"./strapi-BZkmC8So.js";import{C as U,T as Q}from"./Form-DUXdXhmT.js";import{u as k}from"./hooks-BsyjD0Ze.js";import"./sortable.esm-CUOrBiV4.js";import"./FieldTypeIcon-frWP0K-o.js";const B=I.injectEndpoints({endpoints:e=>({getComponentConfiguration:e.query({query:t=>`/content-manager/components/${t}/configuration`,transformResponse:t=>t.data,providesTags:(t,o,s)=>[{type:"ComponentConfiguration",id:s}]}),updateComponentConfiguration:e.mutation({query:({uid:t,...o})=>({url:`/content-manager/components/${t}/configuration`,method:"PUT",data:o}),transformResponse:t=>t.data,invalidatesTags:(t,o,{uid:s})=>[{type:"ComponentConfiguration",id:s},{type:"ContentTypeSettings",id:"LIST"}]})})}),{useGetComponentConfigurationQuery:D,useUpdateComponentConfigurationMutation:H}=B,J=()=>{const{slug:e}=v(),{toggleNotification:t}=z(),{formatMessage:o}=O(),{_unstableFormatAPIError:s}=R(),{components:f,fieldSizes:E,schema:r,error:i,isLoading:c,isFetching:F}=$(void 0,{selectFromResult:a=>{const y=a.data?.components.find(n=>n.uid===e),C=a.data?.components.reduce((n,d)=>(n[d.uid]=d,n),{}),p=q(y?.attributes,C),m=Object.entries(a.data?.fieldSizes??{}).reduce((n,[d,{default:j}])=>(n[d]=j,n),{});return{isFetching:a.isFetching,isLoading:a.isLoading,error:a.error,components:p,schema:y,fieldSizes:m}}});L.useEffect(()=>{i&&t({type:"danger",message:s(i)})},[i,s,t]);const{data:g,isLoading:M,isFetching:x,error:l}=D(e??"");L.useEffect(()=>{l&&t({type:"danger",message:s(l)})},[l,s,t]);const h=M||c||x||F,S=L.useMemo(()=>g&&!h?K(g,{schema:r,components:f}):{layout:[],components:{},metadatas:{},options:{},settings:w},[g,h,r,f]),[P]=H(),N=async a=>{try{const y=Object.entries(g?.component.metadatas??{}).reduce((p,[m,{edit:n,list:d}])=>{const{__temp_key__:j,size:V,name:W,...A}=a.layout.flatMap(_=>_.children).find(_=>_.name===m)??{};return p[m]={edit:{...n,...A},list:d},p},{}),C=await P({layouts:{edit:a.layout.map(p=>p.children.reduce((m,{name:n,size:d})=>n!==Q?[...m,{name:n,size:d}]:m,[])),list:g?.component.layouts.list},settings:G(a.settings,"displayName",void 0),metadatas:y,uid:e});"data"in C?t({type:"success",message:o({id:"notification.success.saved",defaultMessage:"Saved"})}):t({type:"danger",message:s(C.error)})}catch{t({type:"danger",message:o({id:"notification.error",defaultMessage:"An error occurred"})})}};return h?u.jsx(b.Loading,{}):l||i||!r?u.jsx(b.Error,{}):u.jsxs(u.Fragment,{children:[u.jsx(b.Title,{children:`Configure ${S.settings.displayName} Edit View`}),u.jsx(U,{onSubmit:N,attributes:r.attributes,fieldSizes:E,layout:S})]})},K=(e,{schema:t,components:o})=>{const s=T(e.component.layouts.edit,t?.attributes,e.component.metadatas,{configurations:e.components,schemas:o}),f=Object.entries(e.components).reduce((r,[i,c])=>(r[i]={layout:T(c.layouts.edit,o[i].attributes,c.metadatas),settings:{...c.settings,icon:o[i].info.icon,displayName:o[i].info.displayName}},r),{}),E=Object.entries(e.component.metadatas).reduce((r,[i,c])=>({...r,[i]:c.edit}),{});return{layout:[s],components:f,metadatas:E,options:{...t?.options,...t?.pluginOptions},settings:{...e.component.settings,displayName:t?.info.displayName}}},nt=()=>{const e=k(t=>t.admin_app.permissions.contentManager?.componentsConfigurations);return u.jsx(b.Protect,{permissions:e,children:u.jsx(J,{})})};export{J as ComponentConfigurationPage,nt as ProtectedComponentConfigurationPage};
