import*as e from"react";import{useMutation as b,useQueryClient as U}from"@tanstack/react-query";import{useParams as C}from"react-router-dom";import{Button as E}from"@redocly/theme/components/Button/Button";import{H2 as w}from"@redocly/theme/components/Typography/H2";import{Admonition as P}from"@redocly/theme/markdoc/components/Admonition/Admonition";import{useTranslate as D}from"../../../../../client/app/hooks";import{Dialog as R}from"./Dialog";import{ButtonWithLoading as S}from"./ButtonWithLoading";import{ButtonsContainer as A}from"./RollAPIKey";import{DialogHelperText as T,DialogInputMultiline as x,DialogRow as K,DialogRowContent as j}from"./DialogStyledComponents";const z=p=>{const{id:o}=C(),{initialCallbackUrl:u,open:g,onOpen:k,onClose:s}=p,l=u||"",{translate:a}=D(),[r,n]=e.useState(l);e.useEffect(()=>{n(l)},[l]);const c=async()=>{await f(),s()},y=t=>{t.key==="Enter"&&!i&&c()},d=()=>{s(),n(l)},h=U(),{mutateAsync:f,isPending:i,error:m}=b({mutationKey:["UPDATE_APP"+o],mutationFn:async()=>{const t=await fetch(`/api/api-keys/apps/${o}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({callbackUrl:r})});if(!t.ok){const v=await t.json().catch(()=>null);throw new Error(v?.error||`Request failed with status ${t.status}`)}return t.json()},onSettled:()=>{h.invalidateQueries({queryKey:["APP:"+o]}),n(r)}});return e.createElement(R,{open:g,onClose:d,onOpen:k},e.createElement(e.Fragment,null,e.createElement(w,{"data-translation-key":"dev.edit.callbackUrls.dialog.title"},a("dev.edit.callbackUrls.dialog.title","Change callback URLs")),e.createElement(K,null,e.createElement(j,null,e.createElement(x,{value:r,onChange:t=>n(t.target.value),onKeyPress:y,placeholder:a("dev.edit.callbackUrls.dialog.placeholder","https://example.com/callback"),rows:3}),e.createElement(T,{"data-translation-key":"dev.edit.callbackUrls.dialog.hint"},a("dev.edit.callbackUrls.dialog.hint","Separate multiple URLs with a comma")))),m&&e.createElement(P,{type:"danger"},m.message," "),e.createElement(A,null,e.createElement(E,{onClick:d,"data-translation-key":"dev.edit.callbackUrls.dialog.cancel",variant:"text",size:"large"},a("dev.edit.callbackUrls.dialog.cancel","Cancel")),e.createElement(S,{variant:"primary",loading:i,onClick:c,disabled:i,"data-translation-key":"dev.edit.callbackUrls.dialog.save"},a("dev.edit.callbackUrls.dialog.save","Save changes")))))};export{z as EditCallbackUrlDialog};
