1import{j as e,r as f}from"./react-vendor-BvbMP1YH.js";import{L as M}from"./router-BfdCJaCf.js";import{c as B,B as b,r as $,N as W,O as Y,P as J,Q as Z,m as X,o as ee,p as se,q as te,aA as ae,aB as ne,av as re,aC as oe,J as le,L as H}from"./index-C0Lc_zJK.js";import{P as U}from"./page-column-GLL5Vfek.js";import{T as ie,a as de,b as ce}from"./tabs-DyWrFaK7.js";import{S as pe}from"./switch-B4U-AOKT.js";import{S as ue,a as xe,b as he,c as me,d as fe}from"./select-DXAqfqx0.js";import{F as ge,U as be,E as D}from"./UploadCsvBlock-rXYbtYKW.js";import{D as ve,b as F,h as ye,f as L,L as je,C as we,s as Ne,l as Se,c as C,a as _e,d as O,e as ke,g as Ce,i as Re,r as Ae,j as Le,k as Me,R as Ee,m as Pe,n as Te,p as De,o as Oe,q as qe,u as ze,t as Be,v as $e,w as I,x as He,y as Ue,z as Fe,A as Ie}from"./useRecoveryActions-CKRe2o-t.js";import{P as Ve}from"./page-header-CjafyNq8.js";import{B as Ge}from"./badge-CGslW8tu.js";import"./radix-DczJy1ss.js";import"./supabase-BRLmpgyC.js";import"./charts-Debx4V90.js";import"./variableRegistry-Kl1098ZR.js";import"./index-JhFw7KRN.js";import"./customerImport-CX6MRK2V.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const Ke=B("CalendarDays",[["path",{d:"M8 2v4",key:"1cmpym"}],["path",{d:"M16 2v4",key:"4m81vk"}],["rect",{width:"18",height:"18",x:"3",y:"4",rx:"2",key:"1hopcy"}],["path",{d:"M3 10h18",key:"8toen8"}],["path",{d:"M8 14h.01",key:"6423bh"}],["path",{d:"M12 14h.01",key:"1etili"}],["path",{d:"M16 14h.01",key:"1gbofw"}],["path",{d:"M8 18h.01",key:"lrp35t"}],["path",{d:"M12 18h.01",key:"mhygvu"}],["path",{d:"M16 18h.01",key:"kzsmim"}]]);/** 7 * @license lucide-react v0.462.0 - ISC 8 * 9 * This source code is licensed under the ISC license. 10 * See the LICENSE file in the root directory of this source tree. 11 */const Qe=B("Settings2",[["path",{d:"M20 7h-9",key:"3s1dr2"}],["path",{d:"M14 17H5",key:"gfn3mx"}],["circle",{cx:"17",cy:"17",r:"3",key:"18b49y"}],["circle",{cx:"7",cy:"7",r:"3",key:"dfmy0x"}]]);function We({brandId:a,metrics:s,range:n,onRangeChange:i,onToggleRecovery:x,togglePending:l,uploadOpen:d,onUploadOpenChange:c,now:p}){const h=(s==null?void 0:s.recovery_enabled)??!1;return e.jsxs(Ve,{as:"header",title:"Customer Recovery",description:ye(s),children:[e.jsxs("div",{className:"mt-8 flex flex-wrap items-center gap-2.5",children:[e.jsxs("label",{className:"flex h-[42px] cursor-pointer items-center gap-2.5 rounded-full border border-hairline bg-fillbox-soft pl-2.5 pr-4 transition-colors hover:bg-fillbox",children:[e.jsx(pe,{checked:h,disabled:l||!s,onCheckedChange:x,"aria-label":"Recovery on"}),e.jsx("span",{className:"text-[14px] font-medium tracking-[-0.01em] text-foreground",children:h?"Recovery on":"Recovery off"})]}),e.jsxs(b,{variant:"secondary",size:"md",shape:"pill",surface:"toolbar",onClick:()=>c(!0),children:[e.jsx(ge,{className:"h-4 w-4","aria-hidden":!0}),"Upload CSV"]}),e.jsxs(ue,{value:n.key,onValueChange:t=>i(t),children:[e.jsx(xe,{className:"h-[42px] w-[184px] rounded-full border-transparent bg-fillbox pl-4 pr-3 text-[14px] font-medium shadow-none transition-colors hover:bg-fillbox-track",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ke,{className:"h-4 w-4 shrink-0 opacity-80","aria-hidden":!0}),e.jsx(he,{})]})}),e.jsx(me,{children:ve.map(t=>e.jsx(fe,{value:t,children:F(t,p).label},t))})]}),e.jsx(b,{variant:"secondary",size:"md",shape:"pill",surface:"toolbar",asChild:!0,children:e.jsxs(M,{to:$.recoveryRules(),children:[e.jsx(Qe,{className:"h-4 w-4","aria-hidden":!0}),"Recovery rules"]})})]}),e.jsx(W,{open:d,onOpenChange:c,children:e.jsxs(Y,{className:"sm:max-w-lg",children:[e.jsx(J,{children:e.jsx(Z,{children:"Upload a file"})}),e.jsx("p",{className:"text-[15px] leading-[1.6] text-muted-foreground",children:"A newer file is also how a payment gets confirmed while live payment status is unavailable."}),e.jsx(be,{brandId:a,purpose:"payment_recovery",voice:"plain",title:"Upload a file of payments that did not go through."})]})})]})}function k({value:a,label:s}){return e.jsxs("div",{className:"rounded-[13px] border border-hairline bg-fillbox-soft px-5 py-[18px]",children:[e.jsx("p",{className:"text-[24px] font-bold leading-none tracking-[-0.02em] tabular-nums text-foreground",children:a}),e.jsx("p",{className:"mt-1 text-[12.5px] leading-[1.4] text-label-faint",children:s})]})}function Ye({metrics:a}
11){if(!a)return null;const s=a.summary;return e.jsxs("section",{className:"grid grid-cols-2 gap-3 sm:grid-cols-4",children:[e.jsx(k,{value:L(s.recovered_cents),label:"Recovered"}),e.jsx(k,{value:String(s.recovered_customer_count),label:s.recovered_customer_count===1?"Customer back":"Customers back"}),e.jsx(k,{value:L(s.in_recovery_cents),label:"Still in recovery"}),e.jsx(k,{value:String(s.stopped_count),label:"Stopped"})]})}function V({sent:a,total:s,className:n}){if(s<=0)return null;const i=Math.max(0,Math.min(a,s));return e.jsx("span",{"aria-hidden":!0,className:`inline-flex shrink-0 items-center gap-[3px] align-middle ${n??""}`,children:Array.from({length:s},(x,l)=>e.jsx("span",{className:`h-[3px] w-3.5 rounded-full ${l<i?"bg-primary":"bg-fillbox-track"}`},l))})}const q=25,z="grid w-full grid-cols-2 items-baseline gap-x-4 gap-y-1 md:grid-cols-[minmax(0,1.05fr)_6.5rem_minmax(0,1fr)_8rem_minmax(0,1.65fr)]",Je={attention:"warning",live:"accent",good:"success",quiet:"quiet"};function Ze({cases:a,total:s,allowRecurring:n,rulesApproved:i,onSelect:x}){const[l,d]=f.useState(q);if(a.length===0)return e.jsx("div",{className:"rounded-[18px] border border-sheet-border bg-sheet px-7 py-7 shadow-sheet",children:e.jsx("p",{className:"text-[15px] text-label",children:je.empty})});const c=a.slice(0,l),p=a.length>c.length,h=!p&&s>a.length;return e.jsxs("div",{className:"overflow-hidden rounded-[18px] border border-sheet-border bg-sheet shadow-sheet",children:[e.jsx("div",{className:`${z} hidden border-b border-hairline px-6 pb-3.5 pt-6 md:grid`,"aria-hidden":!0,children:we.map((t,o)=>e.jsx("span",{className:`text-[15.5px] font-semibold tracking-[-0.015em] text-foreground ${o===1?"text-right":""}`,children:t},t))}),e.jsx("ul",{children:c.map(t=>{var g;const o=Ne(t.status,((g=t.sequence)==null?void 0:g.status)??null),r=Se(t);return e.jsx("li",{children:e.jsx(b,{variant:"ghost",size:"xl",surface:"list",width:"full",justify:"start",onClick:()=>x(t.id),children:e.jsxs("span",{className:`${z} text-left font-normal`,children:[e.jsx("span",{className:"truncate text-[15.5px] font-medium tracking-[-0.01em] text-foreground",children:C(t)}),e.jsx("span",{className:"text-right text-[15.5px] font-semibold tabular-nums text-foreground",children:_e(t,{recovered:t.status==="recovered",allowRecurring:n})}),e.jsx("span",{className:"col-span-2 truncate text-[15.5px] text-foreground md:col-span-1",title:O(t.category),children:O(t.category)}),e.jsx("span",{className:"col-span-2 md:col-span-1",children:e.jsx(Ge,{variant:Je[ke(t.status)],className:"px-[11px] py-[5px] text-[12.5px] font-semibold tracking-[-0.01em]",children:o})}),e.jsxs("span",{className:"col-span-2 flex min-w-0 items-center gap-2 md:col-span-1",title:r,children:[e.jsx(V,{sent:t.sentCount,total:t.totalSteps}),e.jsx("span",{className:"truncate text-[15px] text-label",children:r})]})]})})},t.id)})}),e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3 border-t border-hairline px-6 py-4",children:[e.jsx("p",{className:"text-[13.5px] text-label-faint",children:Ce({shown:c.length,total:s,rulesApproved:i})}),p?e.jsx(b,{variant:"ghost",size:"sm",surface:"inline",onClick:()=>d(t=>t+q),children:"Show more"}):h?e.jsx("p",{className:"text-[13.5px] text-label-faint",children:"Narrow the date range to see the rest."}):null]})]})}function j({label:a,children:s}){return e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-[14.5px] font-medium text-foreground",children:a}),e.jsx("div",{className:"text-[15px] leading-[1.6] text-muted-foreground",children:s})]})}function Xe({brandId:a,recoveryCase:s,onClose:n,onPause:i,onStop:x,onConfirmPaid:l,onRevokeOffer:d,busy:c}){const[p,h]=f.useState(!1);return e.jsx(X,{open:!!s,onOpenChange:t=>!t&&n(),children:e.jsx(ee,{side:"right",className:"w-full overflow-y-auto sm:max-w-xl",children:s?e.jsxs(e.Fragment,{children:[e.jsx(se,{children:e.jsx(te,{children:C(s)})}),e.jsx(es,{brandId:a,c:s,showLaterSteps:p,setShowLaterSteps:h,onPause:i,onStop:x,onConfirmPaid:l,onRevokeOffer:d,busy:c})]}):null})})}function es({brandId:a,c:s,showLaterSteps:n,setShowLaterSteps:i,onPause:x,onStop:l,onConfirmPaid:d,onRevokeOffer:c,busy:p}){var N,u;const h=Re(s.recovery_url_confidence),t=s.messages.find(v=>v.step_number===1)??s.messages[0]??null,o=s.messages.filter(v=>v.id!==(t==null?void 0:t.id)),r=((N=s.sequence)==null?void 0:N.id)??null,g=!!r&&!["stopped","completed","failed"].includes(s.sequence.status),w=!!s.recovery_basis;return e.jsxs("div",{className:"space-y-6 pt-5",children:[s.status==="needs_review"?e.jsxs("p",{className:"rounded-[12px] border border-border/70 bg-muted/40 px-4 py-3 text-[15px] leading-[1.6] text-foreground
11/90",children:[e.jsx("span",{className:"font-medium text-foreground",children:"Held for review."})," ",Ae(s.review_reason),"."]}):null,e.jsxs("div",{className:"space-y-4",children:[e.jsx(j,{label:"What happened",children:Le(s.category,s.occurred_at??s.expected_payment_at)}),e.jsx(j,{label:"Amount",children:s.amount_cents!=null?L(s.amount_cents,s.currency):"Not known"}),e.jsx(j,{label:"Earlier purchase",children:Me(s)}),w?e.jsxs(j,{label:"What came back",children:[Pe(s.recovered_kind),s.recovered_at?` on ${Te(s.recovered_at)}`:"","."," ",Ee[s.recovery_basis].explanation]}):e.jsx(j,{label:"Progress",children:e.jsxs("span",{className:"flex flex-wrap items-center gap-2",children:[e.jsx(V,{sent:s.sentCount,total:s.totalSteps}),e.jsxs("span",{children:[De(s.sentCount,s.totalSteps),((u=s.sequence)==null?void 0:u.pause_reason)==="awaiting_subscription_recheck"?". Paused: a payment arrived but has not been matched to the subscription.":"."]})]})}),e.jsx(j,{label:"Confirmation",children:Oe(s.confirmation_mode)}),s.recovery_url?e.jsx(j,{label:"Link destination",children:h??"Direct to the payment that failed."}):null,s.offer?e.jsxs(j,{label:"Offer",children:[qe(s.offer,s.offer.code)," ",e.jsx(M,{className:"underline underline-offset-4",to:`/offers?brand=${a}`,target:"_blank",rel:"noreferrer",children:"View the offer page"}),"."]}):null]}),t?e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-[14.5px] font-medium text-foreground",children:"Emails"}),e.jsx(D,{brandId:a,frozenMessageId:t.id,audienceLine:C(s)}),o.length>0?e.jsxs(ae,{open:n,onOpenChange:i,children:[e.jsx(ne,{asChild:!0,children:e.jsxs(b,{variant:"ghost",size:"sm",surface:"plain",children:[e.jsx(re,{className:`h-4 w-4 transition-transform ${n?"rotate-180":""}`,"aria-hidden":!0}),n?"Hide follow-up emails":`Show ${o.length} follow-up email${o.length===1?"":"s"}`]})}),e.jsx(oe,{className:"space-y-3 pt-3",children:o.map(v=>e.jsx(D,{brandId:a,frozenMessageId:v.id,audienceLine:C(s)},v.id))})]}):null]}):null,w?null:e.jsxs("div",{className:"flex flex-wrap gap-2 border-t border-border/60 pt-5",children:[e.jsx(b,{variant:"outline",size:"sm",disabled:p,onClick:()=>d(s.id,s.amount_cents),children:"Mark payment received"}),g?e.jsxs(e.Fragment,{children:[e.jsx(b,{variant:"ghost",size:"sm",disabled:p,onClick:()=>x(r),children:"Pause recovery"}),e.jsx(b,{variant:"ghost",size:"sm",disabled:p,onClick:()=>l(r),children:"Stop recovery"})]}):null,c&&s.offer&&!s.offer.used_at&&!s.offer.revoked_at?e.jsx(b,{variant:"ghost",size:"sm",disabled:p,onClick:()=>c(s.offer.id),children:"Stop the offer"}):null]})]})}const ss=new Set(["needs_attention","disconnected","paused"]),ts=[{value:"all",label:"All payments"},{value:"one_time",label:"One time"},{value:"subscription",label:"Subscription"}],as="rounded-[8px] px-4 py-2 text-[14px] font-medium text-label transition-colors hover:text-foreground data-[state=active]:bg-chip data-[state=active]:font-semibold data-[state=active]:text-foreground data-[state=active]:shadow-chip";function _s(){const{activeBrand:a}=le(),s=a==null?void 0:a.id,n=f.useMemo(()=>new Date,[]),[i,x]=f.useState("30d"),l=f.useMemo(()=>F(i,n),[i,n]),d=ze(s,l),c=Be(s,l),p=d.data,h=$e(s,I(p)),[t,o]=f.useState(!1),[r,g]=f.useState(null),[w,N]=f.useState("all"),u=He(s),v=u.pauseSequence.isPending||u.stopSequence.isPending||u.confirmRecovered.isPending||u.revokeOffer.isPending,_=u.approveAllPending;return s?e.jsx(ns,{brandId:s,metrics:p,cases:c.data??[],isLoading:d.isLoading||c.isLoading,storeHealth:h.data,range:l,onRangeChange:x,now:n,uploadOpen:t,onUploadOpenChange:o,selectedId:r,onSelect:g,familyFilter:w,onFamilyFilterChange:N,onToggleRecovery:m=>u.setRecoveryEnabled.mutate(m),togglePending:u.setRecoveryEnabled.isPending,onPause:m=>u.pauseSequence.mutate(m),onStop:m=>u.stopSequence.mutate(m),onRevokeOffer:m=>u.revokeOffer.mutate(m),onConfirmPaid:(m,R)=>u.confirmRecovered.mutate({caseId:m,amountCents:R}),busy:v,onApproveAll:m=>_.mutateAsync(m),approvePending:_.isPending}):e.jsx(U,{children:e.jsx("p",{className:"text-[15.5px] text-muted-foreground",children:"Select a shop to see its recovery."})})}function ns({brandId:a,metrics:s,cases:n,isLoading:i,storeHealth:x,range:l,onRangeChange:d,now:c,uploadOpen:p,onUploadOpe
11nChange:h,selectedId:t,onSelect:o,familyFilter:r,onFamilyFilterChange:g,onToggleRecovery:w,togglePending:N,onPause:u,onStop:v,onConfirmPaid:_,onRevokeOffer:m,busy:R,onApproveAll:E,approvePending:G}){const P=f.useMemo(()=>{if(r==="all")return n;const y=r==="subscription"?"subscriber_recovery":"payment_recovery";return n.filter(S=>S.family===y)},[n,r]),T=f.useMemo(()=>[...P].sort(Ue),[P]),K=r==="all"&&(s!=null&&s.groups)?Object.values(s.groups).reduce((y,S)=>y+S,0):T.length,Q=t?n.find(y=>y.id===t)??null:null,A=(s==null?void 0:s.rules.status)==="approved";return e.jsxs(U,{children:[e.jsx(We,{brandId:a,metrics:s,range:l,onRangeChange:d,onToggleRecovery:w,togglePending:N,uploadOpen:p,onUploadOpenChange:h,now:c}),s&&!A?e.jsx(os,{metrics:s}):null,s&&A&&s.awaiting_approval_count>0&&E?e.jsx(rs,{count:s.awaiting_approval_count,onApprove:E,pending:G??!1}):null,I(s)?e.jsx(ls,{rows:x}):null,i?e.jsxs("p",{className:"flex items-center gap-2 py-6 text-[15.5px] text-muted-foreground",children:[e.jsx(H,{className:"h-3.5 w-3.5 animate-spin","aria-hidden":!0}),"Loading."]}):e.jsxs(e.Fragment,{children:[e.jsx(Ye,{metrics:s}),Fe(s)?e.jsx("div",{children:e.jsx(ie,{value:r,onValueChange:y=>g(y),children:e.jsx(de,{className:"h-auto gap-[3px] rounded-[11px] bg-fillbox-track p-[3px]",children:ts.map(({value:y,label:S})=>e.jsx(ce,{value:y,className:as,children:S},y))})})}):null,e.jsx(Ze,{cases:T,total:K,allowRecurring:Ie(s),rulesApproved:A,onSelect:o},`${r}:${l.key}`)]}),e.jsx(Xe,{brandId:a,recoveryCase:Q,onClose:()=>o(null),onPause:u,onStop:v,onConfirmPaid:_,onRevokeOffer:m,busy:R})]})}function rs({count:a,onApprove:s,pending:n}){const[i,x]=f.useState({storeRoot:!1,voluntary:!1}),[l,d]=f.useState([]),[c,p]=f.useState(!1),h=async()=>{let o;try{o=await s(i)}catch{d(["The approval did not go through. Try again."]);return}if(o.approved){d([]),p(!0);return}if(o.reason==="store_root_fallback_acknowledgment_required"){x(r=>({...r,storeRoot:!0})),d(r=>[...r.filter(g=>!g.startsWith("Some emails link")),"Some emails link to the store's front page because no exact payment page is known."]);return}if(o.reason==="voluntary_winback_acknowledgment_required"){x(r=>({...r,voluntary:!0})),d(r=>[...r.filter(g=>!g.startsWith("Some emails go")),"Some emails go to customers who cancelled on purpose."]);return}d(["Nothing is waiting for approval right now."])};if(c)return e.jsx("div",{className:"rounded-[14px] border border-sheet-border bg-sheet px-5 py-[18px] shadow-sheet",children:e.jsx("p",{className:"text-[15px] leading-[1.45] text-foreground",children:"Approved. Sending follows the schedule."})});const t=l.length>0&&(i.storeRoot||i.voluntary);return e.jsxs("div",{className:"space-y-2 rounded-[14px] border border-sheet-border bg-sheet px-5 py-[18px] shadow-sheet",children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-[18px]",children:[e.jsxs("p",{className:"flex-1 text-[15px] leading-[1.45] text-foreground",children:[a===1?"Recovery emails for 1 customer are prepared and waiting for approval.":`Recovery emails for ${a} customers are prepared and waiting for approval.`," ","Nothing sends without approval."]}),e.jsx(b,{variant:"default",size:"sm",onClick:h,disabled:n,children:n?e.jsx(H,{className:"h-3.5 w-3.5 animate-spin","aria-hidden":!0}):t?"Approve anyway":"Approve and send"})]}),l.map(o=>e.jsx("p",{className:"text-[14px] leading-[1.45] text-muted-foreground",children:o},o))]})}function os({metrics:a}){const s=a.rules.status==="draft";return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-[18px] rounded-[14px] border border-sheet-border bg-sheet px-5 py-[18px] shadow-sheet",children:[e.jsx("p",{className:"flex-1 text-[15px] leading-[1.45] text-foreground",children:s?"Recovery rules and the three emails are prepared and waiting for approval. Nothing sends until they are approved.":"Recovery rules have not been set up. Nothing sends until they are approved."}),e.jsx(b,{variant:"default",size:"sm",asChild:!0,children:e.jsx(M,{to:$.recoveryRules(),children:s?"Review the rules":"Set up the rules"})})]})}function ls({rows:a}
11){if(!a||a.length===0)return null;const s=a.filter(n=>ss.has(n.status));return s.length===0?null:e.jsx("div",{className:"space-y-2 rounded-[14px] border border-sheet-border bg-sheet px-5 py-[18px] shadow-sheet",children:s.map(n=>e.jsxs("p",{className:"text-[15px] leading-[1.45] text-foreground",children:[n.display_name||n.source_type," connection needs attention. Sending is paused because payment status cannot be confirmed."]},n.source_type))})}export{ns as PaymentRecoveryView,_s as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.