import{R as _,o as g,r as d,k as D,cB as T,l as W,aD as B,d6 as G,j as o,a_ as M,h as z,dP as H,G as J,dQ as Q,az as $,dR as K,dS as X,aR as Y,dT as Z,ag as R,dU as v,dV as x,dW as C,aS as f}from"./app-BgLa5dit.js";import{F as ee}from"./w68yffRy.chunk.js";import{E as te}from"./kJGoOu-N.chunk.js";import"./BfMtaC_g.chunk.js";function oe(c){const[E,L]=_.useState(!1),S=g(Y.currentUser()),u=g(d.clip(c.clipId)),h=g(Z.contentRegions()),l=D(),I=T(),k=W(),P=B(G.ResourceUpload);async function U(t,r){L(!0);const i=t.resources.filter(e=>e.typeId===R.Resource),n=t.resources.filter(e=>e.typeId===R.Link);try{await Promise.all([y(i),w(i),j(i),q(n),A(n),F(n)]),I([`clip:${c.clipId}`]),k.success("Successfully updated resources.")}catch{k.error("Something went wrong while updating resources. Please try again.")}finally{L(!1),r({reset:!1})}}async function y(t){const r=t.filter(e=>!e.id).map(e=>l(d.addResource(c.clipId,e))),n=(await Promise.all(r)).map(e=>{const a={...e,contentRegions:t.find(s=>s.url===e.url).contentRegions};return l(d.updateResourceContentRegions(a))});return Promise.all(n)}function w(t){const r=u.data.resources.data.filter(s=>s.typeId===R.Resource),n=t.filter(s=>{if(!s.id)return!1;const p=r.find(m=>m.id.toString()===s.id.toString());return!f.isEqual(f.omit(s,["contentRegions"]),f.omit(p,["contentRegions"]))}).map(s=>l(d.updateResource(c.clipId,s))),a=t.filter(s=>{var m;const p=r.find(V=>s.url===V.url);return p?!f.isEqual(s.contentRegions,(m=p.contentRegions)==null?void 0:m.data):!1}).map(s=>l(d.updateResourceContentRegions(s)));return Promise.all([...n,...a])}function j(t){const n=u.data.resources.data.filter(e=>e.typeId===R.Resource).filter(e=>!t.find(a=>{var s;return((s=a.id)==null?void 0:s.toString())===e.id.toString()})).map(e=>l(d.removeResource(c.clipId,e.id)));return Promise.all(n)}function q(t){const i=t.filter(n=>!n.id).map(n=>l(d.addLink(c.clipId,n)));return Promise.all(i)}function A(t){const r=u.data.links.data.filter(e=>e.typeId===R.Link),n=t.filter(e=>{if(!e.id)return!1;const a=r.find(s=>s.id.toString()===e.id.toString());return e.name!==a.name||e.url!==a.url}).map(e=>l(d.updateLink(c.clipId,e)));return Promise.all(n)}function F(t){const n=u.data.links.data.filter(e=>e.typeId===R.Link).filter(e=>!t.find(a=>{var s;return((s=a.id)==null?void 0:s.toString())===e.id.toString()})).map(e=>l(d.removeLink(c.clipId,e.id)));return Promise.all(n)}function O(t){return new Promise(r=>{P.addFiles(t);const i=[];function n(e){i.push(e.toJSON()),i.length===t.length&&(r(i),v.Radio.channel(x.RESOURCE_UPLOAD).off(C.UPLOAD_COMPLETE,n))}v.Radio.channel(x.RESOURCE_UPLOAD).on(C.UPLOAD_COMPLETE,n)})}function b(){return!u.hasCompleted||!h.hasCompleted}if(b())return o.jsx(M,{});const N=[...u.data.resources.data.map(t=>({...t,contentRegions:t.contentRegions.data})),...u.data.links.data];return o.jsxs(o.Fragment,{children:[!!S.data&&o.jsx(te,{clipId:c.clipId,activeAction:z.Clips.EDIT_CLIP_RESOURCES,currentUser:S.data}),!!u.data&&o.jsx(H.Provider,{value:h.data.data,children:o.jsx(J,{initialValues:{resources:N,newLink:{url:"",name:""}},validationSchema:Q,onSubmit:U,enableReinitialize:!1,children:o.jsxs($,{children:[o.jsx(K,{onResourceFilesAdded:O,allowNoResources:!1,isSaving:E,hideCheckbox:!0,lockNav:!0}),o.jsx(X,{}),o.jsx("div",{className:"d-flex justify-content-end",children:o.jsx(ee,{variant:"dark"})})]})})})]})}export{oe as EditClipResourcesView};
//# sourceMappingURL=Drxf2LYM.chunk.js.map
