import{bI as P,u as j,r as h,et as y,eu as E,ev as $,ew as C,T as U,aD as V,ex as A,ey as B,ez as O,dX as u,j as g,bw as R,B as S,b4 as _,bs as z}from"./index-C1DmWO_C.js";import{u as H,a as Q}from"./index-DMOJMCC5.js";import{S as W}from"./SchematisedTagsForm-Cki5D0WB.js";const X=()=>{const s=P(),{id:t,fsId:e,fgId:a}=j(),[i,d]=h.useState(!1),{currentData:n}=y({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 E.deleteTag(+t,+e,+a,r)})),await $(+t,+e,+a,o),s(C.invalidateTags([{type:U.ProjectTags.FeatureGroupTag,id:+a}])),d(!1),"success"},[n,s,a,e,t]),i]},q=({onDone:s,tags:t,type:e=u.fv,tdVersion:a})=>{const{fsId:i,id:d,fvName:n,fvVersion:l,mId:o,vId:c}=j(),T=V({shouldUnregister:!1,defaultValues:{tags:t}}),[p,{isLoading:r}]=A(),[I,{isLoading:M}]=B(),[v,{isLoading:k}]=O(),[b,L]=X(),[x,{isLoading:D}]=H(),[F,{isLoading:G}]=Q(),N=h.useCallback(async m=>{const f=J(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(w=>I({pId:+d,fsId:+i,id:n,version:+l,tagName:w}).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(w=>F({projectId:d,modelId:`${o}_${c}`,tagName:w}).unwrap())),x({projectId:d,modelId:`${o}_${c}`,tags:m.tags}).unwrap().then(()=>{s()});throw new Error(`type ${e} is not supported`)},[p,x,v,I,F,i,n,l,o,s,d,t,a,e,b,c]);return g.jsx(R,{...T,children:g.jsxs(S,{width:"100%",flexDirection:"column",children:[g.jsx(W,{type:e,withoutDelete:e===u.td,isEdit:!0,withoutTitle:!0}),g.jsxs(S,{justifyContent:"flex-end",sx:{gap:"15px"},children:[g.jsx(_,{intent:"secondary",onClick:s,children:"cancel"}),g.jsx(z,{onClick:T.handleSubmit(N),isLoading:r||k||L||D||M||G,children:"save"})]})]})})},ee=h.memo(q),J=(s,t)=>{const e=s.map(i=>i.name),a=Object.keys(t);return e.filter(i=>!a.includes(i))};export{ee as S};
