import{bH as P,u as x,r as h,eN as C,eO as D,eP as E,eQ as y,Q as U,aC as V,eR as A,eS as O,eT as Q,ee as u,j as g,bv as R,$ as j,b1 as H,br as _}from"./index-46ma1THH.js";import{u as B,a as W}from"./index-D241LvV7.js";import{S as q}from"./SchematisedTagsForm-dh4vAE2D.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(y.invalidateTags([{type:U.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=V({shouldUnregister:!1,defaultValues:{tags:t}}),[p,{isLoading:r}]=A(),[w,{isLoading:M}]=O(),[v,{isLoading:k}]=Q(),[b,L]=z(),[S,{isLoading:N}]=B(),[F,{isLoading:$}]=W(),G=h.useCallback(async m=>{const f=K(t,m.tags);if(e===u.fg)return b(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 v({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,v,w,F,i,n,l,o,s,d,t,a,e,b,c]);return g.jsx(R,{...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(H,{intent:"secondary",onClick:s,children:"cancel"}),g.jsx(_,{onClick:T.handleSubmit(G),isLoading:r||k||L||N||M||$,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};
