import{bI as P,u as x,r as h,eP as C,eQ as D,eR as E,eS as U,Q as V,aC as y,eT as A,eU as Q,eV as R,ef as u,j as g,bw as O,$ as j,b1 as _,bs as B}from"./index-Cc-6WbPt.js";import{u as H,a as W}from"./index-UO3hMyLO.js";import{S as q}from"./SchematisedTagsForm-DGEwUJig.js";const z=()=>{const s=P(),{id:t,fsId:e,fgId:a}=x(),[i,d]=h.useState(!1),{currentData:n}=C({pId:+t,fsId:+e,fgId:+a},{skip:!a});return[h.useCallback(async({tags:o})=>{d(!0);const c=n.map(({name:r})=>r),T=Object.keys(o||{}),p=c.filter(r=>!T.includes(r));return await Promise.allSettled(p.map(async r=>{await D.deleteTag(+t,+e,+a,r)})),await E(+t,+e,+a,o),s(U.invalidateTags([{type:V.ProjectTags.FeatureGroupTag,id:+a}])),d(!1),"success"},[n,s,a,e,t]),i]},J=({onDone:s,tags:t,type:e=u.fv,tdVersion:a})=>{const{fsId:i,id:d,fvName:n,fvVersion:l,mId:o,vId:c}=x(),T=y({shouldUnregister:!1,defaultValues:{tags:t}}),[p,{isLoading:r}]=A(),[w,{isLoading:M}]=Q(),[b,{isLoading:k}]=R(),[v,L]=z(),[S,{isLoading:$}]=H(),[F,{isLoading:G}]=W(),N=h.useCallback(async m=>{const f=K(t,m.tags);if(e===u.fg)return v(m).then(()=>{s()});if(e===u.fv)return f.length>0&&await Promise.all(f.map(I=>w({pId:+d,fsId:+i,id:n,version:+l,tagName:I}).unwrap())),p({pId:+d,fsId:+i,id:n,version:+l,tags:m.tags}).unwrap().then(()=>{s()});if(e===u.td)return b({pId:+d,fsId:+i,fvName:n,fvVersion:+l,id:a,tags:m.tags}).unwrap().then(()=>{s()});if(e===u.modelVersion)return f.length>0&&await Promise.all(f.map(I=>F({projectId:d,modelId:`${o}_${c}`,tagName:I}).unwrap())),S({projectId:d,modelId:`${o}_${c}`,tags:m.tags}).unwrap().then(()=>{s()});throw new Error(`type ${e} is not supported`)},[p,S,b,w,F,i,n,l,o,s,d,t,a,e,v,c]);return g.jsx(O,{...T,children:g.jsxs(j,{width:"100%",flexDirection:"column",children:[g.jsx(q,{type:e,withoutDelete:e===u.td,isEdit:!0,withoutTitle:!0}),g.jsxs(j,{justifyContent:"flex-end",sx:{gap:"15px"},children:[g.jsx(_,{intent:"secondary",onClick:s,children:"cancel"}),g.jsx(B,{onClick:T.handleSubmit(N),isLoading:r||k||L||$||M||G,children:"save"})]})]})})},ee=h.memo(J),K=(s,t)=>{const e=s.map(i=>i.name),a=Object.keys(t);return e.filter(i=>!a.includes(i))};export{ee as S};
