PageSourceSearch

https://illumibot.ai/assets/ManageRevenueShares-CKdUfDkR.js

js illumibot.ai collected 2026-10-05 12:04:43 UTC 101,521 bytes, 6 lines download raw bytes

1import{j as e}from"./radix-Cmq07x6K.js";import{r as i,L as Xe}from"./react-vendor-3Ku-1ERf.js";import{c as xt,aV as mt,aF as re,aA as pt,g as Y,B as K,aB as Se,T as Ie,F as we,X as ut,p as Te,f as ht,A as et,bZ as nt,D as Ve,b9 as gt,a as Ke,b as Ue,d as He,q as _t,k as it,n as De,aa as G}from"./index-DNJkTYkI.js";import{p as yt}from"./componentRules-D7b_RzQj.js";import{C as ie,a as oe}from"./card-BT7M_i6G.js";import{T as bt,A as jt,c as fe}from"./AdminSectionNav-gimHqPCj.js";import{I as te}from"./input-Be1FlM5N.js";import{T as de,a as xe,b as Q,c as g,d as me,e as x}from"./table-BJyY3HtE.js";import{f as Nt,g as ft,W as vt}from"./earningsGrouping-oefmyK-d.js";import{C as wt}from"./ChannelBadge-DliVIe7Z.js";import{R as kt}from"./refresh-cw-Dn1nApOM.js";import{S as lt,P as Ct}from"./store-BHj06vYI.js";import{L as le}from"./label-BDVtETw2.js";import{F as qe}from"./FieldLabel-CmM0Zcdz.js";import{I as ct}from"./InfoHint-DeZ2dfxL.js";import{S as Ye}from"./switch-D3aaHOC8.js";import{T as ot}from"./textarea-BWMYJidp.js";import{S as We}from"./separator-BojV64mU.js";import{d as St}from"./csv-BI-QJQap.js";import{S as $e,a as Be,b as Fe,c as Me,d as je}from"./select-A5viTzpK.js";import{P as $t}from"./package-BOMotPeo.js";import{F as Bt}from"./file-text-B1y-bSaq.js";import{P as Qe}from"./pencil-DreNek2w.js";import{T as Ge}from"./trash-2-1ZfQ2Y8K.js";import"./firebase-CPbFPgyj.js";import"./supabase-M27pd__B.js";import"./chevrons-up-down-DqJW0Z5_.js";import"./tooltip-C6349OgG.js";import"./info-B5BI5FRs.js";import"./chevron-up-CzPssOF-.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 Ft=xt("DatabaseBackup",[["ellipse",{cx:"12",cy:"5",rx:"9",ry:"3",key:"msslwz"}],["path",{d:"M3 12a9 3 0 0 0 5 2.69",key:"1ui2ym"}],["path",{d:"M21 9.3V5",key:"6k6cib"}],["path",{d:"M3 5v14a9 3 0 0 0 6.47 2.88",key:"i62tjy"}],["path",{d:"M12 12v4h4",key:"1bxaet"}],["path",{d:"M13 20a5 5 0 0 0 9-3 4.5 4.5 0 0 0-4.5-4.5c-1.33 0-2.54.54-3.41 1.41L12 16",key:"1f4ei9"}]]);function be(a,m,f,v){var F;const B=a.get(`${f}::`)??[];if(v===null)return B.filter(N=>N.quantity_per_bundle>0).map(N=>({component_product_id:N.component_product_id,component_variation_id:N.component_variation_id,quantity_per_bundle:N.quantity_per_bundle,origin:"product"}));const b=`${f}::${v}`,P=a.get(b)??[];if(!(((F=m.get(b))==null?void 0:F.inherit_from_product)??!0))return P.filter(N=>N.quantity_per_bundle>0).map(N=>({component_product_id:N.component_product_id,component_variation_id:N.component_variation_id,quantity_per_bundle:N.quantity_per_bundle,origin:"variation_addition"}));const L=N=>`${N.component_product_id}::${N.component_variation_id??""}`,u=new Map;for(const N of P)u.set(L(N),N);const I=[];for(const N of B){const H=u.get(L(N));H?(H.quantity_per_bundle>0&&I.push({component_product_id:H.component_product_id,component_variation_id:H.component_variation_id,quantity_per_bundle:H.quantity_per_bundle,origin:"variation_override"}),u.delete(L(N))):N.quantity_per_bundle>0&&I.push({component_product_id:N.component_product_id,component_variation_id:N.component_variation_id,quantity_per_bundle:N.quantity_per_bundle,origin:"product"})}for(const N of u.values())N.quantity_per_bundle>0&&I.push({component_product_id:N.component_product_id,component_variation_id:N.component_variation_id,quantity_per_bundle:N.quantity_per_bundle,origin:"variation_addition"});return I}const Mt=()=>{const{sandbox:a}=mt(),[m,f]=i.useState([]),[v,B]=i.useState([]),[b,P]=i.useState(!0),[A,L]=i.useState(!0),u=i.useCallback(async(_=!1)=>{const r=await re("list_payees",{include_inactive:_});f(r.payees??[])},[]),I=i.useCallback(async()=>{const _=await pt("list"),r=((_==null?void 0:_.products)??[]).map(n=>{var o;const h=Array.isArray(n.variations)?n.variations.map(q=>({variation_id:String(q.variation_id??q.id??""),name:String(q.name??""),price:typeof q.price=="number"?q.price:null})):[],C=Array.isArray(n.images)?n.images:[];return{product_id:String(n.product_id??n.id??""),name:String(n.name??""),thumbnail:C.length>0?((o=C[0])==null?void 0:o.src)??null:null,price:typeof n.price=="number"?n.price:null,variations:h}});B(r.sort((n,h)=>n.name.localeCompare(h.name)))},[]);i.useEffect(()=>{let _=!1;return P(!0),u(!0).catch(r=>{_||Y.error(`Failed to load payees: ${r.message}`)}).finally(()=>{_||P(!1)}),()=>{_=!0}},[u,a]),i.useEffect(()=>{let _=!1;return L(!0),I().catch(r=>{_||Y.error(`Failed to load products: ${r.message}`)}).finally(()=>{_||L(!1)}),()=>{_=!0}},[I,a]);const F=i.useCallback(async _=>{const r=await re("create_payee",_);return await u(!0),r.payee},[u]),N=i.useCallback(async(_,r)=>{const n=await re("update_payee",{id:_,...r});return await u(!0),n.payee},[u]),H=i.useCallback(async(_,r,n=!1,h=null)=>(await re("list_shares",{product_id:_,variation_id:r??null,in_bundle_product_id:h??null,include_ended:n})).shares??[],[]),V=i.useCallback(async(_,r,n,h,C=null)=>re("replace_shares",{product_id:_,variation_id:r??null,in_bundle_product_id:C??null,lines:n,change_reason:h??null}),[]),O=i.useCallback(async _=>re("end_share",{id:_}),[]),E=i.useCallback(async(_,r)=>(await re("list_components",{bundle_product_id:_,bundle_variation_id:r??null})).components??[],[]),R=i.useCallback(async(_,r,n,h)=>re("set_components",{bundle_product_id:_,bundle_variation_id:r??null,components:n,...h===void 0?{}:{inherit_from_product:h}}),[]),J=i.useCallback(async()=>(await re("list_bundles")).bundles??[],[]),d=i.useCallback(async()=>(await re("list_all_active_shares")).shares??[],[]),k=i.useCallback(async()=>{const _=await re("list_all_components");return{components:_.components??[],configs:_.configs??[]}},[]),j=i.useCallback(async(_,r,n="payee")=>re("reconcile",{from:_,to:r,group_by:n}),[]),y=i.useCallback(async(_,r,n={})=>re("reconcile",{from:_,to:r,group_by:"line_item",payee_id:n.payee_id??null,product_id:n.product_id??null}),[]),w=i.useCallback(async(_,r,n={})=>
6re("list_earnings",{from:_,to:r,payee_id:n.payee_id??null,needs_review_only:n.needs_review_only??!1}),[]),D=i.useCallback(async(_,r,n)=>re("backfill_earnings",{from:_,to:r,dry_run:n}),[]),z=i.useCallback(async(_,r)=>re("create_channel_order",{..._,dry_run:r}),[]),U=i.useCallback(async()=>(await re("list_sales_channels",{})).channels,[]);return{sandbox:a,payees:m,products:v,loading:b,productsLoading:A,refreshPayees:u,refreshProducts:I,createPayee:F,updatePayee:N,listShares:H,replaceShares:V,endShare:O,listComponents:E,setComponents:R,listBundles:J,listAllActiveShares:d,listAllComponents:k,reconcile:j,reconcileLines:y,listEarnings:w,backfillEarnings:D,createChannelOrder:z,listSalesChannels:U}},_e=a=>`${a<0?"-":""}$${(Math.abs(a)/100).toFixed(2)}`,tt=a=>a.toISOString().slice(0,10),Ee=a=>new Date(a).toLocaleDateString(void 0,{month:"short",day:"numeric"});function dt(a){return a.status==="cancelled"?"cancelled":Date.parse(a.matures_at)<=Date.now()?"available":"pending"}function Pt({row:a}){var v,B;const m=(v=a.orders)==null?void 0:v.sales_channels;if(!m)return null;const f=(B=a.orders)==null?void 0:B.channel_fee_cents;return e.jsx(wt,{name:m.display_name,title:f?`${m.display_name} took ${_e(f)}
6 of this sale`:m.display_name})}function At(a){return i.useMemo(()=>ft(a.map(m=>({...m,orderKey:m.order_id,payeeKey:m.payee_id,amountCents:m.amount_cents,basisPoints:m.basis_points,state:dt(m),orderedAt:m.created_at}))),[a])}const Lt=({rs:a})=>{const m=i.useMemo(()=>new Date,[]),f=i.useMemo(()=>{const o=new Date(m);return o.setDate(o.getDate()-90),tt(o)},[m]),[v,B]=i.useState(f),[b,P]=i.useState(()=>{const o=new Date(m);return o.setDate(o.getDate()+1),tt(o)}),[A,L]=i.useState(!1),[u,I]=i.useState([]),[F,N]=i.useState([]),[H,V]=i.useState(!1),[O,E]=i.useState(!1),[R,J]=i.useState(null),[d,k]=i.useState(!1),[j,y]=i.useState(null),w=i.useCallback(async()=>{V(!0);try{const o=await a.listEarnings(new Date(v).toISOString(),new Date(b).toISOString(),{needs_review_only:A});I(o.rows??[]),N(o.by_payee??[]),E(!0)}catch(o){Y.error(o.message??"Could not load earnings")}finally{V(!1)}},[a,v,b,A]),D=`${v}|${b}`,z=(R==null?void 0:R.window)===D,U=i.useCallback(async o=>{k(!0);try{const q=await a.backfillEarnings(new Date(v).toISOStr
6ing(),new Date(b).toISOString(),o);o?(J({window:`${v}|${b}`,result:q}),y(null)):(y(q),J(null),await w(),Y.success(`Booked ${q.rows_inserted} ${q.rows_inserted===1?"row":"rows"} across ${q.orders_accrued} ${q.orders_accrued===1?"order":"orders"}.`))}catch(q){Y.error(q.message??"Backfill failed")}finally{k(!1)}},[a,v,b,w]);i.useEffect(()=>{w()},[]);const _=At(u),[r,n]=i.useState(new Set),h=o=>n(q=>{const Z=new Set(q);return Z.has(o)?Z.delete(o):Z.add(o),Z}),C=u.filter(o=>o.needs_review);return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"text-sm font-medium text-white",children:"Earnings ledger"}),e.jsx("p",{className:"mt-1 max-w-3xl text-xs leading-relaxed text-gray-400",children:"What has actually been booked to each payee since the ledger went live, net of the shopper's discount and the affiliate's commission. These figures will not match the Reconcile tab on any discounted order — Reconcile reports the older basis, which pays on the pre-discount price. That difference is the correction."})]}),e.jsxs("div",{className:"flex flex-wrap items-end gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-[10px] text-gray-500",children:"From"}),e.jsx(te,{type:"date",value:v,onChange:o=>B(o.target.value),className:"h-8 w-40 border-white/10 bg-white/5 text-xs"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-[10px] text-gray-500",children:"To"}),e.jsx(te,{type:"date",value:b,onChange:o=>P(o.target.value),className:"h-8 w-40 border-white/10 bg-white/5 text-xs"})]}),e.jsxs("label",{className:"flex h-8 cursor-pointer items-center gap-1.5 text-xs text-gray-300",children:[e.jsx("input",{type:"checkbox",checked:A,onChange:o=>L(o.target.checked),className:"accent-illumigreen"}),"Needs review only"]}),e.jsxs(K,{size:"sm",onClick:()=>void w(),disabled:H,className:"h-8",children:[H?e.jsx(Se,{className:"mr-1.5 h-3.5 w-3.5 animate-spin"}):e.jsx(kt,{className:"mr-1.5 h-3.5 w-3.5"}),"Refresh"]})]}),C.length>0&&e.jsxs("div",{className:"flex items-start gap-2 rounded-lg border border-amber-500/30 bg-amber-500/5 p-3",children:[e.jsx(Ie,{className:"mt-0.5 h-4 w-4 shrink-0 text-amber-400"}),e.jsxs("div",{className:"text-xs text-amber-200",children:[e.jsx("strong",{children:C.length})," ",C.length===1?"earning":"earnings"," accrued without a commission deduction, because the referral code's rate could not be read at the time of sale. The payee may be over-credited on ",C.length===1?"it":"them","."]})]}),e.jsxs("div",{className:"rounded-lg border border-white/10 bg-white/5 p-3",children:[e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-1.5 text-xs font-medium text-white",children:[e.jsx(Ft,{className:"h-3.5 w-3.5"}),"Backfill the ledger"]}),e.jsx("p",{className:"mt-1 max-w-2xl text-[11px] leading-relaxed text-gray-400",children:"Replays every paid order in this window through the accrual, so an order that booked no earning catches up. Safe to repeat — a row that is already there is absorbed rather than paid twice."})]}),e.jsxs("div",{className:"flex shrink-0 items-center gap-2",children:[e.jsxs(K,{size:"sm",variant:"outline",onClick:()=>void U(!0),disabled:d||H,className:"h-8 border-white/15 bg-transparent text-xs",children:[d&&!z&&e.jsx(Se,{className:"mr-1.5 h-3.5 w-3.5 animate-spin"}),"Preview"]}),z&&e.jsxs(K,{size:"sm",onClick:()=>void U(!1),disabled:d,className:"h-8 text-xs",children:[d&&e.jsx(Se,{className:"mr-1.5 h-3.5 w-3.5 animate-spin"}),"Run for real"]})]})]}),z&&e.jsxs("div",{className:"mt-3 rounded border border-white/10 bg-black/20 p-2.5 text-[11px] leading-relaxed text-gray-300",children:[e.jsxs("div",{children:["Scanned ",e.jsx("strong",{className:"text-white",children:R.result.orders_scanned})," paid orders; ",e.jsx("strong",{className:"text-white",children:R.result.orders_accrued})," would accrue, offering"," ",e.jsx("strong",{className:"text-white",children:R.result.rows_inserted})," rows."]}),e.jsx("p",{className:"mt-1 text-gray-500",children:"A preview counts rows the ledger would be offered, including ones already recorded. The real run reports only what it actually wrote."})]}),j&&e.jsxs("div",{className:"mt-3 rounded border border-illumigreen/30 bg-illumigreen/5 p-2.5 text-[11px] leading-relaxed text-gray-300",children:["Wrote ",e.jsx("strong",{className:"text-white",children:j.rows_inserted})," ",j.rows_inserted===1?"row":"rows"," across"," ",e.jsx("strong",{className:"text-white",children:j.orders_accrued})," ",j.orders_accrued===1?"order":"orders",j.refunds_cascaded>0&&e.jsxs(e.Fragment,{children:[" · ",j.refunds_cascaded," refund reversals cascaded"]}),"."]}),R!=null&&R.result.failures.length||j!=null&&j.failures.length?e.jsxs("div",{className:"mt-2 rounded border border-amber-500/30 bg-amber-500/5 p-2.5 text-[11px] text-amber-200",children:[e.jsxs("div",{className:"font-medium",children:[(j??R.result).failures.length," order(s) could not be resolved:"]}),e.jsx("ul",{className:"mt-1 space-y-0.5",children:(j??R.result).failures.slice(0,8).map(o=>e.jsxs("li",{className:"font-mono text-[10px]",children:[o.order_id.slice(0,8)," — ",o.error]},o.order_id))})]}):null]}),e.jsxs("div",{children:[e.jsx("h4",{className:"mb-2 text-xs font-medium text-gray-300",children:"By payee"}),F.length===0?e.jsx("p",{className:"rounded-lg border border-white/10 bg-white/5 p-4 text-xs text-gray-500",children:O?"Nothing accrued in this range. Earnings are recorded when an order is paid — anything settled before the ledger went live is in Reconcile, not here.":"Loading…"}):e.jsx("div",{className:"overflow-x-auto rounded-lg border border-white/10",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"hover:bg-transparent",children:[e.jsx(g,{className:"text-xs",children:"Payee"}),e.jsx(g,{className:"text-right text-xs",children:"Earned"}),e.jsx(g,{className:"text-right text-xs",children:"Available"}),e.jsx(g,{className:"text-right text-xs",children:"Pending"}
6),e.jsx(g,{className:"text-right text-xs",children:"Reversed"}),e.jsx(g,{className:"text-right text-xs",children:"Rows"})]})}),e.jsx(me,{children:F.map(o=>e.jsxs(Q,{children:[e.jsxs(x,{className:"py-2 text-xs text-white",children:[o.payee_name,o.needs_review_count>0&&e.jsxs("span",{className:"ml-1.5 rounded bg-amber-500/15 px-1.5 py-0.5 text-[10px] text-amber-400",children:[o.needs_review_count," to review"]})]}),e.jsx(x,{className:"py-2 text-right text-xs tabular-nums text-white",children:_e(o.earned_cents)}),e.jsx(x,{className:"py-2 text-right text-xs tabular-nums text-illumigreen",children:_e(o.available_cents)}),e.jsx(x,{className:"py-2 text-right text-xs tabular-nums text-gray-400",children:_e(o.pending_cents)}),e.jsx(x,{className:"py-2 text-right text-xs tabular-nums text-amber-400",children:o.reversed_cents?_e(o.reversed_cents):"—"}),e.jsx(x,{className:"py-2 text-right text-xs tabular-nums text-gray-500",children:o.row_count})]},o.payee_id))})]})})]}),u.length>0&&e.jsxs("div",{children:[e.jsxs("h4",{className:"mb-2 text-xs font-medium text-gray-300",children:["Sales"," ",e.jsxs("span",{className:"text-gray-600",children:["(",_.length,_.length===u.length?"":` · ${u.length} lines`,")"]})]}),e.jsx("div",{className:"max-h-[28rem] overflow-auto rounded-lg border border-white/10 [&>div]:overflow-visible",children:e.jsxs(de,{children:[e.jsx(xe,{className:"sticky top-0 z-10 bg-[#14181B]",children:e.jsxs(Q,{className:"hover:bg-transparent",children:[e.jsx(g,{className:"text-xs",children:"Date"}),e.jsx(g,{className:"text-xs",children:"Payee"}),e.jsx(g,{className:"text-xs",children:"Sale"}),e.jsx(g,{className:"text-right text-xs",children:"Rate"}),e.jsx(g,{className:"text-right text-xs",children:"Basis"}),e.jsx(g,{className:"text-right text-xs",children:"Amount"}),e.jsx(g,{className:"text-xs",children:"State"})]})}),e.jsx(me,{children:_.map(o=>{var t;const q=o.children[0],Z=o.children.length,s=r.has(o.key),c=o.children.some(l=>l.needs_review),p=o.children.some(l=>l.adjusts_earning_id);return e.jsxs(i.Fragment,{children:[e.jsxs(Q,{children:[e.jsx(x,{className:"py-1.5 text-xs text-gray-400",children:e.jsxs("span",{className:"inline-flex items-center gap-1",children:[Z>1?e.jsx("button",{type:"button",onClick:()=>h(o.key),"aria-expanded":s,"aria-label":`${s?"Hide":"Show"} the ${Z} lines of this sale`,className:"-m-1.5 rounded p-2 text-gray-500 transition hover:text-white",children:e.jsx(we,{className:`h-3 w-3 transition-transform ${s?"rotate-90":""}`})}):e.jsx("span",{className:"inline-block w-4"}),Ee(o.orderedAt)]})}),e.jsx(x,{className:"py-1.5 text-xs text-gray-300",children:((t=q.payees)==null?void 0:t.display_name)??q.payee_id.slice(0,8)}),e.jsxs(x,{className:"py-1.5 text-xs text-gray-300",children:[Z>1?`${Z} components`:q.source_product_id,e.jsx(Pt,{row:q}),p&&e.jsx("span",{className:"ml-1.5 rounded bg-amber-500/15 px-1.5 py-0.5 text-[10px] text-amber-400",children:"refund"}),c&&e.jsx("span",{className:"ml-1.5 rounded bg-amber-500/15 px-1.5 py-0.5 text-[10px] text-amber-400",children:"review"})]}),e.jsx(x,{className:"py-1.5 text-right text-xs tabular-nums text-gray-400",children:Nt(o.basisPoints)}),e.jsx(x,{className:"py-1.5 text-right text-xs tabular-nums text-gray-600",children:Z>1?"—":_e(q.line_net_cents)}),e.jsx(x,{className:`py-1.5 text-right text-xs font-medium tabular-nums ${o.totalCents<0?"text-amber-400":o.state==="cancelled"?"text-gray-600 line-through":"text-white"}`,children:_e(o.totalCents)}),e.jsx(x,{className:"py-1.5 text-[11px] text-gray-500",children:o.state==="mixed"?"part available":o.state==="pending"?`held to ${Ee(q.matures_at)}`:o.state})]}),Z>1&&s&&o.children.map(l=>{const M=dt(l);return e.jsxs(Q,{className:"bg-white/[0.02]",children:[e.jsx(x,{className:"py-1.5 pl-8 text-xs text-gray-600"}),e.jsx(x,{className:"py-1.5 text-xs text-gray-600"}),e.jsxs(x,{className:"py-1.5 text-xs text-gray-400",children:[l.source_product_id,l.adjusts_earning_id&&e.jsx("span",{className:"ml-1.5 rounded bg-amber-500/15 px-1.5 py-0.5 text-[10px] text-amber-400",children:"refund"}),l.needs_review&&e.jsx("span",{className:"ml-1.5 rounded bg-amber-500/15 px-1.5 py-0.5 text-[10px] text-amber-400",children:"review"})]}),e.jsxs(x,{className:"py-1.5 text-right text-xs tabular-nums text-gray-400",children:[(l.basis_points/100).toFixed(2),"%"]}),e.jsx(x,{className:"py-1.5 text-right text-xs tabular-nums text-gray-500",children:_e(l.line_net_cents)}),e.jsx(x,{className:`py-1.5 text-right text-xs font-medium tabular-nums ${l.amount_cents<0?"text-amber-400":M==="cancelled"?"text-gray-600 line-through":"text-white"}`,children:_e(l.amount_cents)}),e.jsx(x,{className:"py-1.5 text-[11px] text-gray-500",children:M==="pending"?`held to ${Ee(l.matures_at)}`:M})]},l.id)})]},o.key)})})]})})]})]})};function Dt(a){if(!a.channel)return{error:"Choose the store the sale was made on."};const m=a.orderNumber.trim().replace(/^#/,"");if(!m)return{error:"Enter the order number."};if(!/^\d{4}-\d{2}-\d{2}$/.test(a.date))return{error:"Enter the order date."};
6const f=a.lines.filter(B=>B.sku.trim()!=="");if(f.length===0)return{error:"Add at least one SKU."};const v=[];for(const B of f){const b=Number(B.quantity);if(!Number.isInteger(b)||b<1)return{error:`Quantity for ${B.sku.trim()} must be a whole number of at least 1.`};let P=null;if(B.price.trim()!==""){const A=Number(B.price.trim().replace(/^\$/,""));if(!Number.isFinite(A)||A<0)return{error:`Price for ${B.sku.trim()} is not a number.`};P=Math.round(A*100)}v.push({sku:B.sku.trim(),quantity:b,unit_price_cents:P})}return{input:{channel:a.channel,external_order_id:m,external_order_number:`#${m}`,paid_at:`${a.date}T12:00:00Z`,customer_name:a.customer.trim()||null,lines:v}}}const ve=a=>`$${(a/100).toFixed(2)}`,Oe=()=>({sku:"",quantity:"1",price:""}),Tt=({rs:a})=>{const[m,f]=i.useState(null),[v,B]=i.useState(""),[b,P]=i.useState(""),[A,L]=i.useState(""),[u,I]=i.useState(""),[F,N]=i.useState([Oe()]),[H,V]=i.useState(!1),[O,E]=i.useState(null),[R,J]=i.useState([]);i.useEffect(()=>{let r=!0;return a.listSalesChannels().then(n=>{r&&(f(n),n.length===1&&B(n[0].slug))}).catch(n=>{r&&(f([]),Y.error(n.message??"Could not load the partner stores"))}),()=>{r=!1}},[a]);const d=(m==null?void 0:m.find(r=>r.slug===v))??null,k=(d==null?void 0:d.display_name)??"the partner",j=i.useMemo(()=>Dt({channel:v,orderNumber:b,date:A,customer:u,lines:F}),[v,b,A,u,F]),y="input"in j?JSON.stringify(j.input):null,w=O!==null&&O.key===y,D=w?O.result:null,z=D!==null&&(D.already_present||D.unmapped_skus.length>0),U=(r,n)=>N(h=>h.map((C,o)=>o===r?{...C,...n}:C)),_=i.useCallback(async r=>{if(!("input"in j)){Y.error(j.error);return}V(!0);try{const n=await a.createChannelOrder(j.input,r);if(r){E({key:JSON.stringify(j.input),result:n});return}E(null),J(h=>[{number:j.input.external_order_number??j.input.external_order_id,result:n},...h]),n.already_present?Y.message(`${j.input.external_order_number} was already recorded. Nothing booked.`):(Y.success(`Recorded ${j.input.external_order_number}: ${n.earnings_inserted} ${n.earnings_inserted===1?"earning":"earnings"} booked.`),P(""),I(""),N([Oe()]))}catch(n){Y.error(n.message??"Could not record the order")}finally{V(!1)}},[a,j]);return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsxs("h3",{className:"flex items-center gap-1.5 text-sm font-medium text-white",children:[e.jsx(lt,{className:"h-4 w-4"}),"Record ",d?`a ${d.display_name}`:"a partner store"," order"]}),e.jsxs("p",{className:"mt-1 max-w-3xl text-xs leading-relaxed text-gray-400",children:["For sales made on ",k,"'s store, entered from their packing slip. The order is recorded so revenue shares accrue on it, less ",k,"'s cut. It is never added to the packing queue, because ",k," ships it. Leave the price blank to use our current list price."]})]}),m!==null&&m.length===0&&e.jsx("p",{className:"text-xs text-amber-300",children:"No partner stores are set up, so there is nowhere to record a sale against."}),m!==null&&m.length>1&&e.jsxs("div",{children:[e.jsx("label",{htmlFor:"channel-order-store",className:"mb-1 block text-[10px] text-gray-500",children:"Store"}),e.jsxs("select",{id:"channel-order-store",value:v,onChange:r=>B(r.target.value),className:"h-8 rounded-md border border-white/10 bg-white/5 px-2 text-xs text-white",children:[e.jsx("option",{value:"",children:"Choose the store…"}),m.map(r=>e.jsxs("option",{value:r.slug,children:[r.display_name," (",r.fee_bps/100,"% cut)"]},r.slug))]})]}),e.jsxs("div",{className:"rounded-lg border border-white/10 bg-white/5 p-3 space-y-3",children:[e.jsxs("div",{className:"flex flex-wrap items-end gap-2",children:[e.jsxs("div",{children:[e.jsx("label",{htmlFor:"channel-order-number",className:"mb-1 block text-[10px] text-gray-500",children:"Order #"}),e.jsx(te,{id:"channel-order-number",value:b,onChange:r=>P(r.target.value),placeholder:"770554",className:"h-8 w-32 border-white/10 bg-white/5 text-xs"})]}),e.jsxs("div",{children:[e.jsx("label",{htmlFor:"channel-order-date",className:"mb-1 block text-[10px] text-gray-500",children:"Order date"}),e.jsx(te,{id:"channel-order-date",type:"date",value:A,onChange:r=>L(r.target.value),className:"h-8 w-40 border-white/10 bg-white/5 text-xs"})]}),e.jsxs("div",{className:"min-w-[12rem] flex-1",children:[e.jsx("label",{htmlFor:"channel-order-customer",className:"mb-1 block text-[10px] text-gray-500",children:"Customer (bill to)"}),e.jsx(te,{id:"channel-order-customer",value:u,onChange:r=>I(r.target.value),className:"h-8 border-white/10 bg-white/5 text-xs"})]})]}),e.jsxs("div",{className:"space-y-1.5",children:[F.map((r,n)=>e.jsxs("div",{className:"grid grid-cols-[1fr_4rem_6rem_2.25rem] items-end gap-2 sm:flex sm:flex-wrap",children:[e.jsxs("div",{className:"min-w-0",children:[n===0?e.jsx("label",{className:"mb-1 block text-[10px] text-gray-500",children:"SKU"}
6):e.jsx("span",{"aria-hidden":!0,className:"mb-1 block text-[10px] text-gray-500 sm:hidden",children:"SKU"}),e.jsx(te,{"aria-label":`SKU, line ${n+1}`,value:r.sku,onChange:h=>U(n,{sku:h.target.value}),className:"h-9 w-full border-white/10 bg-white/5 font-mono text-xs sm:h-8 sm:w-40"})]}),e.jsxs("div",{className:"min-w-0",children:[n===0?e.jsx("label",{className:"mb-1 block text-[10px] text-gray-500",children:"Qty"}):e.jsx("span",{"aria-hidden":!0,className:"mb-1 block text-[10px] text-gray-500 sm:hidden",children:"Qty"}),e.jsx(te,{"aria-label":`Quantity, line ${n+1}`,inputMode:"numeric",value:r.quantity,onChange:h=>U(n,{quantity:h.target.value}),className:"h-9 w-full border-white/10 bg-white/5 text-xs sm:h-8 sm:w-16"})]}),e.jsxs("div",{className:"min-w-0",children:[n===0?e.jsx("label",{className:"mb-1 block text-[10px] text-gray-500",children:"Unit price ($)"}):e.jsx("span",{"aria-hidden":!0,className:"mb-1 block text-[10px] text-gray-500 sm:hidden",children:"Unit price ($)"}),e.jsx(te,{"aria-label":`Unit price, line ${n+1}`,inputMode:"decimal",value:r.price,onChange:h=>U(n,{price:h.target.value}),placeholder:"list price",className:"h-9 w-full border-white/10 bg-white/5 text-xs sm:h-8 sm:w-28"})]}),F.length>1&&e.jsx(K,{size:"icon",variant:"ghost","aria-label":`Remove line ${n+1}`,onClick:()=>N(h=>h.filter((C,o)=>o!==n)),className:"h-9 w-9 text-gray-400 hover:text-white sm:h-8 sm:w-8",children:e.jsx(ut,{className:"h-3.5 w-3.5"})})]},n)),e.jsxs(K,{size:"sm",variant:"ghost",onClick:()=>N(r=>[...r,Oe()]),className:"h-7 px-2 text-xs text-gray-300 hover:text-white",children:[e.jsx(Te,{className:"mr-1 h-3.5 w-3.5"}),"Add line"]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(K,{size:"sm",variant:"outline",onClick:()=>void _(!0),disabled:H,className:"h-8 border-white/15 bg-transparent text-xs",children:[H&&!w&&e.jsx(Se,{className:"mr-1.5 h-3.5 w-3.5 animate-spin"}),"Preview"]}),w&&!z&&e.jsxs(K,{size:"sm",onClick:()=>void _(!1),disabled:H,className:"h-8 text-xs",children:[H&&e.jsx(Se,{className:"mr-1.5 h-3.5 w-3.5 animate-spin"}),"Record order"]})]}),D&&e.jsxs("div",{className:"space-y-2 rounded border border-white/10 bg-black/20 p-2.5 text-[11px] text-gray-300",children:[D.already_present&&e.jsxs("div",{className:"flex items-start gap-1.5 text-amber-200",children:[e.jsx(Ie,{className:"mt-0.5 h-3.5 w-3.5 shrink-0"}),"This order is already recorded. Recording it again would book nothing."]}),D.unmapped_skus.length>0&&e.jsxs("div",{className:"flex items-start gap-1.5 text-amber-200",children:[e.jsx(Ie,{className:"mt-0.5 h-3.5 w-3.5 shrink-0"}),e.jsxs("span",{children:["Not mapped to any of our products:"," ",e.jsx("span",{className:"font-mono",children:D.unmapped_skus.join(", ")}),". Recorded as is, ",D.unmapped_skus.length===1?"that line":"those lines"," would pay nobody. Check the SKU, or have it mapped first."]})]}),!D.already_present&&e.jsxs(e.Fragment,{children:[e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10",children:[e.jsx(g,{className:"h-7 text-[10px]",children:"SKU"}),e.jsx(g,{className:"h-7 text-[10px]",children:"Product"}),e.jsx(g,{className:"h-7 text-right text-[10px]",children:"Qty"}),e.jsx(g,{className:"h-7 text-right text-[10px]",children:"Unit"}),e.jsx(g,{className:"h-7 text-right text-[10px]",children:"Total"})]})}),e.jsx(me,{children:D.lines.map((r,n)=>e.jsxs(Q,{className:"border-white/5",children:[e.jsx(x,{className:"py-1 font-mono text-[10px]",children:r.sku}),e.jsx(x,{className:`py-1 ${r.unmapped?"text-amber-300":""}`,children:r.unmapped?"Unmapped":r.product_name}),e.jsx(x,{className:"py-1 text-right",children:r.quantity}),e.jsxs(x,{className:"py-1 text-right",children:[ve(r.unit_price_cents),r.price_from_catalogue&&e.jsx("span",{className:"ml-1 text-[10px] text-gray-500",children:"list"})]}),e.jsx(x,{className:"py-1 text-right",children:ve(r.total_price_cents)})]},n))})]}),e.jsxs("div",{className:"text-right",children:["Sale ",e.jsx("strong",{className:"text-white",children:ve(D.gross_cents)})," −"," ",k," ",D.channel_fee_bps/100,"%"," ",e.jsx("strong",{className:"text-white",children:ve(D.channel_fee_cents)})," = shared"," ",e.jsx("strong",{className:"text-white",children:ve(D.basis_cents)})]})]})]})]}),R.length>0&&e.jsxs("div",{className:"space-y-1",children:[e.jsx("h4",{className:"text-xs font-medium text-white",children:"Recorded this session"}),e.jsx("ul",{className:"space-y-0.5 text-[11px] text-gray-300",children:R.map(({number:r,result:n},h)=>e.jsxs("li",{children:[e.jsx("span",{className:"font-mono text-white",children:r})," ·"," ",n.lines.map(C=>`${C.quantity}× ${C.product_name}`).join(", ")," ·"," ",n.already_present?"already recorded, nothing booked":`${ve(n.basis_cents)} shared · ${n.earnings_inserted} ${n.earnings_inserted===1?"earning":"earnings"} booked`,n.earnings_needs_review>0&&e.jsxs("span",{className:"text-amber-300",children:[" · ",n.earnings_needs_review," need review"]})]},`${r}-${h}`))})]})]})},ae="bg-black border-white/20 text-white placeholder:text-gray-500 focus-visible:ring-illumigreen/40",ne="text-gray-300 text-xs uppercase tracking-wider",Ce={payees:"Anyone who receives a cut of revenue — artists, collaborators, or the house.",stripeConnectId:"Stored for your records only. Nothing on this page transfers funds — payouts are reconciled and paid out manually.",housePayee:"The catch-all payee for revenue not assigned to anyone else. Only one payee can be the house.",notes:"Free-form internal notes about this payee. Admin-only.",splits:"Every product at a glance. Click a row to edit its split; variation overrides open in a dialog."},Je="data-[state=checked]:bg-illumigreen data-[state=unchecked]:bg-gray-700",ke="bg-illumigreen hover:bg-illumigreen/80 text-black font-medium",pe="text-gray-300 hover:text-white hover:bg-white/5";function ce(a){return(a/100).toFixed(2)}function st(a){const m=parseFloat(a);return Number.isFinite(m)?Math.round(m*100):0}
6function se(a){return`$${(a/100).toFixed(2)}`}const Rt=[{value:"payees",label:"Payees",icon:nt},{value:"splits",label:"Splits",icon:Ct},{value:"bundles",label:"Bundles",icon:$t},{value:"reconcile",label:"Reconcile",icon:Bt},{value:"earnings",label:"Earnings",icon:vt},{value:"channel-orders",label:"Channel orders",icon:lt}],js=()=>{const[a,m]=i.useState("payees"),{isSignedIn:f,isAdmin:v}=ht(),B=f()&&v(),b=Mt();return B?e.jsx("div",{className:"min-h-screen bg-black text-white",children:e.jsxs("div",{className:"container mx-auto px-3 sm:px-4 pt-1 sm:pt-6 pb-8 max-w-7xl",children:[e.jsxs("div",{className:"flex items-center gap-2 sm:gap-4 mb-6",children:[e.jsx(Xe,{to:"/admin",children:e.jsx(K,{variant:"ghost",size:"icon",className:"text-white hover:text-illumigreen",children:e.jsx(et,{className:"h-5 w-5"})})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("h1",{className:"text-xl sm:text-3xl font-bold",children:"Revenue Shares"}),e.jsx("p",{className:"text-sm sm:text-base text-gray-400",children:"Manage payees, configure per-product / per-variation splits, and reconcile monthly payouts."})]})]}),e.jsxs("div",{className:"mb-6 rounded-md border border-illumigreen/30 bg-illumigreen/5 px-4 py-3 text-sm text-gray-200",children:[e.jsx("span",{className:"font-semibold text-illumigreen",children:"Manual payouts only."})," ","Nothing here moves money. Splits are recorded and totals are computed for end-of-month reconciliation — you pay each payee outside of this tool."]}),e.jsxs(bt,{value:a,onValueChange:m,className:"w-full",children:[e.jsx(jt,{sections:Rt,value:a,onValueChange:m,label:"Revenue share sections"}),e.jsx(fe,{value:"payees",className:"space-y-6",children:e.jsx(qt,{rs:b})}),e.jsx(fe,{value:"splits",className:"space-y-6",children:e.jsx(Ot,{rs:b})}),e.jsx(fe,{value:"bundles",className:"space-y-6",children:e.jsx(It,{rs:b})}),e.jsx(fe,{value:"reconcile",className:"space-y-6",children:e.jsx(zt,{rs:b})}),e.jsx(fe,{value:"earnings",className:"space-y-6",children:e.jsx(Lt,{rs:b})}),e.jsx(fe,{value:"channel-orders",className:"space-y-6",children:e.jsx(Tt,{rs:b})})]})]})}):e.jsx("div",{className:"min-h-screen bg-black flex items-center justify-center px-4",children:e.jsx(ie,{className:"bg-[#14181B] border-white/10 max-w-md w-full",children:e.jsxs(oe,{className:"p-8 text-center",children:[e.jsx("h1",{className:"text-3xl font-bold mb-8 text-white",children:"Access Denied"}),e.jsx(Xe,{to:"/",children:e.jsxs(K,{className:`${ke} px-6 py-2 flex items-center gap-2 mx-auto`,children:[e.jsx(et,{className:"h-4 w-4"}),"Back to Home"]})})]})})})};function qt({rs:a}){const[m,f]=i.useState(!1),[v,B]=i.useState(!1),[b,P]=i.useState({display_name:"",email:"",stripe_connect_account_id:"",is_house:!1,notes:""}),A=v?a.payees:a.payees.filter(u=>u.is_active),L=async()=>{if(!b.display_name.trim()){Y.error("display name required");return}try{await a.createPayee({display_name:b.display_name.trim(),email:b.email.trim()||null,stripe_connect_account_id:b.stripe_connect_account_id.trim()||null,is_house:b.is_house,notes:b.notes.trim()||null}),Y.success("Payee created"),f(!1),P({display_name:"",email:"",stripe_connect_account_id:"",is_house:!1,notes:""})}catch(u){Y.error(u.message)}};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsx("div",{children:e.jsxs("h2",{className:"text-xl font-semibold text-white inline-flex items-center gap-1",children:["Payees",e.jsx(ct,{text:Ce.payees})]})}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ye,{className:Je,checked:v,onCheckedChange:B}),e.jsx(le,{className:ne,children:"Show inactive"})]}),e.jsxs(Ve,{open:m,onOpenChange:f,children:[e.jsx(gt,{asChild:!0,children:e.jsxs(K,{className:ke,children:[e.jsx(Te,{className:"h-4 w-4 mr-2"})," Add payee"]})}),e.jsxs(Ke,{className:"bg-[#14181B] border-white/10 text-white",children:[e.jsx(Ue,{children:e.jsx(He,{className:"text-white",children:"New payee"})}),e.jsxs("div",{className:"grid gap-4 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(le,{className:ne,children:"Display name"}),e.jsx(te,{className:ae,value:b.display_name,onChange:u=>
6P({...b,display_name:u.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(le,{className:ne,children:"Email"}),e.jsx(te,{className:ae,value:b.email,onChange:u=>P({...b,email:u.target.value})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(qe,{className:ne,hint:Ce.stripeConnectId,children:"Stripe Connect ID (reference only)"}),e.jsx(te,{className:ae,placeholder:"acct_… (optional)",value:b.stripe_connect_account_id,onChange:u=>P({...b,stripe_connect_account_id:u.target.value})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Ye,{className:Je,checked:b.is_house,onCheckedChange:u=>P({...b,is_house:u})}),e.jsx(qe,{className:ne,hint:Ce.housePayee,children:"House payee (only one allowed)"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(qe,{className:ne,hint:Ce.notes,children:"Notes"}),e.jsx(ot,{className:ae,value:b.notes,onChange:u=>P({...b,notes:u.target.value})})]})]}),e.jsxs(_t,{children:[e.jsx(K,{variant:"ghost",className:pe,onClick:()=>f(!1),children:"Cancel"}),e.jsx(K,{onClick:L,className:ke,children:"Create"})]})]})]})]})]}),a.loading?e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"Loading…"})}):A.length===0?e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"py-12 text-center",children:[e.jsx(nt,{className:"h-10 w-10 text-gray-500 mx-auto mb-3"}),e.jsx("p",{className:"text-gray-400",children:"No payees yet. Create the house payee first."}),e.jsx(K,{variant:"ghost",onClick:()=>f(!0),className:"mt-3 text-illumigreen hover:text-illumigreen/80",children:"Add your first payee"})]})}):e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Name"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Email"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Stripe Connect (ref)"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Status"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right",children:"Actions"})]})}),e.jsx(me,{children:A.map(u=>e.jsx(Et,{payee:u,rs:a},u.id))})]})})]})}function Et({payee:a,rs:m}){const f=async()=>{try{await m.updatePayee(a.id,{is_active:!a.is_active})}catch(v){Y.error(v.message)}};return e.jsxs(Q,{className:"border-white/10 hover:bg-white/5",children:[e.jsx(x,{className:"py-2 text-white text-sm",children:e.jsxs("div",{className:"flex items-center gap-2",children:[a.display_name,a.is_house&&e.jsx(G,{className:"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider",children:"House"})]})}),e.jsx(x,{className:"py-2 text-sm text-gray-300",children:a.email??"—"}),e.jsx(x,{className:"py-2 text-sm text-gray-300 font-mono text-xs",children:a.stripe_connect_account_id??"—"}),e.jsx(x,{className:"py-2 text-sm",children:a.is_active?e.jsx(G,{className:"bg-green-600/20 text-green-400 border border-green-500/30 text-[10px] uppercase tracking-wider",children:"Active"}):e.jsx(G,{className:"bg-gray-600/30 text-gray-400 border border-gray-500/30 text-[10px] uppercase tracking-wider",children:"Inactive"})}),e.jsx(x,{className:"py-2 text-right",children:e.jsx(K,{variant:"ghost",size:"sm",className:pe,onClick:f,children:a.is_active?"Deactivate":"Activate"})})]})}function ee(a,m){return`${a}::${m??""}`}function Ot({rs:a}){var c,p;const[m,f]=i.useState([]),[v,B]=i.useState([]),[b,P]=i.useState([]),[A,L]=i.useState(!0),[u,I]=i.useState(""),[F,N]=i.useState("all"),[H,V]=i.useState(null),[O,E]=i.useState(null),[R,J]=i.useState(null),d=async()=>{try{const[t,l]=await Promise.all([a.listAllActiveShares(),a.listAllComponents()]);f(t),B(l.components),P(l.configs)}catch(t){Y.error(`Could not load splits: ${t.message}`)}};i.useEffect(()=>{L(!0),d().finally(()=>L(!1))},[a.sandbox]);const k=i.useMemo(()=>{const t=new Map;for(const l of m){if(l.in_bundle_product_id!==null)continue;const M=ee(l.product_id,l.variation_id),S=t.get(M)??[];S.push(l),t.set(M,S)}return t},[m]),j=i.useMemo(()=>{const t=new Map;for(const l of m){if(l.in_bundle_product_id===null)continue;const M=`${l.in_bundle_product_id}::${l.product_id}::${l.variation_id??""}`,S=t.get(M)??[];S.push(l),t.set(M,S)}return t},[m]),y=i.useMemo(()=>{const t=new Map;for(const l of v){const M=ee(l.bundle_product_id,l.bundle_variation_id),S=t.get(M)??[];S.push(l),t.set(M,S)}return t},[v]),w=i.useMemo(()=>{const t=new Map;for(const l of b)t.set(`${l.bundle_product_id}::${l.bundle_variation_id}`,l);return t},[b]),D=t=>{var l;return((l=a.payees.find(M=>M.id===t))==null?void 0:l.display_name)??t.slice(0,8)},z=t=>a.products.find(l=>l.product_id===t),U=(t,l)=>{const M=z(t);if(!M)return null;if(l){const S=M.variations.find($=>$.variation_id===l);if(S&&S.price!==null&&S.price!==void 0)return S.price}return M.price??null},_=(t,l,M=null)=>{if(M){if(l){const $=j.get(`${M}::${t}::${l}`)??[];if($.length>0)return $}const S=j.get(`${M}::${t}::`)??[];if(S.length>0)return S}if(l){const S=k.get(ee(t,l))??[];if(S.length>0)return S}return k.get(ee(t,null))??[]},r=t=>{const l=new Map,M=be(y,w,t,null);for(const $ of M)l.set(`${$.component_product_id}::${$.component_variation_id??""}`,{component_product_id:$.component_product_id,component_variation_id:$.component_variation_id});const S=z(t);if(S)for(const $ of S.variations){const W=be(y,w,t,$.variation_id);for(const T of W)l.set(`${T.component_product_id}::${T.component_variation_id??""}`,{component_product_id:T.component_product_id,component_variation_id:T.component_variation_id})}return Array.from(l.values())},n=t=>{const l=(k.get(ee(t.product_id,null))??[]).length>0,M=t.variations.some(W=>(k.get(ee(t.product_id,W.variation_id))??[]).length>0);if(l||M)return{kind:"own",componentCount:0,configuredComponents:0}
6;const S=be(y,w,t.product_id,null);if(S.length===0)return{kind:"missing",componentCount:0,configuredComponents:0};let $=0;for(const W of S)_(W.component_product_id,W.component_variation_id,t.product_id).length>0&&($+=1);return $===S.length?{kind:"inherited_full",componentCount:S.length,configuredComponents:$}:$>0?{kind:"inherited_partial",componentCount:S.length,configuredComponents:$}:{kind:"missing",componentCount:S.length,configuredComponents:0}},h=(t,l)=>{const M=be(y,w,t.product_id,l);if(M.length===0)return[];let S=0;const $=M.map(T=>{const X=U(T.component_product_id,T.component_variation_id)??0,ye=Math.round(X*T.quantity_per_bundle*100);return S+=ye,{...T,notional:ye}});if(S===0)return[];const W=new Map;for(const T of $){if(T.notional===0)continue;const X=T.notional/S,ye=_(T.component_product_id,T.component_variation_id,t.product_id),Ne=z(T.component_product_id),Pe=(Ne==null?void 0:Ne.name)??T.component_product_id;for(const ge of ye){const ue=ge.basis_points/1e4,Ze=X*ue,Re=W.get(ge.payee_id)??{payee_id:ge.payee_id,payee_name:D(ge.payee_id),basis_points:0,breakdown:[]};Re.basis_points+=Math.round(Ze*1e4),Re.breakdown.push({source:`${Pe} ${(X*100).toFixed(1)}% × ${ce(ge.basis_points)}%`,share:Ze}),W.set(ge.payee_id,Re)}}return Array.from(W.values()).sort((T,X)=>X.basis_points-T.basis_points)},C=i.useMemo(()=>{const t=u.trim().toLowerCase();return a.products.filter(l=>t?l.name.toLowerCase().includes(t)||l.product_id.toLowerCase().includes(t):!0).filter(l=>{if(F==="all")return!0;const M=n(l),S=M.kind==="own"||M.kind==="inherited_full";return F==="configured"?S:!S})},[a.products,u,F,k,y,w]),o=a.products.filter(t=>{const l=n(t);return l.kind==="own"||l.kind==="inherited_full"}).length,q=O&&a.products.find(t=>t.product_id===O.product_id),Z=(q==null?void 0:q.variations.find(t=>t.variation_id===(O==null?void 0:O.variation_id)))??null,s=O?k.get(ee(O.product_id,O.variation_id))??[]:[];return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsx("div",{children:e.jsxs("h2",{className:"text-xl font-semibold text-white inline-flex items-center gap-1",children:["Splits",e.jsx(ct,{text:Ce.splits})]})}),e.jsxs("div",{className:"text-xs text-gray-500",children:[o," of ",a.products.length," products configured"]})]}),e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"p-3 flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"relative flex-1 min-w-[14rem]",children:[e.jsx(it,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-gray-500"}),e.jsx(te,{className:`${ae} pl-8 h-9`,placeholder:"Search by name or product ID…",value:u,onChange:t=>I(t.target.value)})]}),e.jsx("div",{className:"flex items-center gap-1",children:["all","configured","missing"].map(t=>e.jsx(K,{variant:"ghost",size:"sm",className:F===t?"bg-illumigreen/20 text-illumigreen hover:bg-illumigreen/30 hover:text-illumigreen":pe,onClick:()=>N(t),children:t==="all"?"All":t==="configured"?"Configured":"Missing"},t))})]})}),e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:A||a.productsLoading?e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"Loading…"}):C.length===0?e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"No products match this filter."}):e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider w-8"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Product"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider hidden md:table-cell",children:"Product-level split"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Overrides"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Status"})]})}),e.jsx(me,{children:C.map(t=>{const l=k.get(ee(t.product_id,null))??[],M=t.variations.filter(W=>(k.get(ee(t.product_id,W.variation_id))??[]).length>0),S=H===t.product_id,$=n(t);return e.jsxs(i.Fragment,{children:[e.jsxs(Q,{className:"border-white/10 hover:bg-white/5 cursor-pointer",onClick:()=>V(S?null:t.product_id),children:[e.jsx(x,{className:"py-2",children:S?e.jsx(De,{className:"h-4 w-4 text-gray-400"}):e.jsx(we,{className:"h-4 w-4 text-gray-400"})}),e.jsx(x,{className:"py-2",children:e.jsxs("div",{className:"flex items-center gap-2",children:[t.thumbnail&&e.jsx("img",{src:t.thumbnail,alt:"",className:"h-7 w-7 rounded object-cover border border-white/10"}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm text-white",children:t.name||t.product_id}),e.jsx("span",{className:"text-[10px] text-gray-500 font-mono",children:t.product_id})]})]})}),e.jsx(x,{className:"py-2 hidden md:table-cell",children:l.length>0?e.jsx("div",{className:"flex flex-wrap gap-1",children:l.map(W=>e.jsxs(G,{className:"bg-illumigreen/10 text-illumigreen border border-illumigreen/30 text-[10px]",children:[D(W.payee_id)," ",ce(W.basis_points),"%"]},W.id))}):$.kind==="inherited_full"||$.kind==="inherited_partial"?e.jsxs("span",{className:"text-xs text-gray-400",children:["via ",$.configuredComponents," of ",$.componentCount," components"]}):e.jsx("span",{className:"text-xs text-gray-500",children:"— no product-level split"})}),e.jsx(x,{className:"py-2 text-sm text-right text-gray-300 hidden md:table-cell",children:M.length>0?e.jsxs("span",{children:[M.length," of ",t.variations.length]}):t.variations.length>0?e.jsxs("span",{className:"text-gray-500",children:["0 of ",t.variations.length]}):e.jsx("span",{className:"text-gray-500",children:"—"})}),e.jsx(x,{className:"py-2",children:$.kind==="own"?e.jsx(G,{className:"bg-green-600/20 text-green-400 border border-green-500/30 text-[10px] uppercase tracking-wider",children:"Configured"}):$.kind==="inherited_full"?e.jsxs(G,{className:"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider",children:["Inherited (",$.componentCount,")"]}):$.kind==="inherited_partial"?e.jsxs(G,{className:"bg-amber-500/20 text-amber-300 border border-amber-500/30 text-[10px] uppercase tracking-wider",children:["Partial (",$.configuredComponents,"/",$.componentCount,")"]}):e.jsx(G,{className:"bg-red-600/20 text-red-400 border border-red-500/30 text-[10px] uppercase tracking-wider",children:"Missing"})})]}),S&&e.jsx(Q,{className:"border-white/10 hover:bg-transparent",children:e.jsx(x,{colSpan:5,className:"p-0 bg-black/40",children:e.jsxs("div",{className:"p-4 space-y-4",children:[($.kind==="inherited_full"||$.kind==="inherited_partial")&&e.jsx(Vt,{title:"Effective payout (resolved from components)",description:$.kind==="inherited_full"?"Every component has splits configured — payouts will resolve automatically. Add a product-level split below only if you want to override component inheritance.":`Only ${$.configuredComponents} of ${$.componentCount}
6 components have splits configured — partial coverage means some revenue won't be assigned. Add splits to the components shown as missing below, or set a product-level split to override.`,payouts:h(t,null),resolved:be(y,w,t.product_id,null),componentSplitsFor:(W,T)=>_(W,T),productName:W=>{var T;return((T=z(W))==null?void 0:T.name)??W}}),e.jsx(ze,{rs:a,productId:t.product_id,variationId:null,existingLines:l,heading:"Product-level split",subheading:$.kind==="inherited_full"||$.kind==="inherited_partial"?"Leave empty to inherit from components (recommended). Add lines here only to override.":"Applies to any variation that doesn't have its own override.",onSaved:d}),t.variations.length>0&&e.jsxs(e.Fragment,{children:[e.jsx(We,{className:"bg-white/10"}),e.jsxs("div",{children:[e.jsx("div",{className:"flex items-center justify-between mb-2",children:e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-white",children:"Variation overrides"}),e.jsx("p",{className:"text-xs text-gray-500",children:"Override the product-level split for a specific variation."})]})}),e.jsx("div",{className:"space-y-1",children:t.variations.map(W=>{const T=k.get(ee(t.product_id,W.variation_id))??[];return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2 px-3 py-2 rounded bg-[#14181B] border border-white/5",children:[e.jsx("div",{className:"text-sm text-gray-300",children:W.name||W.variation_id}),e.jsxs("div",{className:"flex items-center gap-2",children:[T.length===0?e.jsx("span",{className:"text-xs text-gray-500",children:"inherits product-level"}):e.jsx("div",{className:"flex flex-wrap gap-1",children:T.map(X=>e.jsxs(G,{className:"bg-illumigreen/10 text-illumigreen border border-illumigreen/30 text-[10px]",children:[D(X.payee_id)," ",ce(X.basis_points),"%"]},X.id))}),e.jsxs(K,{variant:"ghost",size:"sm",className:pe,onClick:()=>E({product_id:t.product_id,variation_id:W.variation_id}),children:[e.jsx(Qe,{className:"h-3.5 w-3.5 mr-1"}),T.length===0?"Add":"Edit"]})]})]},W.variation_id)})})]})]}),(()=>{const W=r(t.product_id);return W.length===0?null:e.jsxs(e.Fragment,{children:[e.jsx(We,{className:"bg-white/10"}),e.jsxs("div",{children:[e.jsx("div",{className:"flex items-center justify-between mb-2",children:e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-white",children:"Component splits in this bundle context"}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Override how a component splits revenue when it's sold as part of ",e.jsx("em",{children:t.name||t.product_id}),". Standalone splits remain unaffected."]})]})}),e.jsx("div",{className:"space-y-1",children:W.map(T=>{var ge;const X=z(T.component_product_id),ye=j.get(`${t.product_id}::${T.component_product_id}::${T.component_variation_id??""}`)??[],Ne=_(T.component_product_id,T.component_variation_id,null),Pe=T.component_variation_id?((ge=X==null?void 0:X.variations.find(ue=>ue.variation_id===T.component_variation_id))==null?void 0:ge.name)??T.component_variation_id:null;return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2 px-3 py-2 rounded bg-[#14181B] border border-white/5",children:[e.jsxs("div",{className:"flex items-center gap-2 min-w-0",children:[(X==null?void 0:X.thumbnail)&&e.jsx("img",{src:X.thumbnail,alt:"",className:"h-5 w-5 rounded object-cover border border-white/10"}),e.jsx("div",{className:"min-w-0",children:e.jsxs("div",{className:"text-sm text-gray-300 truncate",children:[(X==null?void 0:X.name)??T.component_product_id,Pe&&e.jsxs("span",{className:"text-gray-500 text-xs ml-1",children:["· ",Pe]})]})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[ye.length>0?e.jsx("div",{className:"flex flex-wrap gap-1",children:ye.map(ue=>e.jsxs(G,{className:"bg-amber-500/20 text-amber-300 border border-amber-500/30 text-[10px]",children:[D(ue.payee_id)," ",ce(ue.basis_points),"%"]},ue.id))}):Ne.length>0?e.jsxs("span",{className:"text-xs text-gray-500",children:["inherits standalone:"," ",Ne.map(ue=>`${D(ue.payee_id)} ${ce(ue.basis_points)}%`).join(", ")]}):e.jsx("span",{className:"text-xs text-red-400",children:"no splits"}),e.jsxs(K,{variant:"ghost",size:"sm",className:pe,onClick:()=>J({bundle_product_id:t.product_id,component_product_id:T.component_product_id,component_variation_id:T.component_variation_id}),children:[e.jsx(Qe,{className:"h-3.5 w-3.5 mr-1"}),ye.length===0?"Override":"Edit"]})]})]},`${T.component_product_id}-${T.component_variation_id??""}`)})})]})]})})()]})})})]},t.product_id)})})]})}),e.jsx(Ve,{open:!!O,onOpenChange:t=>!t&&E(null),children:e.jsxs(Ke,{className:"bg-[#14181B] border-white/10 text-white max-w-3xl",children:[e.jsx(Ue,{children:e.jsxs(He,{className:"text-white",children:[q==null?void 0:q.name," → ",(Z==null?void 0:Z.name)??"variation"]})}),O&&e.jsx(ze,{rs:a,productId:O.product_id,variationId:O.variation_id,existingLines:s,heading:"",subheading:"",onSaved:async()=>{await d()},onDone:()=>E(null),allowClear:!0})]})}),e.jsx(Ve,{open:!!R,onOpenChange:t=>!t&&J(null),children:e.jsxs(Ke,{className:"bg-[#14181B] border-white/10 text-white max-w-3xl",children:[e.jsx(Ue,{children:e.jsx(He,{className:"text-white",children:R&&e.jsxs(e.Fragment,{children:[((c=z(R.component_product_id))==null?void 0:c.name)??R.component_product_id," ",e.jsx("span",{className:"text-gray-500 text-sm",children:"in"})," ",((p=z(R.bundle_product_id))==null?void 0:p.name)??R.bundle_product_id]})})}),R&&e.jsx(ze,{rs:a,productId:R.component_product_id,variationId:R.component_variation_id,inBundleProductId:R.bundle_product_id,existingLines:j.get(`${R.bundle_product_id}::${R.component_product_id}::${R.component_variation_id??""}
6`)??[],heading:"Bundle-context split",subheading:"Applies only when this component is sold as part of the bundle above. Standalone splits keep their own configuration.",onSaved:async()=>{await d()},onDone:()=>J(null),allowClear:!0})]})})]})}function ze({rs:a,productId:m,variationId:f,inBundleProductId:v=null,existingLines:B,heading:b,subheading:P,onSaved:A,onDone:L,allowClear:u}){const I=B.length?B.map(y=>({payee_id:y.payee_id,pct:ce(y.basis_points),role:y.role??""})):[{payee_id:"",pct:"100",role:""}],[F,N]=i.useState(I),[H,V]=i.useState(""),[O,E]=i.useState(!1);i.useEffect(()=>{N(B.length?B.map(y=>({payee_id:y.payee_id,pct:ce(y.basis_points),role:y.role??""})):[{payee_id:"",pct:"100",role:""}]),V("")},[m,f,v,B.length]);const R=i.useMemo(()=>a.payees.filter(y=>y.is_active),[a.payees]),J=F.reduce((y,w)=>y+st(w.pct),0),d=J===1e4,k=async()=>{if(!d){Y.error(`lines must sum to 100% (currently ${ce(J)}%)`);return}if(F.some(y=>!y.payee_id)){Y.error("every line needs a payee");return}E(!0);try{await a.replaceShares(m,f,F.map(y=>({payee_id:y.payee_id,basis_points:st(y.pct),role:y.role.trim()||null})),H.trim()||void 0,v),Y.success("Split saved"),V(""),await(A==null?void 0:A()),L==null||L()}catch(y){Y.error(y.message)}finally{E(!1)}},j=async()=>{if(confirm(v?"Remove this bundle-context override? The component will fall back to its standalone split.":"Remove this variation's override? It will inherit the product-level split.")){E(!0);try{await a.replaceShares(m,f,[],H.trim()||"cleared override",v),Y.success("Override cleared"),await(A==null?void 0:A()),L==null||L()}catch(w){Y.error(w.message)}finally{E(!1)}}};return e.jsxs("div",{className:"space-y-3",children:[(b||P)&&e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[b&&e.jsx("h4",{className:"text-sm font-medium text-white",children:b}),P&&e.jsx("p",{className:"text-xs text-gray-500",children:P})]}),e.jsxs(G,{className:d?"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider":"bg-red-600/20 text-red-400 border border-red-500/30 text-[10px] uppercase tracking-wider",children:["Total ",ce(J),"%"]})]}),!b&&!P&&e.jsx("div",{className:"flex justify-end",children:e.jsxs(G,{className:d?"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider":"bg-red-600/20 text-red-400 border border-red-500/30 text-[10px] uppercase tracking-wider",children:["Total ",ce(J),"%"]})}),F.map((y,w)=>e.jsxs("div",{className:"grid grid-cols-12 gap-2 items-end",children:[e.jsxs("div",{className:`col-span-12 md:col-span-6 space-y-2${w>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${w>0?" md:sr-only":""}`,children:"Payee"}),e.jsxs($e,{value:y.payee_id,onValueChange:D=>N(F.map((z,U)=>U===w?{...z,payee_id:D}:z)),children:[e.jsx(Be,{className:ae,children:e.jsx(Fe,{placeholder:"Select payee"})}),e.jsx(Me,{className:"bg-[#14181B] border-white/10 text-white",children:R.map(D=>e.jsxs(je,{value:D.id,children:[D.display_name,D.is_house?" (house)":""]},D.id))})]})]}),e.jsxs("div",{className:`col-span-4 md:col-span-2 space-y-2${w>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${w>0?" md:sr-only":""}`,children:"Share %"}),e.jsx(te,{className:`${ae} font-mono text-right`,inputMode:"decimal",value:y.pct,onChange:D=>N(F.map((z,U)=>U===w?{...z,pct:D.target.value}:z))})]}),e.jsxs("div",{className:`col-span-6 md:col-span-3 space-y-2${w>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${w>0?" md:sr-only":""}`,children:"Role (optional)"}),e.jsx(te,{className:ae,placeholder:"e.g. artist",value:y.role,onChange:D=>N(F.map((z,U)=>U===w?{...z,role:D.target.value}:z))})]}),e.jsx("div",{className:"col-span-2 md:col-span-1 flex justify-end",children:e.jsx(K,{variant:"ghost",size:"icon",className:"text-gray-400 hover:text-red-400",onClick:()=>N(F.filter((D,z)=>z!==w)),disabled:F.length===1,children:e.jsx(Ge,{className:"h-4 w-4"})})})]},w)),e.jsxs(K,{variant:"ghost",size:"sm",className:"text-illumigreen hover:text-illumigreen/80",onClick:()=>N([...F,{payee_id:"",pct:"0",role:""}]),children:[e.jsx(Te,{className:"h-4 w-4 mr-1"})," Add line"]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(le,{className:ne,children:"Change reason (optional)"}),e.jsx(ot,{className:ae,value:H,onChange:y=>V(y.target.value),placeholder:"Why is this split changing? Logged on every new version."})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("div",{className:"flex gap-2",children:u&&B.length>0&&e.jsx(K,{variant:"ghost",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",onClick:j,disabled:O,children:"Clear override"})}),e.jsxs("div",{className:"flex gap-2",children:[L&&e.jsx(K,{variant:"ghost",className:pe,onClick:L,disabled:O,children:"Cancel"}),e.jsx(K,{className:ke,onClick:k,disabled:O||!d,children:"Save"})]})]})]})}function he(a){return a.toISOString().slice(0,10)}const at=[{label:"This month",computeRange:()=>{const a=new Date;return{from:he(new Date(a.getFullYear(),a.getMonth(),1)),to:he(new Date(a.getFullYear(),a.getMonth()+1,
61))}}},{label:"Last month",computeRange:()=>{const a=new Date;return{from:he(new Date(a.getFullYear(),a.getMonth()-1,1)),to:he(new Date(a.getFullYear(),a.getMonth(),1))}}},{label:"Last 7 days",computeRange:()=>{const a=new Date,m=new Date(a.getFullYear(),a.getMonth(),a.getDate()+1),f=new Date(m);return f.setDate(f.getDate()-7),{from:he(f),to:he(m)}}},{label:"Last 30 days",computeRange:()=>{const a=new Date,m=new Date(a.getFullYear(),a.getMonth(),a.getDate()+1),f=new Date(m);return f.setDate(f.getDate()-30),{from:he(f),to:he(m)}}},{label:"Year to date",computeRange:()=>{const a=new Date;return{from:he(new Date(a.getFullYear(),0,1)),to:he(new Date(a.getFullYear(),a.getMonth(),a.getDate()+1))}}}];function zt({rs:a}){const m=at[1].computeRange(),[f,v]=i.useState(m.from),[B,b]=i.useState(m.to),[P,A]=i.useState("__all__"),[L,u]=i.useState("__all__"),[I,F]=i.useState(""),[N,H]=i.useState("payee"),[V,O]=i.useState(!1),[E,R]=i.useState([]),[J,d]=i.useState(!1),[k,j]=i.useState(null),[y,w]=i.useState(null),D=i.useMemo(()=>{const s=I.trim().toLowerCase();return s?a.products.filter(c=>c.name.toLowerCase().includes(s)||c.product_id.toLowerCase().includes(s)):a.products},[a.products,I]),z=async()=>{if(!f||!B){Y.error("from and to required");return}O(!0);try{const s=await a.reconcileLines(new Date(f).toISOString(),new Date(B).toISOString(),{payee_id:P==="__all__"?null:P,product_id:L==="__all__"?null:L});R(s.rows),w({from:s.from,to:s.to,is_sandbox:s.is_sandbox}),d(!0),j(null)}catch(s){Y.error(s.message)}finally{O(!1)}},U=s=>{var c;return((c=a.products.find(p=>p.product_id===s))==null?void 0:c.name)??s},_=(s,c)=>{var p,t;return c?((t=(p=a.products.find(l=>l.product_id===s))==null?void 0:p.variations.find(l=>l.variation_id===c))==null?void 0:t.name)??c:null},r=i.useMemo(()=>{let s=0,c=0,p=0,t=0;const l=new Set,M=new Set,S=new Set;for(const $ of E)l.add($.order_id),M.add($.payee_id),p+=$.payout_cents,S.has($.order_line_item_id)||(S.add($.order_line_item_id),s+=$.gross_cents,c+=$.net_basis_cents,t+=$.prorated_refund_cents);return{grossCents:s,netCents:c,payoutCents:p,refundCents:t,orderCount:l.size,lineCount:S.size,payeeCount:M.size}},[E]),n=i.useMemo(()=>{const s=new Map;for(const c of E){const p=s.get(c.payee_id)??{payee_id:c.payee_id,payee_name:c.payee_name,line_count:0,payout_cents:0,by_source:new Map};p.line_count+=1,p.payout_cents+=c.payout_cents;const t=c.source_product_id,l=p.by_source.get(t)??{product_id:t,payout_cents:0,line_count:0};l.payout_cents+=c.payout_cents,l.line_count+=1,p.by_source.set(t,l),s.set(c.payee_id,p)}return Array.from(s.values()).sort((c,p)=>p.payout_cents-c.payout_cents)},[E]),h=i.useMemo(()=>{const s=new Map;for(const c of E){const p=`${c.payee_id}::${c.source_product_id}`,t=s.get(p)??{payee_id:c.payee_id,payee_name:c.payee_name,product_id:c.source_product_id,payout_cents:0,line_count:0};t.payout_cents+=c.payout_cents,t.line_count+=1,s.set(p,t)}return Array.from(s.values()).sort((c,p)=>p.payout_cents-c.payout_cents)},[E]),C=i.useMemo(()=>{const s=new Map;for(const c of E){const p=s.get(c.order_id)??{order_id:c.order_id,paid_at:c.paid_at,total_payout_cents:0,total_gross_cents:0,total_net_cents:0,total_refund_cents:0,line_items:new Map,payees:new Map};p.total_payout_cents+=c.payout_cents,p.line_items.has(c.order_line_item_id)||(p.line_items.set(c.order_line_item_id,{order_line_item_id:c.order_line_item_id,product_id:c.product_id,variation_id:c.variation_id,gross_cents:c.gross_cents,net_basis_cents:c.net_basis_cents,refund_cents:c.prorated_refund_cents}),p.total_gross_cents+=c.gross_cents,p.total_net_cents+=c.net_basis_cents,p.total_refund_cents+=c.prorated_refund_cents);const t=p.payees.get(c.payee_id)??{payee_name:c.payee_name,payout_cents:0};t.payout_cents+=c.payout_cents,p.payees.set(c.payee_id,t),s.set(c.order_id,p)}return Array.from(s.values()).sort((c,p)=>!c.paid_at||!p.paid_at?0:new Date(p.paid_at).getTime()-new Date(c.paid_at).getTime())},[E]),[o,q]=i.useState(null),Z=()=>{var s,c;if(E.length===0){Y.error("nothing to export");return}St(`payouts_${((s=y==null?void 0:y.from)==null?void 0:s.slice(0,10))??f}_${((c=y==null?void 0:y.to)==null?void 0:c.slice(0,10))??B}.csv`,["paid_at","order_id","order_line_item_id","bundle_product_id","bundle_variation_id","source_product_id","source_variation_id","allocation_basis_points","payee_name","payee_basis_points","role","gross_cents","prorated_refund_cents","net_basis_cents","payout_cents"],E.map(p=>[p.paid_at??"",p.order_id,p.order_line_item_id,p.product_id,p.variation_id??"",p.source_product_id,p.source_variation_id??"",p.allocation_basis_points,p.payee_name,p.basis_points,p.role??"",p.gross_cents,p.prorated_refund_cents,p.net_basis_cents,p.payout_cents]))};return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-semibold text-white",children:"Reconcile"}),e.jsx("p",{className:"text-sm text-gray-400",children:"What each payee is owed for a date range. Net of prorated refunds. Bundles with no own splits are expanded into their components automatically."})]}),J&&y&&e.jsx(G,{className:y.is_sandbox?"bg-amber-500/20 text-amber-300 border border-amber-500/30 text-[10px] uppercase tracking-wider":"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider",children:y.is_sandbox?"Sandbox data":"Live data"})]}),e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"p-3 space-y-3",children:[e.jsx("div",{className:"flex flex-wrap items-center gap-1",children:at.map(s=>e.jsx(K,{variant:"ghost",size:"sm",className:pe,onClick:()=>{const c=s.computeRange();v(c.from),b(c.to)},children:s.label},s.label))}),e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-12 gap-3 items-end",children:[e.jsxs("div",{className:"md:col-span-2 space-y-2",children:[e.jsx(le,{className:ne,children:"From"}),e.jsx(te,{type:"date",className:ae,value:f,onChange:s=>v(s.target.value)})]}),e.jsxs("div",{className:"md:col-span-2 space-y-2",children:[e.jsx(le,{className:ne,children:"To (excl.)"}),e.jsx(te,{type:"date",className:ae,value:B,onChange:s=>b(s.target.value)})]}),e.jsxs("div",{className:"md:col-span-3 space-y-2",children:[e.jsx(le,{className:ne,children:"Payee"}),e.jsxs($e,{value:P,onValueChange:A,children:[e.jsx(Be,{className:ae,children:e.jsx(Fe,{})}),e.jsxs(Me,{className:"bg-[#14181B] border-white/10 text-white",children:[e.jsx(je,{value:"__all__",children:"All payees"}),a.payees.filter(s=>s.is_active).map(s=>e.jsxs(je,{value:s.id,children:[s.display_name,s.is_house?" (house)":""]},s.id))]})]})]}),e.jsxs("div",{className:"md:col-span-3 space-y-2",children:[e.jsx(le,{className:ne,children:"Product (line)"}),e.jsxs($e,{value:L,onValueChange:u,children:[e.jsx(Be,{className:ae,children:e.jsx(Fe,{placeholder:"All products"})}),e.jsxs(Me,{className:"bg-[#14181B] border-white/10 text-white max-h-[24rem]",children:[e.jsx("div",{className:"p-2 sticky top-0 bg-[#14181B] z-10 border-b border-white/10",children:e.jsx(te,{className:ae,placeholder:"Search…",value:I,onChange:s=>F(s.target.value),onKeyDown:s=>s.stopPropagation()})}),e.jsx(je,{value:"__all__",children:"All products"}),D.map(s=>e.jsx(je,{value:s.product_id,children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("span",{children:s.name||s.product_id}),e.jsx("span",{className:"text-[10px] text-gray-500 font-mono",children:s.product_id})]})},s.product_id))]})]})]}),e.jsx("div",{className:"md:col-span-2 flex justify-end gap-2",children:e.jsx(K,{className:ke,onClick:z,disabled:V,children:V?"Running…":"Run"})})]})]})}),J&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-3",children:[e.jsx(Ae,{label:"Total payout",value:se(r.payoutCents),accent:!0}),e.jsx(Ae,{label:"Gross revenue",value:se(r.grossCents)}),e.jsx(Ae,{label:"Net basis",value:se(r.netCents),hint:`refunded ${se(r.refundCents)}`}),e.jsx(Ae,{label:"Activity",value:`${r.orderCount} orders`,hint:`${r.lineCount} lines · ${r.payeeCount} payees`})]}),e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"p-3 flex flex-wrap items-center justify-between gap-3",children:[e.jsx("div",{className:"flex flex-wrap items-center gap-1",children:["payee","payee_source","order","lines"].map(s=>e.jsxs(K,{variant:"ghost",size:"sm",className:N===s?"bg-illumigreen/20 text-illumigreen hover:bg-illumigreen/30 hover:text-illumigreen":pe,onClick:()=>H(s),children:[s==="payee"&&"By payee",s==="payee_source"&&"Payee × source",s==="order"&&"By order",s==="lines"&&"Line items"]},s))}),e.jsx(K,{variant:"ghost",size:"sm",className:pe,onClick:Z,children:"Export CSV"})]})}),E.length===0?e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"No paid orders in this window for the selected filters."})}):N==="payee"?e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider w-8"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Payee"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Lines"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Sources"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"% of total"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right",children:"Payout"})]})}),e.jsx(me,{children:n.map(s=>{const c=k===s.payee_id,p=r.payoutCents>0?s.payout_cents/r.payoutCents*100:0;return e.jsxs(i.Fragment,{children:[e.jsxs(Q,{className:"border-white/10 hover:bg-white/5 cursor-pointer",onClick:()=>j(c?null:s.payee_id),children:[e.jsx(x,{className:"py-2",children:c?e.jsx(De,{className:"h-4 w-4 text-gray-400"}):e.jsx(we,{className:"h-4 w-4 text-gray-400"})}),e.jsx(x,{className:"py-2 text-white text-sm",children:s.payee_name}),e.jsx(x,{className:"py-2 text-sm text-gray-300 text-right hidden md:table-cell",children:s.line_count}),e.jsx(x,{className:"py-2 text-sm text-gray-300 text-right hidden md:table-cell",children:s.by_source.size}),e.jsxs(x,{className:"py-2 text-sm text-gray-300 text-right font-mono hidden md:table-cell",children:[p.toFixed(1),"%"]}),e.jsx(x,{className:"py-2 text-sm text-white text-right font-mono font-medium",children:se(s.payout_cents)})]}),c&&e.jsx(Q,{className:"border-white/10 hover:bg-transparent",children:e.jsx(x,{colSpan:6,className:"p-0 bg-black/40",children:e.jsxs("div",{className:"p-4 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-xs uppercase tracking-wider text-gray-400 mb-2",children:"By 
6source product"}),e.jsx("div",{className:"space-y-1",children:Array.from(s.by_source.values()).sort((t,l)=>l.payout_cents-t.payout_cents).map(t=>e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-x-3 gap-y-1 px-3 py-1.5 rounded bg-[#14181B] border border-white/5 text-sm",children:[e.jsx("span",{className:"text-gray-300",children:U(t.product_id)}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs("span",{className:"text-xs text-gray-500",children:[t.line_count," lines"]}),e.jsx("span",{className:"text-white font-mono",children:se(t.payout_cents)})]})]},t.product_id))})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-xs uppercase tracking-wider text-gray-400 mb-2",children:"Contributing line items"}),e.jsx("div",{className:"rounded border border-white/5 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/5 hover:bg-transparent bg-[#14181B]",children:[e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider hidden md:table-cell",children:"Date"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider",children:"Sold"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider hidden md:table-cell",children:"Source"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right",children:"Net"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right hidden md:table-cell",children:"Split"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right",children:"Payout"})]})}),e.jsx(me,{children:E.filter(t=>t.payee_id===s.payee_id).map((t,l)=>{const M=t.source_product_id!==t.product_id||t.allocation_basis_points!==1e4,S=Math.round(t.net_basis_cents*t.allocation_basis_points/1e4);return e.jsxs(Q,{className:"border-white/5 hover:bg-white/5",children:[e.jsx(x,{className:"py-1.5 text-[11px] text-gray-400 hidden md:table-cell",children:t.paid_at?new Date(t.paid_at).toLocaleDateString():"—"}),e.jsxs(x,{className:"py-1.5 text-[11px] text-gray-300",children:[U(t.product_id),t.variation_id&&e.jsxs("span",{className:"text-gray-500",children:[" · ",_(t.product_id,t.variation_id)]})]}),e.jsx(x,{className:"py-1.5 text-[11px] hidden md:table-cell",children:M?e.jsxs("span",{className:"text-amber-300",children:[U(t.source_product_id),e.jsxs("span",{className:"text-gray-500",children:[" · ",(t.allocation_basis_points/100).toFixed(1),"% of bundle"]})]}):e.jsx("span",{className:"text-gray-300",children:"direct"})}),e.jsx(x,{className:"py-1.5 text-[11px] text-gray-300 text-right font-mono",children:se(S)}),e.jsxs(x,{className:"py-1.5 text-[11px] text-gray-300 text-right font-mono hidden md:table-cell",children:[ce(t.basis_points),"%"]}),e.jsx(x,{className:"py-1.5 text-[11px] text-white text-right font-mono",children:se(t.payout_cents)})]},`${t.order_line_item_id}-${l}`)})})]})})]})]})})})]},s.payee_id)})})]})}):N==="payee_source"?e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Payee"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Source product"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Lines"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right",children:"Payout"})]})}),e.jsx(me,{children:h.map(s=>e.jsxs(Q,{className:"border-white/10 hover:bg-white/5",children:[e.jsx(x,{className:"py-2 text-white text-sm",children:s.payee_name}),e.jsxs(x,{className:"py-2 text-gray-300 text-sm",children:[U(s.product_id),e.jsx("span",{className:"ml-2 text-[10px] text-gray-500 font-mono",children:s.product_id})]}),e.jsx(x,{className:"py-2 text-sm text-gray-300 text-right hidden md:table-cell",children:s.line_count}),e.jsx(x,{className:"py-2 text-sm text-white text-right font-mono font-medium",children:se(s.payout_cents)})]},`${s.payee_id}-${s.product_id}`))})]})}):N==="order"?e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider w-8"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Order"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider hidden md:table-cell",children:"Sold"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Net"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Payouts"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right",children:"Total"})]})}),e.jsx(me,{children:C.map(s=>{const c=o===s.order_id,p=Array.from(s.line_items.values());return e.jsxs(i.Fragment,{children:[e.jsxs(Q,{className:"border-white/10 hover:bg-white/5 cursor-pointer",onClick:()=>q(c?null:s.order_id),children:[e.jsx(x,{className:"py-2",children:c?e.jsx(De,{className:"h-4 w-4 text-gray-400"}):e.jsx(we,{className:"h-4 w-4 text-gray-400"})}),e.jsxs(x,{className:"py-2",children:[e.jsx("div",{className:"text-xs font-mono text-gray-300",children:s.order_id.slice(0,8)}),e.jsx("div",{className:"text-[10px] text-gray-500",children:s.paid_at?new Date(s.paid_at).toLocaleStr
6ing():"—"})]}),e.jsx(x,{className:"py-2 text-sm text-gray-300 hidden md:table-cell",children:p.length===1?e.jsxs(e.Fragment,{children:[U(p[0].product_id),p[0].variation_id&&e.jsxs("span",{className:"text-gray-500 text-xs",children:[" · ",_(p[0].product_id,p[0].variation_id)]})]}):e.jsxs("span",{children:[p.length," line items"]})}),e.jsx(x,{className:"py-2 text-sm text-gray-300 text-right font-mono hidden md:table-cell",children:se(s.total_net_cents)}),e.jsx(x,{className:"py-2",children:e.jsx("div",{className:"flex flex-wrap gap-1",children:Array.from(s.payees.values()).map((t,l)=>e.jsxs(G,{className:"bg-illumigreen/10 text-illumigreen border border-illumigreen/30 text-[10px]",children:[t.payee_name," ",se(t.payout_cents)]},`${t.payee_name}-${l}`))})}),e.jsx(x,{className:"py-2 text-sm text-white text-right font-mono font-medium",children:se(s.total_payout_cents)})]}),c&&e.jsx(Q,{className:"border-white/10 hover:bg-transparent",children:e.jsx(x,{colSpan:6,className:"p-0 bg-black/40",children:e.jsxs("div",{className:"p-4 space-y-4",children:[e.jsxs("div",{children:[e.jsxs("h4",{className:"text-xs uppercase tracking-wider text-gray-400 mb-2",children:["Line items (",p.length,")"]}),e.jsx("div",{className:"space-y-1",children:p.map(t=>e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-x-3 gap-y-1 px-3 py-1.5 rounded bg-[#14181B] border border-white/5 text-sm",children:[e.jsxs("div",{className:"text-gray-300",children:[U(t.product_id),t.variation_id&&e.jsxs("span",{className:"text-gray-500 text-xs",children:[" · ",_(t.product_id,t.variation_id)]})]}),e.jsxs("div",{className:"flex items-center gap-3 text-xs",children:[e.jsxs("span",{className:"text-gray-500",children:["gross ",se(t.gross_cents)]}),t.refund_cents>0&&e.jsxs("span",{className:"text-red-400",children:["-",se(t.refund_cents)]}),e.jsxs("span",{className:"text-gray-300 font-mono",children:["net ",se(t.net_basis_cents)]})]})]},t.order_line_item_id))})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-xs uppercase tracking-wider text-gray-400 mb-2",children:"Payout attributions"}),e.jsx("div",{className:"rounded border border-white/5 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/5 hover:bg-transparent bg-[#14181B]",children:[e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider",children:"Sold"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider",children:"Source"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider",children:"Payee"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right hidden md:table-cell",children:"Alloc"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right hidden md:table-cell",children:"Split"}),e.jsx(g,{className:"text-gray-400 text-[10px] uppercase tracking-wider text-right",children:"Payout"})]})}),e.jsx(me,{children:E.filter(t=>t.order_id===s.order_id).map((t,l)=>{const M=t.source_product_id!==t.product_id||t.allocation_basis_points!==1e4;return e.jsxs(Q,{className:"border-white/5 hover:bg-white/5",children:[e.jsx(x,{className:"py-1.5 text-[11px] text-gray-300",children:U(t.product_id)}),e.jsx(x,{className:"py-1.5 text-[11px]",children:M?e.jsx("span",{className:"text-amber-300",children:U(t.source_product_id)}):e.jsx("span",{className:"text-gray-300",children:"direct"})}),e.jsx(x,{className:"py-1.5 text-[11px] text-gray-300",children:t.payee_name}),e.jsxs(x,{className:"py-1.5 text-[11px] text-gray-400 text-right font-mono hidden md:table-cell",children:[(t.allocation_basis_points/100).toFixed(1),"%"]}),e.jsxs(x,{className:"py-1.5 text-[11px] text-gray-400 text-right font-mono hidden md:table-cell",children:[ce(t.basis_points),"%"]}),e.jsx(x,{className:"py-1.5 text-[11px] text-white text-right font-mono",children:se(t.payout_cents)})]},`${t.order_line_item_id}-${t.payee_id}-${l}`)})})]})})]})]})})})]},s.order_id)})})]})}):e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider hidden md:table-cell",children:"Date"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Order"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Sold"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider hidden md:table-cell",children:"Source"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Payee"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Net"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Split"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right",children:"Payout"})]})}),e.jsx(me,{children:E.map((s,c)=>{const p=s.source_product_id!==s.product_id||s.allocation_basis_points!==1e4,t=Math.round(s.net_basis_cents
6*s.allocation_basis_points/1e4);return e.jsxs(Q,{className:"border-white/10 hover:bg-white/5",children:[e.jsx(x,{className:"py-2 text-[11px] text-gray-400 hidden md:table-cell",children:s.paid_at?new Date(s.paid_at).toLocaleDateString():"—"}),e.jsx(x,{className:"py-2 text-[11px] text-gray-300 font-mono",children:s.order_id.slice(0,8)}),e.jsxs(x,{className:"py-2 text-xs text-gray-300",children:[U(s.product_id),s.variation_id&&e.jsxs("span",{className:"text-gray-500",children:[" · ",_(s.product_id,s.variation_id)]})]}),e.jsx(x,{className:"py-2 text-xs hidden md:table-cell",children:p?e.jsxs("span",{className:"text-amber-300",children:[U(s.source_product_id),e.jsxs("span",{className:"text-gray-500",children:[" · ",(s.allocation_basis_points/100).toFixed(1),"%"]})]}):e.jsx("span",{className:"text-gray-300",children:"direct"})}),e.jsx(x,{className:"py-2 text-xs text-gray-300",children:s.payee_name}),e.jsx(x,{className:"py-2 text-xs text-gray-300 text-right font-mono hidden md:table-cell",children:se(t)}),e.jsxs(x,{className:"py-2 text-xs text-gray-300 text-right font-mono hidden md:table-cell",children:[ce(s.basis_points),"%"]}),e.jsx(x,{className:"py-2 text-xs text-white text-right font-mono font-medium",children:se(s.payout_cents)})]},`${s.order_line_item_id}-${s.payee_id}-${c}`)})})]})})]})]})}function Ae({label:a,value:m,hint:f,accent:v}){return e.jsx(ie,{className:v?"bg-illumigreen/10 border-illumigreen/30":"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"p-4",children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wider text-gray-400",children:a}),e.jsx("div",{className:`text-xl font-semibold font-mono mt-1 ${v?"text-illumigreen":"text-white"}`,children:m}),f&&e.jsx("div",{className:"text-[10px] text-gray-500 mt-1",children:f})]})})}const Le=a=>({component_product_id:a.component_product_id,component_variation_id:a.component_variation_id,quantity_per_bundle:a.quantity_per_bundle,component_kind:a.component_kind,notes:a.notes});function It({rs:a}){const[m,f]=i.useState([]),[v,B]=i.useState([]),[b,P]=i.useState(!0),[A,L]=i.useState(""),[u,I]=i.useState("all"),[F,N]=i.useState(null),H=async()=>{try{const d=await a.listAllComponents();f(d.components),B(d.configs)}catch(d){Y.error(`Could not load bundles: ${d.message}`)}};i.useEffect(()=>{P(!0),H().finally(()=>P(!1))},[a.sandbox]);const V=i.useMemo(()=>{const d=new Map;for(const k of m){const j=ee(k.bundle_product_id,k.bundle_variation_id),y=d.get(j)??[];y.push(k),d.set(j,y)}return d},[m]),O=i.useMemo(()=>{const d=new Map;for(const k of v)d.set(`${k.bundle_product_id}::${k.bundle_variation_id}`,k);return d},[v]),E=d=>a.products.find(k=>k.product_id===d),R=i.useMemo(()=>{const d=A.trim().toLowerCase();return a.products.filter(k=>d?k.name.toLowerCase().includes(d)||k.product_id.toLowerCase().includes(d):!0).filter(k=>{if(u==="all")return!0;const j=(V.get(ee(k.product_id,null))??[]).length>0||k.variations.some(y=>(V.get(ee(k.product_id,y.variation_id))??[]).length>0);return u==="bundles"?j:!j})},[a.products,A,u,V]),J=a.products.filter(d=>{const k=(V.get(ee(d.product_id,null))??[]).length>0,j=d.variations.some(y=>(V.get(ee(d.product_id,y.variation_id))??[]).length>0);return k||j}).length;return e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-3",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-semibold text-white",children:"Bundles"}),e.jsx("p",{className:"text-sm text-gray-400",children:"Every product at a glance. Bundles show their components inline; click a row to edit."})]}),e.jsxs("div",{className:"text-xs text-gray-500",children:[J," of ",a.products.length," products are bundles"]})]}),e.jsx(ie,{className:"bg-[#14181B] border-white/10",children:e.jsxs(oe,{className:"p-3 flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"relative flex-1 min-w-[14rem]",children:[e.jsx(it,{className:"absolute left-2.5 top-1/2 -translate-y-1/2 h-3.5 w-3.5 text-gray-500"}),e.jsx(te,{className:`${ae} pl-8 h-9`,placeholder:"Search by name or product ID…",value:A,onChange:d=>L(d.target.value)})]}),e.jsx("div",{className:"flex items-center gap-1",children:["all","bundles","non_bundles"].map(d=>e.jsx(K,{variant:"ghost",size:"sm",className:u===d?"bg-illumigreen/20 text-illumigreen hover:bg-illumigreen/30 hover:text-illumigreen":pe,onClick:()=>I(d),children:d==="all"?"All":d==="bundles"?"Bundles only":"Non-bundles"},d))})]})}),e.jsx(ie,{className:"bg-[#14181B] border-white/10 overflow-hidden",children:b||a.productsLoading?e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"Loading…"}):R.length===0?e.jsx(oe,{className:"py-12 text-center text-gray-400",children:"No products match this filter."}):e.jsxs(de,{children:[e.jsx(xe,{children:e.jsxs(Q,{className:"border-white/10 hover:bg-transparent",children:[e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider w-8"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Product"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider hidden md:table-cell",children:"Components"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Bundle price"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider text-right hidden md:table-cell",children:"Notional"}),e.jsx(g,{className:"text-gray-400 text-xs uppercase tracking-wider",children:"Status"})]})}),e.jsx(me,{children:R.map(d=>{const k=V.get(ee(d.product_id,null))??[],j=d.variations.filter(n=>(V.get(ee(d.product_id,n.variation_id))??[]).length>0),y=F===d.product_id,w=k.length>0||j.length>0,D=be(V,O,d.product_id,null);let z=0,U=!1;for(const n of D){const h=E(n.component_product_id);let C=null;if(h){if(n.component_variation_id){const o=h.variations.find(q=>q.variation_id===n.component_variation_id);o&&o.price!==null&&o.price!==void 0&&(C=o.price)}C===null&&(C=h.price??null)}C===null?U=!0:z+=Math.round(C*n.quantity_per_bundle*100)}const _=d.price!==null?Math.round(d.price*100):null,r=w&&_!==null&&z>0&&_>z;return e.jsxs(i.Fragment,{children:[e.jsxs(Q,{className:"border-white/10 hover:bg-white/5 cursor-pointer",onClick:()=>N(y?null:d.product_id),children:[e.jsx(x,{className:"py-2",children:y?e.jsx(De,{className:"h-4 w-4 text-gray-400"}):e.jsx(we,{className:"h-4 w-4 text-gray-400"})}),e.jsx(x,{className:"py-2",children:e.jsxs("div",{className:"flex items-center gap-2",children:[d.thumbnail&&e.jsx("img",{src:d.thumbnail,alt:"",className:"h-7 w-7 rounded object-cover border border-white/10"}),e.jsxs("div",{className:"flex flex-col",children:[e.jsx("span",{className:"text-sm text-white",children:d.name||d.product_id}),e.jsx("span",{className:"text-[10px] text-gray-500 font-mono",children:d.product_id})]})]})}),e.jsx(x,{className:"py-2 hidden md:table-cell",children:w?e.jsxs("div",{className:"flex flex-wrap gap-1",children:[D.map((n,h)=>{const C=E(n.component_product_id);return e.jsxs(G,{className:"bg-illumigreen/10 text-illumigreen border border-illumigreen/30 text-[10px]",children:[(C==null?void 0:C.name)??n.component_product_id," ×",n.quantity_per_bundle]},`${n.component_product_id}-${n.component_variation_id??""}-${h}`)}),j.length>0&&e.jsxs("span",{className:"text-[10px] text-gray-500",children:[j.length," variation override",j.length===1?"":"s"]})]}):e.jsx("span",{className:"text-xs text-gray-500",children:"— not a bundle"})}),e.jsx(x,{className:"py-2 text-sm text-right text-gray-300 font-mono hidden md:table-cell",children:_!==null?`$${(_/100).toFixed(2)}`:"—"}),e.jsx(x,{className:"py-2 text-sm text-right text-gray-300 font-mono hidden md:table-cell",children:w?e.jsxs(e.Fragment,{children:["$",(z/100).toFixed(2),U&&e.jsx("span",{className:"text-red-400 text-[10px] ml-1",children:"*"})]}):e.jsx("span",{className:"text-gray-500",children:"—"})}),e.jsx(x,{className:"py-2",children:w?r?e.jsx(G,{className:"bg-red-600/20 text-red-400 border border-red-500/30 text-[10px] uppercase tracking-wider",children:"Overpriced"}):e.jsx(G,{className:"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider",children:"Bundle"}):e.jsx(G,{className:"bg-gray-600/30 text-gray-400 border border-gray-500/30 text-[10px] uppercase tracking-wider",children:"Not bundle"})})]}),y&&e.jsx(Q,{className:"border-white/10 hover:bg-transparent",children:e.jsx(x,{colSpan:6,className:"p-0 bg-black/40",children:e.jsxs("div",{className:"p-4 space-y-4",children:[e.jsx(rt,{rs:a,bundleProduct:d,bundleVariationId:null,existing:k,onSaved:H}),d.variations.length>0&&e.jsxs(e.Fragment,{children:[e.jsx(We,{className:"bg-white/10"}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-white mb-1",children:"Variation-specific bundle compositions"}),e.jsx("p",{className:"text-xs text-gray-500 mb-3",children:"Override the product-level composition for a specific variation."}),e.jsx("div",{className:"space-y-3",children:d.variations.map(n=>{const h=V.get(ee(d.product_id,n.variation_id))??[],C=O.get(`${d.product_id}::${n.variation_id}`),o=(C==null?void 0:C.inherit_from_product)??!0,q=be(V,O,d.product_id,n.variation_id),Z=
6h.length>0;return e.jsxs("details",{className:"rounded border border-white/5 bg-[#14181B] open:border-illumigreen/20",children:[e.jsxs("summary",{className:"cursor-pointer list-none px-3 py-2 flex flex-wrap items-center justify-between gap-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(we,{className:"h-3.5 w-3.5 text-gray-400"}),e.jsx("span",{className:"text-sm text-gray-300",children:n.name||n.variation_id}),!o&&e.jsx(G,{className:"bg-amber-500/20 text-amber-300 border border-amber-500/30 text-[10px] uppercase tracking-wider",children:"Standalone"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-[10px] text-gray-500",children:[q.length," resolved"]}),Z?e.jsxs(G,{className:"bg-illumigreen/10 text-illumigreen border border-illumigreen/30 text-[10px]",children:[h.length," override",h.length===1?"":"s"]}):o?e.jsx("span",{className:"text-xs text-gray-500",children:"inherits product-level"}):e.jsx("span",{className:"text-xs text-gray-500",children:"empty"})]})]}),e.jsx("div",{className:"p-3 border-t border-white/5",children:e.jsx(rt,{rs:a,bundleProduct:d,bundleVariationId:n.variation_id,existing:h,productLevelComponents:V.get(ee(d.product_id,null))??[],inheritFromProduct:o,onSaved:H,allowClear:!0})})]},n.variation_id)})})]})]})]})})})]},d.product_id)})})]})})]})}function rt({rs:a,bundleProduct:m,bundleVariationId:f,existing:v,productLevelComponents:B=[],inheritFromProduct:b,onSaved:P,allowClear:A}){const L=f!==null,[u,I]=i.useState(v.map(Le)),[F,N]=i.useState(b??!0),[H,V]=i.useState(!1);i.useEffect(()=>{I(v.map(Le)),N(b??!0)},[m.product_id,f,v.length,b]);const O=r=>a.products.find(n=>n.product_id===r),E=(r,n)=>{const h=O(r);if(!h)return null;if(n){const C=h.variations.find(o=>o.variation_id===n);if(C&&C.price!==null&&C.price!==void 0)return C.price}return h.price??null},R=(r,n)=>`${r}::${n??""}`,J=new Set(u.map(r=>R(r.component_product_id,r.component_variation_id))),d=L&&F?B.filter(r=>!J.has(R(r.component_product_id,r.component_variation_id))):[],k=L&&F?[...d.map(r=>({component_product_id:r.component_product_id,component_variation_id:r.component_variation_id,quantity_per_bundle:r.quantity_per_bundle})),...u.filter(r=>r.quantity_per_bundle>0)]:u.filter(r=>r.quantity_per_bundle>0),j=k.reduce((r,n)=>{const h=E(n.component_product_id,n.component_variation_id);return h===null?r:r+Math.round(h*n.quantity_per_bundle*100)},0),y=k.some(r=>E(r.component_product_id,r.component_variation_id)===null),w=m.price!==null?Math.round(m.price*100):null,D=r=>{I([...u,Le(r)])},z=r=>{I([...u,{...Le(r),quantity_per_bundle:0}])},U=async()=>{if(u.some(h=>!h.component_product_id||!Number.isFinite(h.quantity_per_bundle)||h.quantity_per_bundle<0)){Y.error("every component needs a product and quantity >= 0");return}const n=yt(u,a.products);if(n.length>0){Y.error(`pick a variation for: ${n.join(", ")}`);return}V(!0);try{await a.setComponents(m.product_id,f,u.map((h,C)=>({component_product_id:h.component_product_id,component_variation_id:h.component_variation_id,quantity_per_bundle:h.quantity_per_bundle,component_kind:h.component_kind,notes:h.notes??null,sort_order:C})),L?F:void 0),Y.success(u.length===0?"Bundle cleared":"Saved"),await(P==null?void 0:P())}catch(h){Y.error(h.message)}finally{V(!1)}},_=async()=>{if(confirm("Clear all overrides for this variation?")){V(!0);try{await a.setComponents(m.product_id,f,[],F),Y.success("Cleared"),I([]),await(P==null?void 0:P())}catch(r){Y.error(r.message)}finally{V(!1)}}};return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between flex-wrap gap-2",children:[e.jsx("h4",{className:"text-sm font-medium text-white",children:L?"Variation composition":"Product-level composition"}),e.jsxs("div",{className:"flex items-center gap-2",children:[w!==null&&e.jsxs(G,{className:"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px] uppercase tracking-wider",children:["Bundle $",(w/100).toFixed(2)]}),e.jsxs(G,{className:y?"bg-red-600/20 text-red-400 border border-red-500/30 text-[10px] uppercase tracking-wider":"bg-gray-600/30 text-gray-300 border border-gray-500/30 text-[10px] uppercase tracking-wider",children:["Notional $",(j/100).toFixed(2),y?" (missing prices)":""]})]})]}),L&&e.jsxs("div",{className:"flex items-start gap-3 p-3 rounded bg-black/40 border border-white/5",children:[e.jsx(Ye,{className:Je,checked:F,onCheckedChange:N}),e.jsxs("div",{className:"flex-1",children:[e.jsx(le,{className:"text-gray-200 text-sm",children:"Inherit product-level components"}),e.jsx("p",{className:"text-[11px] text-gray-500 mt-0.5",children:F?"This variation starts with the product-level list. Add overrides or remove specific items below.":"This variation is standalone — only the components listed below apply."})]})]}),L&&F&&d.length>0&&e.jsxs("div",{className:"rounded border border-white/5 bg-black/30",children:[e.jsxs("div",{className:"px-3 py-2 border-b border-white/5 flex items-center justify-between",children:[e.jsx("span",{className:"text-[11px] uppercase tracking-wider text-gray-400",children:"Inherited from product"}),e.jsxs("span",{className:"text-[10px] text-gray-500",children:[d.length," item",d.length===1?"":"s"]})]}),e.jsx("div",{className:"divide-y divide-white/5",children:d.map(r=>{const n=O(r.component_product_id),h=E(r.component_product_id,r.component_variation_id);return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-2 px-3 py-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[(n==null?void 0:n.thumbnail)&&e.jsx("img",{src:n.thumbnail,alt:"",className:"h-5 w-5 rounded object-cover border border-white/10"}),e.jsx("span",{className:"text-sm text-gray-300",children:(n==null?void 0:n.name)??r.component_product_id}),e.jsxs("span",{className:"text-[10px] text-gray-500 font-mono",children:["×",r.quantity_per_bundle,h!==null&&` · $${h.toFixed(2)}`]})]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsxs(K,{variant:"ghost",size:"sm",className:pe,onClick:()=>D(r),children:[e.jsx(Qe,{className:"h-3.5 w-3.5 mr-1"})," Override"]}),e.jsxs(K,{variant:"ghost",size:"sm",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",onClick:()=>z(r),children:[e.jsx(Ge,{className:"h-3.5 w-3.5 mr-1"})," Remove"]})]})]},r.id)})})]}),L&&F&&u.length>0&&e.jsx("div",{className:"text-[11px] uppercase tracking-wider text-gray-400 pt-2",children:"Variation-specific overrides & additions"}),u.length===0&&!(L&&F&&d.length>0)?e.jsx("div",{className:"py-4 text-sm text-gray-400",children:L&&F?"No overrides yet — this variation matches the product-level composition.":"No components yet. Add the products this bundle is made of."}):u.length===0?null:u.map((r,n)=>
6{const h=O(r.component_product_id),C=E(r.component_product_id,r.component_variation_id),o=C===null?null:C*r.quantity_per_bundle,q=L&&F&&r.quantity_per_bundle===0,Z=o!==null&&o>0&&j>0?(Math.round(o*100)/j*100).toFixed(1):null;return e.jsxs("div",{className:`grid grid-cols-12 gap-2 items-end ${q?"opacity-60":""}`,children:[e.jsxs("div",{className:`col-span-12 md:col-span-6 space-y-2${n>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${n>0?" md:sr-only":""}`,children:"Component product"}),e.jsxs($e,{value:r.component_product_id,onValueChange:s=>I(u.map((c,p)=>p===n?{...c,component_product_id:s,component_variation_id:null,component_kind:void 0}:c)),children:[e.jsx(Be,{className:ae,children:e.jsx(Fe,{placeholder:"Select product"})}),e.jsx(Me,{className:"bg-[#14181B] border-white/10 text-white max-h-[24rem]",children:a.products.filter(s=>s.product_id!==m.product_id).map(s=>e.jsx(je,{value:s.product_id,children:e.jsxs("div",{className:"flex items-center gap-2",children:[s.thumbnail&&e.jsx("img",{src:s.thumbnail,alt:"",className:"h-5 w-5 rounded object-cover border border-white/10"}),e.jsx("span",{children:s.name||s.product_id}),s.price!==null&&e.jsxs("span",{className:"text-[10px] text-gray-500 font-mono",children:["$",s.price.toFixed(2)]})]})},s.product_id))})]})]}),e.jsxs("div",{className:`col-span-7 md:col-span-3 space-y-2${n>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${n>0?" md:sr-only":""}`,children:"Variation"}),e.jsxs($e,{value:r.component_variation_id??"",onValueChange:s=>I(u.map((c,p)=>p===n?{...c,component_variation_id:s||null}:c)),disabled:!h||h.variations.length===0,children:[e.jsx(Be,{className:ae,children:e.jsx(Fe,{placeholder:h&&h.variations.length>0?"Select variation":"—"})}),e.jsx(Me,{className:"bg-[#14181B] border-white/10 text-white",children:h==null?void 0:h.variations.map(s=>e.jsx(je,{value:s.variation_id,children:e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx("span",{children:s.name||s.variation_id}),s.price!==null&&e.jsxs("span",{className:"text-[10px] text-gray-500 font-mono",children:["$",s.price.toFixed(2)]})]})},s.variation_id))})]})]}),e.jsxs("div",{className:`col-span-3 md:col-span-2 space-y-2${n>0?" md:space-y-0":""}`,children:[e.jsx(le,{className:`${ne}${n>0?" md:sr-only":""}`,children:"Qty"}),e.jsx(te,{className:`${ae} font-mono text-right`,inputMode:"numeric",value:String(r.quantity_per_bundle),onChange:s=>{const c=parseInt(s.target.value||"0",10),p=Number.isFinite(c)?Math.max(0,c):0,l=Math.max(L&&F?0:1,p);I(u.map((M,S)=>S===n?{...M,quantity_per_bundle:l}:M))}})]}),e.jsx("div",{className:"col-span-2 md:col-span-1 flex justify-end",children:e.jsx(K,{variant:"ghost",size:"icon",className:"text-gray-400 hover:text-red-400",onClick:()=>I(u.filter((s,c)=>c!==n)),children:e.jsx(Ge,{className:"h-4 w-4"})})}),e.jsx("div",{className:"col-span-12 md:col-span-11 md:col-start-2 text-[11px] text-gray-500 -mt-1",children:q?e.jsx("span",{className:"text-amber-400",children:"Removes this inherited component from this variation"}):C===null?e.jsx("span",{className:"text-red-400",children:"no standalone price on file"}):e.jsxs(e.Fragment,{children:["$",C.toFixed(2)," × ",r.quantity_per_bundle," = $",(o??0).toFixed(2),Z!==null&&e.jsxs(e.Fragment,{children:[" · ",Z,"% of notional"]})]})})]},n)}),e.jsxs(K,{variant:"ghost",size:"sm",className:"text-illumigreen hover:text-illumigreen/80",onClick:()=>I([...u,{component_product_id:"",component_variation_id:null,quantity_per_bundle:1}]),children:[e.jsx(Te,{className:"h-4 w-4 mr-1"})," Add component"]}),w!==null&&j>0&&e.jsxs("p",{className:"text-[11px] text-gray-400",children:["Bundle is sold for $",(w/100).toFixed(2),"; components total $",(j/100).toFixed(2)," standalone."," ",w<j?e.jsxs("span",{className:"text-illumigreen",children:["Bundle discount of $",((j-w)/100).toFixed(2)," (",((j-w)/j*100).toFixed(1),"%)."]}):w>j?e.jsxs("span",{className:"text-red-400",children:["Bundle priced above sum of components by $",((w-j)/100).toFixed(2)," — check prices."]}):e.jsx("span",{children:"Bundle is sum-of-parts."})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("div",{className:"flex gap-2",children:A&&v.length>0&&e.jsx(K,{variant:"ghost",className:"text-red-400 hover:text-red-300 hover:bg-red-500/10",onClick:_,disabled:H,children:"Clear"})}),e.jsx(K,{className:ke,onClick:U,disabled:H,children:"Save"})]})]})}function Vt({title:a,description:m,payouts:f,resolved:v,componentSplitsFor:B,productName:b}){const P=v.filter(A=>B(A.component_product_id,A.component_variation_id).length===0);return e.jsxs("div",{className:"rounded border border-illumigreen/20 bg-illumigreen/5 p-3 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-illumigreen",children:a}),e.jsx("p",{className:"text-[11px] text-gray-300 mt-0.5 leading-relaxed",children:m})]}),f.length===0?e.jsx("p",{className:"text-xs text-gray-400",children:"Components have no splits yet — nothing will be paid out for this bundle."}):e.jsx("div",{className:"space-y-1",children:f.map(A=>e.jsx("div",{className:"flex items-start justify-between gap-3 p-2 rounded bg-black/40 border border-white/5",children:e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm text-white",children:A.payee_name}),e.jsxs(G,{className:"bg-illumigreen/20 text-illumigreen border border-illumigreen/30 text-[10px]",children:[ce(A.basis_points),"% of bundle"]})]}),e.jsx("div",{className:"text-[10px] text-gray-500 mt-1 leading-snug",children:A.breakdown.map(L=>L.source).join(" + ")})]})},A.payee_id))}),P.length>0&&e.jsxs("div",{className:"text-[11px] text-amber-300 border-t border-white/5 pt-2",children:[e.jsx("span",{className:"font-medium",children:"Missing splits on:"})," ",P.map(A=>
6b(A.component_product_id)).join(", ")]})]})}export{js 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.