import{c as _t,a as V,l as bt}from"../chunks/cL9VaPgy.js";import{p as xt,v as $t,w as kt,x as Tt,s as B,f,l as n,y as d,a as e,b as yt,d as G,z as _,o as l,F as R,c as W,A as J,n as K,g as Q,B as D,t as U,h as L,j as nt,k as H,m as b,C as Y}from"../chunks/C75YQU_W.js";import{v as F}from"../chunks/D1kbsK6n.js";import{i as x}from"../chunks/B4FYT0oR.js";import{d as ut}from"../chunks/BAJkae7J.js";import{B as z,F as ot,T as ht,d as At}from"../chunks/Wy6oXzdU.js";import{i as St}from"../chunks/Cz-4yBSw.js";import{f as st}from"../chunks/D-5ps19L.js";t[R]="src/routes/dashboard/+page.svelte";var wt=V(W("You can earn <b> </b> if you spend <b> </b> by <b> </b>",1),t[R],[[98,4],[103,4],[106,7]]),It=V(W('🎉 Hit bonus (<a href="/">new card?</a>)',1),t[R],[[120,20]]),Ct=V(W("<!> <!>",1),t[R],[]),Mt=V(W('<p>How it works:</p> <div class="mbe.4 flex flex.align.center"><!> <p>-> Connect bank -> Activate tracking -> Hit requirements -> Earn bonus</p></div> <!> <!>',1),t[R],[[61,0],[62,0,[[64,2]]]]);function t(pt,X){_t(new.target),xt(X,!0,t);const h=$t(kt({plaidInstance:null,selectedCardId:""}),"state");Tt(async()=>{h.plaidInstance=(await J(window.Plaid.create({token:X.data.plaid.link_token,onExit(){h.selectedCardId=""},async onSuccess(A,$){if(K($.account.type,"credit",!1)){alert("Selected account is not a credit card");return}const v=new FormData;v.append("publicToken",A),v.append("cardId",h.selectedCardId);const N=(await J(fetch("?/plaidTokenExchange",{body:v,method:"POST"})))(),i=At((await J(N.text()))());if(h.selectedCardId="",K(i.status,200,!1)){alert("There was an error connecting the card");return}(await J(St()))()}})))()});var rt=Mt(),Z=B(f(rt),2),ft=G(Z);n(()=>z(ft,{href:"/dashboard/card",children:d(t,(A,$)=>{_();var v=l("Add Card");e(A,v)}),$$slots:{default:!0}}),"component",t,63,2,{componentTag:"Button"}),_(2),Q(Z);var dt=B(Z,2);{const A=d(t,function($,v=D){F(...arguments),n(()=>z($,{type:"submit",get loading(){return v().isLoading},children:d(t,(N,i)=>{_();var a=l("Sync");e(N,a)}),$$slots:{default:!0}}),"component",t,73,4,{componentTag:"Button"})});n(()=>ot(dt,{action:"?/sync",onresponse:$=>$.update({invalidateAll:!0}),content:A,$$slots:{content:!0}}),"component",t,66,0,{componentTag:"Form"})}var vt=B(dt,2);{const A=d(t,function(i,a=D){F(...arguments),_();var c=l();U(()=>L(c,`${a().nickname??""}
    ${a().plaidMask?` (${a().plaidMask})`:""}`)),e(i,c)}),$=d(t,function(i,a=D){F(...arguments),_();var c=wt();const S=nt(H(()=>ut(a().bonusDeadline).format("MMM D, YYYY")),"bonusDeadline");b(S);var w=B(f(c)),j=G(w);Q(w);var r=B(w,2),m=G(r);Q(r);var o=B(r,2),k=G(o,!0);Q(o),U((I,s)=>{L(j,`${I??""}
      ${a().bonusType??""}`),L(m,`$${s??""}`),L(k,b(S))},[()=>`${K(a().bonusType,"CASH")?"$":""}${st(a().bonusAmount)}`,()=>st(a().bonusSpend)]),e(i,c)}),v=d(t,function(i,a=D){F(...arguments);var c=Y(),S=f(c);{var w=r=>{var m=l();U(o=>L(m,`🚫 Closed ${o??""}`),[()=>ut(a().closedAt).format("MMM D, YYYY")]),e(r,m)},j=r=>{var m=Y(),o=f(m);{var k=s=>{var T=l("☹️ Missed bonus");e(s,T)},I=s=>{var T=Y(),u=f(T);{var C=g=>{var p=It();_(2),e(g,p)},M=g=>{var p=Y(),tt=f(p);{var at=y=>{var P=l("🏦 Link bank");e(y,P)},q=y=>{var P=Y(),ct=f(P);{var et=E=>{var O=l("🕵️ Activate tracking");e(E,O)},mt=E=>{var O=l();const lt=nt(H(()=>a().bonusSpend-a().totalSpent),"difference");b(lt);const it=nt(H(()=>Math.max(b(lt),0)),"bonusSpendRemaining");b(it),U(gt=>L(O,`💲 ${gt??""} until bonus`),[()=>st(b(it))]),e(E,O)};n(()=>x(ct,E=>{a().paymentId?E(mt,!1):E(et)},!0),"if",t,124,4)}e(y,P)};n(()=>x(tt,y=>{a().plaidAccessToken?y(q,!1):y(at)},!0),"if",t,121,4)}e(g,p)};n(()=>x(u,g=>{a().bonusSpend&&a().bonusSpend<=a().totalSpent?g(C):g(M,!1)},!0),"if",t,118,4)}e(s,T)};n(()=>x(o,s=>{a().bonusDeadline&&a().bonusDeadline<new Date&&a().totalSpent<a().bonusSpend?s(k):s(I,!1)},!0),"if",t,115,4)}e(r,m)};n(()=>x(S,r=>{a().closedAt?r(w):r(j,!1)}),"if",t,112,4)}e(i,c)}),N=d(t,function(i,a=D){F(...arguments);var c=Ct(),S=f(c);{var w=o=>{},j=o=>{var k=Y(),I=f(k);{var s=u=>{{let C=H(()=>K(h.selectedCardId,a().id));n(()=>z(u,{get loading(){return b(C)},onclick:()=>{h.selectedCardId=a().id,h.plaidInstance?.open()},children:d(t,(M,g)=>{_();var p=l("Connect");e(M,p)}),$$slots:{default:!0}}),"component",t,140,6,{componentTag:"Button"})}},T=u=>{var C=Y(),M=f(C);{var g=p=>{{const tt=d(t,function(q,y=D){F(...arguments),n(()=>z(q,{type:"submit",get loading(){return y().isLoading},children:d(t,(P,ct)=>{_();var et=l("Activate");e(P,et)}),$$slots:{default:!0}}),"component",t,157,10,{componentTag:"Button"})});let at=H(()=>`?/activate&cardId=${a().id}`);n(()=>ot(p,{get action(){return b(at)},onresponse:q=>q.update({invalidateAll:!0}),content:tt,$$slots:{content:!0}}),"component",t,150,6,{componentTag:"Form"})}};n(()=>x(M,p=>{a().paymentId||p(g)},!0),"if",t,149,4)}e(u,C)};n(()=>x(I,u=>{a().plaidAccessToken?u(T,!1):u(s)},!0),"if",t,139,4)}e(o,k)};n(()=>x(S,o=>{a().closedAt?o(w):o(j,!1)}),"if",t,136,4)}var r=B(S,2);{var m=o=>{{const k=d(t,function(s,T=D){F(...arguments),n(()=>z(s,{type:"submit",get loading(){return T().isLoading},children:d(t,(u,C)=>{_();var M=l("Close");e(u,M)}),$$slots:{default:!0}}),"component",t,170,10,{componentTag:"Button"})});let I=H(()=>`?/close&cardId=${a().id}`);n(()=>ot(o,{get action(){return b(I)},onresponse:s=>s.update({invalidateAll:!0}),content:k,$$slots:{content:!0}}),"component",t,163,6,{componentTag:"Form"})}};n(()=>x(r,o=>{a().closedAt||o(m)}),"if",t,162,4)}e(i,c)});n(()=>ht(vt,{get items(){return X.data.cards},headers:[{key:"name",text:"Name"},{key:"bonus",text:"Bonus"},{key:"status",text:"Status"},{key:"actions",text:"Actions"},{key:"plaidBank",text:"Bank"}],nameTd:A,bonusTd:$,statusTd:v,actionsTd:N,$$slots:{nameTd:!0,bonusTd:!0,statusTd:!0,actionsTd:!0}}),"component",t,77,0,{componentTag:"Table"})}return e(pt,rt),yt({...bt()})}export{t as component};
