import{B as e,Ft as t,H as n,It as r,K as i,L as a,Mt as o,Q as s,U as c,V as l,W as u,Z as d,_t as f,at as p,d as m,ft as h,ht as g,jt as _,mt as v,ot as y,pt as b,s as x,st as S,tt as C,yt as w}from"./BA99CCI5.js";import{a as T}from"./DP37y6wO.js";import"./xihTtKlq.js";import"./DGo8yE5j.js";import{t as E}from"./BKqLjepE.js";import{t as D}from"./BB59mDRy.js";import{r as O,t as k}from"./B66v-P2b.js";import{t as A}from"./D-E0nZEG.js";import{a as j}from"./DvT-Y1N62.js";import{t as M}from"./BBtCGbx62.js";import"./BymfG72d2.js";import{t as N}from"./7yuvYLu22.js";var P=u(`<div class="mt-1"><!></div>`),F=u(`<div class="rounded-xl border border-neutral-200 bg-white px-3 py-2.5 dark:border-neutral-700/60 dark:bg-neutral-800/50"><div class="flex items-center justify-between"><div><span class="text-sm font-medium text-neutral-900 dark:text-neutral-100"> </span> <!></div> <div class="flex items-center"><!></div></div></div>`),I=u(`<div class="space-y-3"><p class="text-sm text-neutral-600 dark:text-neutral-400">The following instances sync this entity. Would you like to push your changes now?</p> <div class="space-y-2"></div></div>`),L=u(`<!> <!>`,1);function R(u,R){o(R,!1);let z=g(),B=g(),V=x(R,`open`,12,!1),H=x(R,`redirectTo`,8),U=x(R,`affectedArrs`,8),W=x(R,`databaseId`,8),G=x(R,`entityName`,8),K=x(R,`entityType`,8,`qualityProfile`),q=g(new Map),J=g(new Map),Y=g(new Map);async function X(e){s(q).set(e,`syncing`),f(q,s(q));try{let t=await fetch(`/arr/${e}/resync`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({databaseId:W(),entityName:G(),entityType:K()})});if(t.ok)s(q).set(e,`done`);else{let n=await t.json();t.status===409?n.retryAfter?(s(q).set(e,`cooldown`),s(Y).set(e,n.retryAfter),f(Y,s(Y))):(s(q).set(e,`cooldown`),s(J).set(e,n.error||`Sync in progress`)):(s(q).set(e,`failed`),s(J).set(e,n.error||`Sync failed`))}}catch{s(q).set(e,`failed`),s(J).set(e,`Network error`)}f(q,s(q)),f(J,s(J))}function Z(){for(let e of U()){let t=s(q).get(e.instanceId);(t===`idle`||t===`failed`)&&X(e.instanceId)}}function Q(){V(!1),T(H())}function $(){V(!1),T(H())}function ee(e){switch(e){case`done`:return`success`;case`failed`:return`danger`;case`cooldown`:return`warning`;default:return`secondary`}}function te(e,t){switch(e){case`syncing`:return`Syncing...`;case`done`:return`Synced`;case`failed`:return s(J).get(t)||`Failed`;case`cooldown`:{let e=s(Y).get(t);return e?`Wait ${e}s`:s(J).get(t)||`Cooldown`}default:return`Pending`}}p(()=>(d(V()),d(U())),()=>{V()&&(f(q,new Map(U().map(e=>[e.instanceId,`idle`]))),f(J,new Map),f(Y,new Map))}),p(()=>(d(U()),s(q)),()=>{f(z,U().length>0&&U().every(e=>{let t=s(q).get(e.instanceId);return t===`done`||t===`failed`||t===`cooldown`}))}),p(()=>(d(U()),s(q)),()=>{f(B,U().some(e=>s(q).get(e.instanceId)===`syncing`))}),y(),m(),N(u,{get open(){return V()},header:`Sync Changes`,$$events:{cancel:Q},$$slots:{body:(o,u)=>{var f=I(),p=v(h(f),2);a(p,5,U,e=>e.instanceId,(a,o)=>{let u=w(()=>(s(q),s(o),C(()=>s(q).get(s(o).instanceId)||`idle`)));var f=F(),p=h(f),m=h(p),g=h(m),_=h(g,!0);r(g);var y=v(g,2),x=e=>{var a=P(),c=h(a);{let e=w(()=>(d(s(u)),C(()=>ee(s(u)))));M(c,{get variant(){return s(e)},size:`sm`,rounded:`md`,children:(e,r)=>{t();var a=i();S(e=>l(a,e),[()=>(d(s(u)),s(o),C(()=>te(s(u),s(o).instanceId)))]),n(e,a)},$$slots:{default:!0}})}r(a),n(e,a)};e(y,e=>{s(u)!==`idle`&&e(x)}),r(m);var T=v(m,2),j=h(T),N=t=>{var r=c(),i=b(r),a=e=>{{let t=w(()=>s(u)===`failed`?A:E);k(e,{text:`Sync`,get icon(){return s(t)},size:`xs`,variant:`primary`,$$events:{click:()=>X(s(o).instanceId)}})}},l=e=>{O(e,{size:16,class:`animate-spin text-neutral-400`})},d=e=>{D(e,{size:16,class:`text-green-500`})};e(i,e=>{s(u)===`idle`||s(u)===`failed`?e(a):s(u)===`syncing`?e(l,1):s(u)===`done`&&e(d,2)}),n(t,r)},I=e=>{O(e,{size:16,class:`animate-spin text-neutral-400`})},L=e=>{D(e,{size:16,class:`text-green-500`})},R=e=>{k(e,{text:`Retry`,get icon(){return A},size:`xs`,variant:`primary`,$$events:{click:()=>X(s(o).instanceId)}})};e(j,e=>{d(U()),C(()=>U().length>1)?e(N):s(u)===`syncing`?e(I,1):s(u)===`done`?e(L,2):(s(u)===`failed`||s(u)===`cooldown`)&&e(R,3)}),r(T),r(p),r(f),S(()=>l(_,(s(o),C(()=>s(o).instanceName)))),n(a,f)}),r(p),r(f),n(o,f)},footer:(t,r)=>{var i=L(),a=b(i);k(a,{text:`Skip`,get icon(){return j},get disabled(){return s(B)},$$events:{click:Q}});var o=v(a,2),c=e=>{k(e,{text:`Done`,get icon(){return D},variant:`primary`,$$events:{click:$}})},l=e=>{k(e,{text:`Sync All`,get icon(){return A},variant:`primary`,get disabled(){return s(B)},$$events:{click:Z}})};e(o,e=>{s(z)?e(c):e(l,-1)}),n(t,i)}}}),_()}export{R as t};