const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["EventsTable-D5GNxVvq.js","strapi-ye6TfKul.js","strapi-D6821bIJ.css","selectors-Br3dx8yO.js","useWebhooks-pXreJ-3p.js"])))=>i.map(i=>d[i]);
import{iv as j,fH as e,ck as g,cI as S,gB as Y,gP as L,f$ as I,dI as ee,aD as x,hU as C,g8 as te,jD as z,bm as R,c7 as $,fc as se,h_ as A,bU as w,cz as b,be as re,gZ as ne,ew as ae,gX as ie,gH as k,dJ as oe,f6 as le,M as de,x as N,bW as ce,h1 as D,hS as ue,d5 as he,an as ge,aZ as _,r as me,jG as F,dH as pe,bd as fe,e5 as q,e8 as U,ec as be,e6 as E,iC as xe,iF as je,iG as ke,hi as ye,bL as P,h0 as we,i$ as Se,fo as W}from"./strapi-ye6TfKul.js";import{s as ve}from"./selectors-Br3dx8yO.js";import{u as Ee}from"./useWebhooks-pXreJ-3p.js";const Ce=({children:t})=>{const{formatMessage:s}=j(),r=s({id:"Settings.webhooks.form.events",defaultMessage:"Events"});return e.jsxs(g,{direction:"column",alignItems:"stretch",gap:1,children:[e.jsx(S.Label,{"aria-hidden":!0,children:r}),e.jsx(Me,{"aria-label":r,children:t})]})},Me=z(se)`
  tbody tr:nth-child(odd) {
    background: ${({theme:t})=>t.colors.neutral100};
  }

  thead th span {
    color: ${({theme:t})=>t.colors.neutral500};
  }

  td,
  th {
    padding-block-start: ${({theme:t})=>t.spaces[3]};
    padding-block-end: ${({theme:t})=>t.spaces[3]};
    width: 6%;
    vertical-align: middle;
  }

  tbody tr td:first-child {
    /**
     * Add padding to the start of the first column to avoid the checkbox appearing
     * too close to the edge of the table
     */
    padding-inline-start: ${({theme:t})=>t.spaces[2]};
  }
`,Ie=()=>[{id:"Settings.webhooks.events.create",defaultMessage:"Create"},{id:"Settings.webhooks.events.update",defaultMessage:"Update"},{id:"app.utils.delete",defaultMessage:"Delete"},{id:"app.utils.publish",defaultMessage:"Publish"},{id:"app.utils.unpublish",defaultMessage:"Unpublish"}],Re=({getHeaders:t=Ie})=>{const{formatMessage:s}=j(),r=t();return e.jsx(Y,{children:e.jsxs(L,{children:[e.jsx(I,{children:e.jsx(ee,{children:s({id:"Settings.webhooks.event.select",defaultMessage:"Select event"})})}),r.map(a=>["app.utils.publish","app.utils.unpublish"].includes(a?.id??"")?e.jsx(I,{title:s({id:"Settings.webhooks.event.publish-tooltip",defaultMessage:"This event only exists for content with draft & publish enabled"}),children:e.jsx(x,{variant:"sigma",textColor:"neutral600",children:s(a)})},a.id):e.jsx(I,{children:e.jsx(x,{variant:"sigma",textColor:"neutral600",children:s(a)})},a.id))]})})},Ae=({providedEvents:t})=>{const s=t||Te(),{value:r=[],onChange:a}=C("events"),h="events",i=r,n=[],u=i.reduce((l,d)=>{const o=d.split(".")[0];return l[o]||(l[o]=[]),l[o].push(d),l},{}),f=(l,d)=>{const o=new Set(i);d?o.add(l):o.delete(l),a(h,Array.from(o))},c=(l,d)=>{const o=new Set(i);d?s[l].forEach(m=>{n.includes(m)||o.add(m)}):s[l].forEach(m=>o.delete(m)),a(h,Array.from(o))};return e.jsx(te,{children:Object.entries(s).map(([l,d])=>e.jsx(Pe,{disabledEvents:n,name:l,events:d,inputValue:u[l],handleSelect:f,handleSelectAll:c},l))})},Te=()=>({entry:["entry.create","entry.update","entry.delete","entry.publish","entry.unpublish"],media:["media.create","media.update","media.delete"]}),Pe=({disabledEvents:t=[],name:s,events:r=[],inputValue:a=[],handleSelect:h,handleSelectAll:i})=>{const{formatMessage:n}=j(),u=r.filter(o=>!t.includes(o)),f=a.length>0,c=a.length===u.length,l=()=>{i(s,!c)},d=5;return e.jsxs(L,{children:[e.jsx(R,{children:e.jsx($,{"aria-label":n({id:"global.select-all-entries",defaultMessage:"Select all entries"}),name:s,checked:f&&!c?"indeterminate":c,onCheckedChange:l,children:He(s)})}),r.map(o=>e.jsx(R,{textAlign:"center",children:e.jsx(g,{width:"100%",justifyContent:"center",children:e.jsx($,{disabled:t.includes(o),"aria-label":o,name:o,checked:a.includes(o),onCheckedChange:m=>h(o,!!m)})})},o)),r.length<d&&e.jsx(R,{colSpan:d-r.length})]})},He=t=>t.replace(/-/g," ").split(" ").map(s=>s.charAt(0).toUpperCase()+s.slice(1).toLowerCase()).join(" "),T={Root:Ce,Headers:Re,Body:Ae},$e=()=>e.jsxs(T.Root,{children:[e.jsx(T.Headers,{}),e.jsx(T.Body,{})]}),_e=z(de)`
  cursor: pointer;
`,Fe=()=>{const{formatMessage:t}=j(),s=A("HeadersInput",n=>n.addFieldRow),r=A("HeadersInput",n=>n.removeFieldRow),a=A("HeadersInput",n=>n.onChange),{value:h=[]}=C("headers"),i=n=>{h.length===1?a("headers",[{key:"",value:""}]):r("headers",n)};return e.jsxs(g,{direction:"column",alignItems:"stretch",gap:1,children:[e.jsx(S.Label,{children:t({id:"Settings.webhooks.form.headers",defaultMessage:"Headers"})}),e.jsxs(w,{padding:8,background:"neutral100",hasRadius:!0,children:[h.map((n,u)=>e.jsxs(b.Root,{gap:4,padding:2,children:[e.jsx(b.Item,{xs:12,col:6,direction:"column",alignItems:"stretch",children:e.jsx(qe,{name:`headers.${u}.key`,"aria-label":`row ${u+1} key`,label:t({id:"Settings.webhooks.key",defaultMessage:"Key"})})}),e.jsx(b.Item,{xs:12,col:6,direction:"column",alignItems:"stretch",children:e.jsxs(g,{alignItems:"flex-end",gap:2,children:[e.jsx(w,{style:{flex:1},children:e.jsx(re,{name:`headers.${u}.value`,"aria-label":`row ${u+1} value`,label:t({id:"Settings.webhooks.value",defaultMessage:"Value"}),type:"text"})}),e.jsx(ne,{width:"4rem",height:"4rem",onClick:()=>i(u),color:"primary600",label:t({id:"Settings.webhooks.headers.remove",defaultMessage:"Remove header row {number}"},{number:u+1}),type:"button",children:e.jsx(ae,{width:"0.8rem"})})]})})]},`${u}-${JSON.stringify(n.key)}`)),e.jsx(w,{paddingTop:4,children:e.jsx(_e,{type:"button",onClick:()=>{s("headers",{key:"",value:""})},startIcon:e.jsx(ie,{}),children:t({id:"Settings.webhooks.create.header",defaultMessage:"Create new header"})})})]})]})},qe=({name:t,label:s,...r})=>{const[a,h]=k.useState([...O]),{value:i}=C("headers"),n=C(t);k.useEffect(()=>{const c=O.filter(l=>!i?.some(d=>d.key!==n.value&&d.key===l));h(c)},[i,n.value]);const u=c=>{n.onChange(t,c)},f=c=>{h(l=>[...l,c]),c&&u(c)};return e.jsxs(S.Root,{name:t,error:n.error,children:[e.jsx(S.Label,{children:s}),e.jsx(oe,{...r,onClear:()=>u(""),onChange:u,onCreateOption:f,placeholder:"",creatable:!0,value:n.value,children:a.map(c=>e.jsx(le,{value:c,children:c},c))}),e.jsx(S.Error,{})]})},O=["A-IM","Accept","Accept-Charset","Accept-Encoding","Accept-Language","Accept-Datetime","Access-Control-Request-Method","Access-Control-Request-Headers","Authorization","Cache-Control","Connection","Content-Length","Content-Type","Cookie","Date","Expect","Forwarded","From","Host","If-Match","If-Modified-Since","If-None-Match","If-Range","If-Unmodified-Since","Max-Forwards","Origin","Pragma","Proxy-Authorization","Range","Referer","TE","User-Agent","Upgrade","Via","Warning"],Ue=({isPending:t,onCancel:s,response:r})=>{const{statusCode:a,message:h}=r??{},{formatMessage:i}=j();return e.jsx(w,{background:"neutral0",padding:5,shadow:"filterShadow",hasRadius:!0,children:e.jsxs(b.Root,{gap:4,style:{alignItems:"center"},children:[e.jsx(b.Item,{col:3,direction:"column",alignItems:"stretch",children:e.jsx(x,{children:i({id:"Settings.webhooks.trigger.test",defaultMessage:"Test-trigger"})})}),e.jsx(b.Item,{col:3,direction:"column",alignItems:"stretch",children:e.jsx(We,{isPending:t,statusCode:a})}),e.jsx(b.Item,{col:6,direction:"column",alignItems:"stretch",children:t?e.jsx(g,{justifyContent:"flex-end",children:e.jsx("button",{onClick:s,type:"button",children:e.jsxs(g,{gap:2,alignItems:"center",children:[e.jsx(x,{textColor:"neutral400",children:i({id:"Settings.webhooks.trigger.cancel",defaultMessage:"cancel"})}),e.jsx(N,{fill:"neutral400",height:"1.2rem",width:"1.2rem"})]})})}):e.jsx(Oe,{statusCode:a,message:h})})]})})},We=({isPending:t,statusCode:s})=>{const{formatMessage:r}=j();return t||!s?e.jsxs(g,{gap:2,alignItems:"center",children:[e.jsx(ce,{height:"1.2rem",width:"1.2rem"}),e.jsx(x,{children:r({id:"Settings.webhooks.trigger.pending",defaultMessage:"pending"})})]}):s>=200&&s<300?e.jsxs(g,{gap:2,alignItems:"center",children:[e.jsx(D,{fill:"success700",height:"1.2rem",width:"1.2rem"}),e.jsx(x,{children:r({id:"Settings.webhooks.trigger.success",defaultMessage:"success"})})]}):s>=300?e.jsxs(g,{gap:2,alignItems:"center",children:[e.jsx(N,{fill:"danger700",height:"1.2rem",width:"1.2rem"}),e.jsxs(x,{children:[r({id:"Settings.error",defaultMessage:"error"})," ",s]})]}):null},Oe=({statusCode:t,message:s})=>{const{formatMessage:r}=j();return t?t>=200&&t<300?e.jsx(g,{justifyContent:"flex-end",children:e.jsx(x,{textColor:"neutral600",ellipsis:!0,children:r({id:"Settings.webhooks.trigger.success.label",defaultMessage:"Trigger succeeded"})})}):t>=300?e.jsx(g,{justifyContent:"flex-end",children:e.jsx(g,{maxWidth:"25rem",justifyContent:"flex-end",title:s,children:e.jsx(x,{ellipsis:!0,textColor:"neutral600",children:s})})}):null:null},Ve=({handleSubmit:t,triggerWebhook:s,isCreating:r,isTriggering:a,triggerResponse:h,data:i})=>{const{formatMessage:n}=j(),[u,f]=k.useState(!1),c=ue($e,async()=>(await he(async()=>{const{EventsTableEE:d}=await import("./EventsTable-D5GNxVvq.js");return{EventsTableEE:d}},__vite__mapDeps([0,1,2,3,4]))).EventsTableEE),l=d=>Object.keys(d).length?Object.entries(d).map(([o,m])=>({key:o,value:m})):[{key:"",value:""}];return c?e.jsx(ge,{initialValues:{name:i?.name||"",url:i?.url||"",headers:l(i?.headers||{}),events:i?.events||[]},method:r?"POST":"PUT",onSubmit:t,validationSchema:Ne({formatMessage:n}),children:({isSubmitting:d,modified:o})=>e.jsxs(e.Fragment,{children:[e.jsx(_.Header,{primaryAction:e.jsxs(g,{width:"100%",gap:2,children:[e.jsx(F,{onClick:()=>{s(),f(!0)},variant:"tertiary",startIcon:e.jsx(pe,{}),disabled:r||a,fullWidth:!0,children:n({id:"Settings.webhooks.trigger",defaultMessage:"Trigger"})}),e.jsx(F,{startIcon:e.jsx(D,{}),type:"submit",disabled:!o,loading:d,fullWidth:!0,children:n({id:"global.save",defaultMessage:"Save"})})]}),title:r?n({id:"Settings.webhooks.create",defaultMessage:"Create a webhook"}):i?.name,navigationAction:e.jsx(w,{display:{initial:"none",medium:"block"},children:e.jsx(me,{fallback:"../webhooks"})})}),e.jsx(_.Content,{children:e.jsxs(g,{direction:"column",alignItems:"stretch",gap:4,children:[u&&e.jsx(Ue,{isPending:a,response:h,onCancel:()=>f(!1)}),e.jsx(w,{background:"neutral0",padding:8,shadow:"filterShadow",hasRadius:!0,children:e.jsxs(g,{direction:"column",alignItems:"stretch",gap:6,children:[e.jsx(b.Root,{gap:6,children:[{label:n({id:"global.name",defaultMessage:"Name"}),name:"name",required:!0,size:6,type:"string"},{label:n({id:"Settings.roles.form.input.url",defaultMessage:"Url"}),name:"url",required:!0,size:12,type:"string"}].map(({size:m,...v})=>e.jsx(b.Item,{col:m,xs:12,direction:"column",alignItems:"stretch",children:e.jsx(fe,{...v})},v.name))}),e.jsx(Fe,{}),e.jsx(c,{})]})})]})})]})}):null},Le=/(^$)|(^[A-Za-z][_0-9A-Za-z ]*$)/,ze=/(^$)|((https?:\/\/.*)(d*)\/?(.*))/,Ne=({formatMessage:t})=>q().shape({name:E().nullable().required(t({id:"Settings.webhooks.validation.name.required",defaultMessage:"Name is required"})).matches(Le,t({id:"Settings.webhooks.validation.name.regex",defaultMessage:"The name must start with a letter and only contain letters, numbers, spaces and underscores"})),url:E().nullable().required(t({id:"Settings.webhooks.validation.url.required",defaultMessage:"Url is required"})).matches(ze,t({id:"Settings.webhooks.validation.url.regex",defaultMessage:"The value must be a valid Url"})),headers:be(s=>{const r=U();if(s.length===1){const{key:a,value:h}=s[0];if(!a&&!h)return r}return r.of(q().shape({key:E().required(t({id:"Settings.webhooks.validation.key",defaultMessage:"Key is required"})).nullable(),value:E().required(t({id:"Settings.webhooks.validation.value",defaultMessage:"Value is required"})).nullable()}))}),events:U()}),V=t=>({...t,headers:t.headers.reduce((s,{key:r,value:a})=>(r!==""&&(s[r]=a),s),{})}),B=()=>{const{formatMessage:t}=j(),r=xe("/settings/webhooks/:id")?.params.id,a=r==="create",h=je(),{toggleNotification:i}=ke(),{_unstableFormatAPIError:n,_unstableFormatValidationErrors:u}=ye(),f=k.useCallback(n,[]),[c,l]=k.useState(!1),[d,o]=k.useState(),{isLoading:m,webhooks:v,error:M,createWebhook:G,updateWebhook:Z,triggerWebhook:X}=Ee({id:r},{skip:a});k.useEffect(()=>{M&&i({type:"danger",message:f(M)})},[M,i,f]);const J=async()=>{try{l(!0);const y=await X(r);if("error"in y){i({type:"danger",message:n(y.error)});return}o(y.data)}catch{i({type:"danger",message:t({id:"notification.error",defaultMessage:"An error occurred"})})}finally{l(!1)}},K=async(y,H)=>{try{if(a){const p=await G(V(y));if("error"in p){W(p.error)&&p.error.name==="ValidationError"?H.setErrors(u(p.error)):i({type:"danger",message:n(p.error)});return}i({type:"success",message:t({id:"Settings.webhooks.created"})}),h(`../webhooks/${p.data.id}`,{replace:!0})}else{const p=await Z({id:r,...V(y)});if("error"in p){W(p.error)&&p.error.name==="ValidationError"?H.setErrors(u(p.error)):i({type:"danger",message:n(p.error)});return}i({type:"success",message:t({id:"notification.form.success.fields"})})}}catch{i({type:"danger",message:t({id:"notification.error",defaultMessage:"An error occurred"})})}};if(m)return e.jsx(P.Loading,{});const[Q]=v??[];return e.jsxs(we,{children:[e.jsx(P.Title,{children:t({id:"Settings.PageTitle",defaultMessage:"Settings - {name}"},{name:"Webhooks"})}),e.jsx(Ve,{data:Q,handleSubmit:K,triggerWebhook:J,isCreating:a,isTriggering:c,triggerResponse:d})]})},De=()=>{const t=Se(ve);return e.jsx(P.Protect,{permissions:t.settings?.webhooks.update,children:e.jsx(B,{})})},Xe=Object.freeze(Object.defineProperty({__proto__:null,EditPage:B,ProtectedEditPage:De},Symbol.toStringTag,{value:"Module"}));export{B as E,Xe as a,T as b};
