1import{u as Ne,c as ve,r as s,j as e,S as O,C as y,d as C,e as A,f as le,g as E,B as d,L as Se,h as we}from"./index-Cbruulzy.js";import{B as U}from"./badge-DDrBl-nU.js";import{I as F}from"./input-NDk1Z9Qt.js";import{L as z}from"./label-CNkV1J_C.js";import{T as ke,a as Ce,b as de,c as p,d as Ae,e as g}from"./table-HQzUaIL3.js";import{A as $}from"./AppShell-Cm3arvdk.js";import{S as q}from"./StatusChip-DvHnG-BT.js";import{T as Le}from"./TeamAccessPanel-BwViv-kc.js";import{p as W}from"./payout-onboarding-Db_i4pCc.js";import{s as De}from"./program-labels-D8x6Mtfo.js";import"./popover-DGNiKMx7.js";const Te="https://jxgrmfdufarwmiaiugqu.supabase.co/functions/v1/admin-crm";async function f(o){const{data:{session:j}}=await we.auth.getSession();return j?fetch(Te,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${j.access_token}`,apikey:"sb_publishable_pa3Q_IQij0VmW-foLMM-wA_vhXc_6av"},body:JSON.stringify(o)}):null}function $e(){const{user:o,loading:j}=Ne(),{canView:L,canMutate:b,loading:ue}=ve(),[D,H]=s.useState([]),[me,_]=s.useState(!1),[xe,he]=s.useState(!1),[u,Q]=s.useState(""),[V,J]=s.useState(!1),[X,N]=s.useState(null),[c,ye]=s.useState(null),[a,T]=s.useState(null),[pe,v]=s.useState(!1),[Y,G]=s.useState(""),[Z,ee]=s.useState(""),[m,K]=s.useState("agency"),[te,ae]=s.useState(!1),[P,S]=s.useState(null),[r,se]=s.useState(null),[ne,re]=s.useState(!1),[R,i]=s.useState(null),[B,ie]=s.useState(null),oe=s.useCallback(async()=>{if(!o||!L)return;_(!0);const t=await f({action:"list_agencies"});if(!t){_(!1);return}if(t.status===403){he(!0),_(!1);return}const n=await t.json().catch(()=>({}));H(n.agencies??[]),_(!1)},[o,L]);s.useEffect(()=>{oe()},[oe]);const I=s.useCallback(async t=>{v(!0);const n=await f({action:"get_agency",agency_id:t});if(!n){v(!1);return}if(!n.ok){T(null),v(!1);return}T(await n.json()),v(!1)},[]);s.useEffect(()=>{c?I(c):T(null)},[c,I]),s.useEffect(()=>{a&&(G(a.agency.payout_contact_name??""),ee(a.agency.payout_contact_email??""),K(a.agency.payout_recipient==="creators"?"creators":"agency"),se(a.karat_recipient??null),ie(a.payout_setup_email_sent_at?{sent_at:a.payout_setup_email_sent_at,to:a.payout_setup_email_to??""}:null),S(null),i(null))},[a]);const ge=async()=>{if(!a)return;ae(!0),S(null);const t=await f({action:"update_agency_payout_settings",agency_id:a.agency.id,payout_contact_name:Y.trim()||null,payout_contact_email:Z.trim()||null,payout_recipient:m});if(ae(!1),!t||!t.ok){S({kind:"error",text:"Couldn't save payout settings â try again."});return}S({kind:"success",text:"Saved."}),I(a.agency.id)},fe=async()=>{if(!a)return;re(!0),i(null);const t=await f({action:"ensure_agency_payout_recipient",agency_id:a.agency.id});if(re(!1),!t||!t.ok){i({kind:"error",text:"Couldn't reach Karat â try again shortly."});return}const n=await t.json().catch(()=>({})),l=n?.status??null,k=n?.onboarding_url??null;se(h=>({status:l??h?.status??null,onboarding_url:k??h?.onboarding_url??null,created_at:h?.created_at??new Date().toISOString(),business_type:h?.business_type??null,business_name:h?.business_name??null}));const M=n?.payout_setup_email_sent_at??null,ce=n?.payout_setup_email_to??null;M&&ie({sent_at:M,to:ce??""}),n?.onboarding_ready?i({kind:"success",text:"Agency payout recipient ready."}):i(l||k?{kind:"success",text:M?`Recipient created â onboarding link emailed to ${ce}.`:"Recipient created â share the onboarding link below to finish setup."}:{kind:"error",text:"Couldn't create a Karat recipient â add a payout contact name and email above, then try again."})},je=async t=>{try{await navigator.clipboard.writeText(t),i({kind:"success",text:"Onboarding link copied."})}catch{i({kind:"error",text:"Couldn't copy â select the link manually."})}},be=async t=>{if(t.preventDefault(),!!u.trim()){J(!0),N(null);try{const n=await f({action:"create_agency",name:u.trim()}),l=await n?.json().catch(()=>({}));n?.status===200?(N(`Created ${u.trim()}.`),l?.agency_id&&H(k=>[{id:l.agency_id,name:u.trim(),created_at:new Date().toISOString(),roster_count:0,staff_count:0},...k]),Q("")):N(l?.error==="invalid_name"?"Name is required.":"Couldn't create the agency â try again.")}catch{N("Couldn't create the agency â try again.")}finally{J(!1)}}};if(j||o&&ue)return e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsx(O,{})});if(!o)return e.jsx($,{children:e.jsx("main",{className:"p-6 max-w-2xl mx-auto",children:e.jsx(y,{children:e.jsx(C,{children:e.jsx(A,{children:"Sign in required"})})})})});if(xe||!L)return e.jsx($,{children:e.jsx("main",{className:"p-6 max-w-2xl mx-auto",children:e.jsx(y,{children:e.jsxs(C,{children:[e.jsx(A,{children:"No access"}),e.jsx(le,{children:"You don't have access to this page."})]})})})});const w=r?.onboarding_url??null,x=w&&/^https?:\/\//.test(w)?w:null,_e=!!w&&!x;return e.jsx($,{children:e.jsxs("main",{className:"p-6 max-w-7xl mx-auto
1space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Agencies"}),e.jsxs("p",{className:"text-muted-foreground",children:[D.length," agencies"]})]}),b&&e.jsxs(y,{children:[e.jsx(C,{children:e.jsx(A,{children:"Create agency"})}),e.jsx(E,{children:e.jsxs("form",{onSubmit:be,className:"flex flex-col md:flex-row gap-3 md:items-center",children:[e.jsx(F,{placeholder:"Agency name",value:u,maxLength:200,required:!0,onChange:t=>Q(t.target.value),className:"md:max-w-sm"}),e.jsx(d,{type:"submit",disabled:V,children:V?"Creatingâ¦":"Create agency"}),X&&e.jsx("p",{className:"text-sm text-muted-foreground",children:X})]})})]}),e.jsx(y,{children:e.jsx(E,{className:"pt-6",children:me?e.jsx("div",{className:"flex justify-center py-12",children:e.jsx(O,{})}):D.length===0?e.jsx("p",{className:"text-muted-foreground text-sm py-8 text-center",children:"No agencies yet."}):e.jsxs(ke,{children:[e.jsx(Ce,{children:e.jsxs(de,{children:[e.jsx(p,{children:"Name"}),e.jsx(p,{children:"Roster"}),e.jsx(p,{children:"Staff"}),e.jsx(p,{children:"Payouts"}),e.jsx(p,{children:"Created"})]})}),e.jsx(Ae,{children:D.map(t=>e.jsxs(de,{className:"cursor-pointer","data-state":c===t.id?"selected":void 0,onClick:()=>ye(t.id===c?null:t.id),children:[e.jsx(g,{className:"font-medium underline-offset-2 hover:underline",children:t.name}),e.jsx(g,{children:e.jsx(U,{variant:t.roster_count>0?"secondary":"outline",children:t.roster_count})}),e.jsx(g,{children:e.jsx(U,{variant:t.staff_count>0?"secondary":"outline",children:t.staff_count})}),e.jsx(g,{children:t.karat_status==null?e.jsx(q,{tone:"grey",children:"Not set up"}):W(t.karat_status)?e.jsx(q,{tone:"green",children:"Ready"}):e.jsx(q,{tone:"yellow",children:"Onboarding"})}),e.jsx(g,{className:"text-xs",children:new Date(t.created_at).toLocaleDateString()})]},t.id))})]})})}),c&&e.jsxs(y,{children:[e.jsxs(C,{children:[e.jsx(A,{children:a?.agency.name??"Agency"}),e.jsx(le,{children:"Roster and staff for this agency."})]}),e.jsx(E,{className:"space-y-6",children:pe||!a?e.jsx("div",{className:"flex justify-center py-8",children:e.jsx(O,{})}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-medium mb-2",children:"Roster"}),a.roster.length===0?e.jsx("p",{className:"text-sm text-muted-foreground",children:"No creators assigned to this agency yet."}):e.jsx("ul",{className:"divide-y",children:a.roster.map(t=>e.jsxs("li",{className:"py-2 text-sm",children:[t.full_name,e.jsxs("span",{className:"text-muted-foreground",children:[" · ",t.primary_platform,"/@",t.primary_handle]})]},t.id))})]}),b&&e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-medium mb-2",children:"Staff logins"}),e.jsx(Le,{scope:"agency",accountId:a.agency.id,description:"Logins that can manage this agency's creators."})]}),e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-medium mb-2",children:"Payouts"}),e.jsxs("div",{className:"rounded-lg border border-border bg-muted/30 p-4 space-y-4",children:[b?e.jsxs("div",{className:"space-y-3 max-w-md",children:[e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(z,{htmlFor:"agency-payout-contact-name",children:"Payout contact name"}),e.jsx(F,{id:"agency-payout-contact-name",value:Y,maxLength:200,onChange:t=>G(t.target.value),placeholder:"Who Karat should onboard"})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(z,{htmlFor:"agency-payout-contact-email",children:"Payout contact email"}),e.jsx(F,{id:"agency-payout-contact-email",type:"email",value:Z,onChange:t=>ee(t.target.value),placeholder:"[email protected]"}),a.payout_contact_conflict&&e.jsxs("p",{className:"text-warning-primary text-xs",children:["This is also"," ",e.jsx(Se,{to:"/platform/admin/creators/$creatorId",params:{creatorId:a.payout_contact_conflict.creator_id},className:"underline",children:a.payout_contact_conflict.full_name??"a creator"}),"âs email. Karat identifies payees by email, so the agency and that creator share one recipient â and one individual-or-entity tax choice, made by whichever onboarded first. If they onboarded as an individual, agency payouts are reported against their personal tax identity. Use a separate address unless that is genuinely intended."]})]}),e.jsxs("div",{className:"space-y-1.5",children:[e.jsx(z,{id:"agency-payout-recipient-label",children:"Default: pay to"}),e.jsxs("div",{role:"group","aria-labelledby":"agency-payout-recipient-label",className:"inline-flex items-center gap-0.5 rounded-md bg-muted p-0.5",children:[e.jsx(d,{type:"button",size:"sm",variant:"ghost","aria-pressed":m==="agency",onClick:()=>K("agency"),className:m==="agency"?"bg-background text-foreground shadow-sm font-medium hover:bg-background":"text-muted-foreground hover:text-foreground",children:"Agency"}),e.jsx(d,{type:"button",size:"sm",variant:"ghost","aria-pressed":m==="creators",onClick:()=>K("creators"),className:m==="creators"?"bg-background text-foreground shadow-sm font-medium hover:bg-background":"text-muted-foreground hover:text-foreground",children:"Creators directly"})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"New deals for this agency prefill from this default â each deal can still override it."})]}),e.jsx(d,{size:"sm",onClick:()=>{ge()},disabled:te,children:te?"Savingâ¦":"Save payout settings"}),P&&e.jsx("p",{className:P.kind==="error"?"text-sm text-destructive-primary":"text-sm text-muted-foreground",children:P.text})]}):e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Default: pay to ",a.agency.payout_recipient==="creators"?"creators directly":"the agency","."]}),e.jsxs("div",{className:"border-t border-border pt-4 space-y-2",children:[e.jsx("p",{className:"text-sm font-medium",children:"Karat payout recipient"}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[r?e.jsxs(e.Fragment,{children:[e.jsx(U,{variant:"secondary",children:De(r.status??"unknown")}),r.created_at&&e.jsxs("span",{className:"text-muted-foreground text-xs",children:["Recipient set up ",new Date(r.created_at).toLocaleDateString()]})]}):e.jsx("p",{className:"text-muted-foreground text-sm",children:"No Karat recipient yet."}),b&&e.jsx(d,{size:"sm",variant:"outline",onClick:()=>{fe()},disabled:ne,children:ne?"Setting upâ¦":"Set up agency payouts"})]}),r&&e.jsx("p",{className:"text-xs text-muted-foreground",children:W(r.status)?"This agency is ready to be paid â Karat onboarding is complete.":"Payouts to this agency will be withheld until Karat onboarding finishes."}),(!r||!W(r.status))&&(B?e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Onboarding email sent to ",B.to," on"," ",new Date(B.sent_at).toLocaleDateString(),"."]}):a.agency.payout_contact_email?null:e.jsx("p",{className:"text-xs text-muted-foreground",children:"Add a payout contact email above to send the onboarding link automatically."})),r?.business_type==="individual"&&e.jsxs("p",{className:"text-warning-primary text-xs",children:["Karat says this payee onboarded as a ",e.jsx("strong",{children:"private individual"}),", not a business. Money owed to the agency is therefore reported against one personâs tax identity â a W-8/W-9 in their own name. The choice is made by the payee inside Karatâs onboarding and canât be changed from here; sorting it out means re-onboarding that recipient with Karat."]}),r?.business_type==="entity"&&e.jsxs("p",{className:"text-muted-foreground text-xs",children:["Karat confirms this payee onboarded as a business entity",r.business_name?` â ${r.business_name}`:"","."]}),x?e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("a",{href:x,target:"_blank",rel:"noopener noreferrer",className:"text-primary underline break-all text-xs",children:x}),e.jsx(d,{size:"sm",variant:"ghost",onClick:()=>{je(x)},children:"Copy onboarding link"})]}):_e?e.jsx("p",{className:"text-xs text-muted-foreground",children:"Karat didn't return an onboarding link â expected in the sandbox environment."}):null,R&&e.jsx("p",{className:R.kind==="error"?"text-destructive text-xs":"text-muted-foreground text-xs",children:R.text})]})]})]})]})})]})]})})}export{$e as component};
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.