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