import*as e from"react";import{useMutation as E,useQueryClient as A}from"@tanstack/react-query";import{useParams as C}from"react-router-dom";import{Button as h}from"@redocly/theme/components/Button/Button";import{H3 as v}from"@redocly/theme/components/Typography/H3";import{Admonition as P}from"@redocly/theme/markdoc/components/Admonition/Admonition";import{useTranslate as S}from"../../../../../client/app/l10n/hooks";import{ButtonWithLoading as b}from"./ButtonWithLoading";import{Dialog as U}from"./Dialog";import I from"./ApisList";import{ButtonsWrapper as T}from"./ApiKeyItem";function W(p){const m=A(),{credential:a,open:c,onClose:n,onOpen:u}=p,{translate:i}=S(),{id:o}=C(),r=e.useMemo(()=>a.apiProductsStatus?.map(t=>t.apiId||t.id),[a]),[l,y]=e.useState(r),{isPending:d,error:s,mutateAsync:g}=E({mutationKey:["MUTATE_CRED"+o],mutationFn:()=>fetch(`/api/api-keys/apps/${o}/credentials/${a.id}`,{method:"PUT",body:JSON.stringify({apiProductIds:l,clientId:a.clientId,clientSecret:a.clientSecret})}).then(t=>t.json()),onSuccess:()=>{m.invalidateQueries({queryKey:["APP:"+o]})}}),f=async()=>{await g(),n()},k=t=>{y(t||[])};return e.createElement(e.Fragment,null,e.createElement(U,{open:c,onClose:n,onOpen:u},e.createElement(e.Fragment,null,e.createElement(v,{"data-translation-key":"dev.update.key.dialog.title"},i("dev.update.key.dialog.title","Update key")),e.createElement(I,{initialEnabledApis:r,onChange:k}),s?e.createElement(P,{type:"danger"}," ",s.message," "):null,e.createElement(T,null,e.createElement(h,{onClick:n,"data-translation-key":"dev.update.key.dialog.cancel"},i("dev.update.key.dialog.cancel","Cancel")),e.createElement(b,{variant:"primary",onClick:f,disabled:d||l.length<1,loading:d,"data-translation-key":"dev.update.key.dialog.update"},i("dev.update.key.dialog.update","Update key"))))))}export{W as UpdateKey};
