@aifeatures/admin-react 0.5.5 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +4 -3
- package/dist/index.d.cts +419 -3
- package/dist/index.d.ts +419 -3
- package/dist/index.js +4 -3
- package/dist/styles.css +1 -1
- package/package.json +5 -2
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
"use strict";var Xr=Object.create;var ls=Object.defineProperty;var en=Object.getOwnPropertyDescriptor;var tn=Object.getOwnPropertyNames;var an=Object.getPrototypeOf,sn=Object.prototype.hasOwnProperty;var rn=(e,t)=>{for(var a in t)ls(e,a,{get:t[a],enumerable:!0})},Qs=(e,t,a,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of tn(t))!sn.call(e,r)&&r!==a&&ls(e,r,{get:()=>t[r],enumerable:!(s=en(t,r))||s.enumerable});return e};var te=(e,t,a)=>(a=e!=null?Xr(an(e)):{},Qs(t||!e||!e.__esModule?ls(a,"default",{value:e,enumerable:!0}):a,e)),nn=e=>Qs(ls({},"__esModule",{value:!0}),e);var li={};rn(li,{AifeaturesApiError:()=>Yt,AifeaturesProvider:()=>Xs,CustomerDetail:()=>es,CustomersList:()=>ts,FormSettings:()=>Wa,FormSubmissions:()=>Ha,FormsDashboard:()=>_s,FormsList:()=>Aa,LeadsView:()=>zs,OrderDetail:()=>Ya,OrderStatusBadge:()=>ua,OrdersList:()=>xa,OrdersView:()=>Vs,PaymentsDashboard:()=>Us,PaymentsSetup:()=>as,ProductSettings:()=>Ja,ProductsList:()=>Ga,ProductsView:()=>Hs,SubmissionDetail:()=>Ua,SubscriptionsView:()=>js,VariantsEditor:()=>Ka,ViewShell:()=>Bt,WebhookDeliveries:()=>ja,WebhookSettings:()=>qa,chargedTotal:()=>St,createApiClient:()=>fs,formatPrice:()=>M,isZeroDecimalCurrency:()=>Lt,toMajorUnits:()=>qe,toMinorUnits:()=>Pt,useAifeatures:()=>ee,useAifeaturesContext:()=>ee,useBusinessSummary:()=>ss,useCustomers:()=>Xa,useEventDestinations:()=>Ma,useForms:()=>va,useOrders:()=>Za,usePaymentsStatus:()=>wa,usePeople:()=>ns,usePerson:()=>is,useProducts:()=>Sa,useSubmissions:()=>Fa,useSubscriptions:()=>os});module.exports=nn(li);var la=te(require("react"),1);var Yt=class extends Error{constructor(a,s,r){super(a);this.status=s;this.details=r;this.name="AifeaturesApiError"}};function ks(e,t){if(typeof e=="string"&&e)return e;if(e&&typeof e=="object")try{return JSON.stringify(e)}catch{}return`Request failed with status ${t}`}function fs(e,t="https://aifeatures.dev"){async function a(s,r={}){let l=`${t}${s}`,c=await fetch(l,{...r,headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json",...r.headers}});if(!c.ok){let b;try{b=await c.json()}catch{}throw new Yt(ks(b?.error,c.status),c.status,b)}if(c.status!==204)return c.json()}return{async getForms(){return(await a("/api/v1/forms")).forms},async getForm(s){return a(`/api/v1/forms/${s}`)},async createForm(s){return a("/api/v1/forms",{method:"POST",body:JSON.stringify(s)})},async updateForm(s,r){return a(`/api/v1/forms/${s}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteForm(s){await a(`/api/v1/forms/${s}`,{method:"DELETE"})},async getSubmissions(s,r={}){let l=new URLSearchParams;r.limit!==void 0&&l.set("limit",String(r.limit)),r.offset!==void 0&&l.set("offset",String(r.offset));let c=l.toString(),b=`/api/v1/forms/${s}/submissions${c?`?${c}`:""}`;return a(b)},async getSubmission(s){return a(`/api/v1/submissions/${s}`)},async deleteSubmission(s){await a(`/api/v1/submissions/${s}`,{method:"DELETE"})},async downloadAttachment(s,r){let l=typeof r=="string"?{name:r}:r,c=l.name,b=l.id??l.name,u=`${t}/api/v1/submissions/${s}/attachments/${encodeURIComponent(b)}`,m=await fetch(u,{headers:{Authorization:`Bearer ${e}`}});if(!m.ok){let v;try{v=await m.json()}catch{}throw new Yt(ks(v?.error,m.status),m.status,v)}let n=await m.blob(),y=URL.createObjectURL(n),w=document.createElement("a");w.href=y,w.download=c,document.body.appendChild(w),w.click(),document.body.removeChild(w),URL.revokeObjectURL(y)},async getPaymentsStatus(){return a("/api/v1/payments/status")},async getProducts(s={}){let r=new URLSearchParams;s.include&&r.set("include",s.include);let l=r.toString();return(await a(`/api/v1/products${l?`?${l}`:""}`)).products},async getProduct(s){return a(`/api/v1/products/${s}`)},async createProduct(s){return a("/api/v1/products",{method:"POST",body:JSON.stringify(s)})},async updateProduct(s,r){return a(`/api/v1/products/${s}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteProduct(s){await a(`/api/v1/products/${s}`,{method:"DELETE"})},async attachProductFile(s,r){let l=new FormData;l.append("file",r);let c=await fetch(`${t}/api/v1/products/${s}/file`,{method:"POST",headers:{Authorization:`Bearer ${e}`},body:l});if(!c.ok){let b;try{b=await c.json()}catch{}throw new Yt(ks(b?.error,c.status),c.status,b)}return c.json()},async attachProductFileFromUrl(s,r){return a(`/api/v1/products/${s}/file`,{method:"POST",body:JSON.stringify({source_url:r})})},async createOption(s,r){return a(`/api/v1/products/${s}/options`,{method:"POST",body:JSON.stringify(r)})},async updateOption(s,r){return a(`/api/v1/options/${s}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteOption(s){await a(`/api/v1/options/${s}`,{method:"DELETE"})},async createVariant(s,r){return a(`/api/v1/products/${s}/variants`,{method:"POST",body:JSON.stringify(r)})},async updateVariant(s,r){return a(`/api/v1/variants/${s}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteVariant(s){await a(`/api/v1/variants/${s}`,{method:"DELETE"})},async setVariantInventory(s,r){return a(`/api/v1/variants/${s}/inventory`,{method:"PUT",body:JSON.stringify(r)})},async adjustVariantInventory(s,r){return a(`/api/v1/variants/${s}/inventory/adjust`,{method:"POST",body:JSON.stringify(r)})},async getCustomers(s={}){let r=new URLSearchParams;s.email&&r.set("email",s.email),s.limit!==void 0&&r.set("limit",String(s.limit)),s.offset!==void 0&&r.set("offset",String(s.offset));let l=r.toString();return a(`/api/v1/customers${l?`?${l}`:""}`)},async getCustomer(s){return a(`/api/v1/customers/${s}`)},async getOrders(s={}){let r=new URLSearchParams;s.status&&r.set("status",s.status),s.excludeStatus?.length&&r.set("exclude_status",s.excludeStatus.join(",")),s.customerEmail&&r.set("customer_email",s.customerEmail),s.from&&r.set("from",s.from),s.to&&r.set("to",s.to),s.unfulfilledOnly&&r.set("unfulfilled_only","true"),s.limit!==void 0&&r.set("limit",String(s.limit)),s.offset!==void 0&&r.set("offset",String(s.offset));let l=r.toString();return a(`/api/v1/orders${l?`?${l}`:""}`)},async getOrder(s){return a(`/api/v1/orders/${s}`)},async resendOrderReceipt(s){await a(`/api/v1/orders/${s}/resend-receipt`,{method:"POST"})},async resetOrderDownloads(s){return a(`/api/v1/orders/${s}/reset-downloads`,{method:"POST"})},async setOrderFulfillment(s,r){return a(`/api/v1/orders/${s}/fulfillment`,{method:"POST",body:JSON.stringify({fulfilled:r.fulfilled,...r.trackingNumber!==void 0?{tracking_number:r.trackingNumber}:{}})})},async getSubscriptions(s={}){let r=new URLSearchParams;s.status&&r.set("status",s.status),s.limit!==void 0&&r.set("limit",String(s.limit)),s.offset!==void 0&&r.set("offset",String(s.offset));let l=r.toString();return a(`/api/v1/subscriptions${l?`?${l}`:""}`)},async getBusinessSummary(s){let r=new URLSearchParams;return r.set("tz_offset_minutes",String(s.tzOffsetMinutes)),a(`/api/v1/summary?${r.toString()}`)},async getPeople(s={}){let r=new URLSearchParams;s.since&&r.set("since",s.since),s.q&&r.set("q",s.q),s.limit!==void 0&&r.set("limit",String(s.limit)),s.offset!==void 0&&r.set("offset",String(s.offset));let l=r.toString();return a(`/api/v1/people${l?`?${l}`:""}`)},async getPerson(s){return a(`/api/v1/people/${encodeURIComponent(s)}`)},async getEventDestinations(){return(await a("/api/v1/event-destinations")).destinations},async createEventDestination(s){return a("/api/v1/event-destinations",{method:"POST",body:JSON.stringify(s)})},async updateEventDestination(s,r){return a(`/api/v1/event-destinations/${s}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteEventDestination(s){await a(`/api/v1/event-destinations/${s}`,{method:"DELETE"})},async rotateEventDestinationSecret(s){return a(`/api/v1/event-destinations/${s}/rotate-secret`,{method:"POST"})},async getEventDestinationSecret(s){return(await a(`/api/v1/event-destinations/${s}/secret`)).secret},async getEventDeliveries(s,r){let l=r===void 0?"":`?limit=${r}`;return(await a(`/api/v1/event-destinations/${s}/deliveries${l}`)).deliveries},async retryEventDelivery(s,r){return a(`/api/v1/event-destinations/${s}/deliveries/${r}/retry`,{method:"POST"})}}}var oa=require("react/jsx-runtime"),Ys=la.createContext(null);function on(e){return e?e.startsWith("sk_")?"Invalid token type: You passed an organization API key (sk_xxx) but AifeaturesProvider requires a site token (st_xxx). Site tokens are returned when you create a site via the API.":e.startsWith("st_")?null:`Invalid token format: Expected a site token starting with "st_" but got "${e.slice(0,10)}...". Site tokens are returned when you create a site via the API.`:"No siteToken provided to AifeaturesProvider"}function Xs({siteToken:e,apiUrl:t="https://aifeatures.dev",dark:a=!1,className:s,children:r}){let l=la.useMemo(()=>on(e),[e]),c=la.useMemo(()=>fs(e,t),[e,t]),b=la.useMemo(()=>({siteToken:e,apiUrl:t,api:c}),[e,t,c]);return l?(0,oa.jsx)("div",{className:`aifeatures-admin ${a?"dark":""} ${s||""}`,children:(0,oa.jsxs)("div",{className:"af-rounded-md af-border af-border-destructive af-bg-destructive/10 af-p-4",children:[(0,oa.jsx)("h3",{className:"af-text-sm af-font-semibold af-text-destructive af-mb-2",children:"AifeaturesProvider Configuration Error"}),(0,oa.jsx)("p",{className:"af-text-sm af-text-destructive",children:l})]})}):(0,oa.jsx)(Ys.Provider,{value:b,children:(0,oa.jsx)("div",{className:`aifeatures-admin ${a?"dark":""} ${s||""}`,children:r})})}function ee(){let e=la.useContext(Ys);if(!e)throw new Error("useAifeaturesContext must be used within an AifeaturesProvider");return e}var da=te(require("react"),1),ka=require("lucide-react");var cs=te(require("react"),1),Ct=te(require("@radix-ui/react-tabs"),1);var er=require("clsx"),tr=require("tailwind-merge"),ln=(0,tr.extendTailwindMerge)({prefix:"af-"});function V(...e){return ln((0,er.clsx)(e))}var us=require("react/jsx-runtime"),ds=Ct.Root,Ba=cs.forwardRef(({className:e,...t},a)=>(0,us.jsx)(Ct.List,{ref:a,className:V("af-inline-flex af-h-9 af-items-center af-justify-center af-rounded-lg af-bg-muted af-p-1 af-text-muted-foreground",e),...t}));Ba.displayName=Ct.List.displayName;var Xt=cs.forwardRef(({className:e,...t},a)=>(0,us.jsx)(Ct.Trigger,{ref:a,className:V("af-inline-flex af-items-center af-justify-center af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-ring-offset-background af-transition-all focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring focus-visible:af-ring-offset-2 disabled:af-pointer-events-none disabled:af-opacity-50 data-[state=active]:af-bg-background data-[state=active]:af-text-foreground data-[state=active]:af-shadow",e),...t}));Xt.displayName=Ct.Trigger.displayName;var ea=cs.forwardRef(({className:e,...t},a)=>(0,us.jsx)(Ct.Content,{ref:a,className:V("af-mt-2 af-ring-offset-background focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring focus-visible:af-ring-offset-2",e),...t}));ea.displayName=Ct.Content.displayName;var ar=te(require("react"),1),sr=require("@radix-ui/react-slot"),rr=require("class-variance-authority");var nr=require("react/jsx-runtime"),fn=(0,rr.cva)("af-inline-flex af-items-center af-justify-center af-gap-2 af-whitespace-nowrap af-rounded-md af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring disabled:af-pointer-events-none disabled:af-opacity-50 [&_svg]:af-pointer-events-none [&_svg]:af-size-4 [&_svg]:af-shrink-0",{variants:{variant:{default:"af-bg-primary af-text-primary-foreground af-shadow hover:af-bg-primary/90",destructive:"af-bg-destructive af-text-destructive-foreground af-shadow-sm hover:af-bg-destructive/90",outline:"af-border af-border-input af-bg-background af-shadow-sm hover:af-bg-accent hover:af-text-accent-foreground",secondary:"af-bg-secondary af-text-secondary-foreground af-shadow-sm hover:af-bg-secondary/80",ghost:"hover:af-bg-accent hover:af-text-accent-foreground",link:"af-text-primary af-underline-offset-4 hover:af-underline"},size:{default:"af-h-9 af-px-4 af-py-2",sm:"af-h-8 af-rounded-md af-px-3 af-text-xs",lg:"af-h-10 af-rounded-md af-px-8",icon:"af-h-9 af-w-9"}},defaultVariants:{variant:"default",size:"default"}}),N=ar.forwardRef(({className:e,variant:t,size:a,asChild:s=!1,...r},l)=>(0,nr.jsx)(s?sr.Slot:"button",{className:V(fn({variant:t,size:a,className:e})),ref:l,...r}));N.displayName="Button";var Ia=require("lucide-react");var ta=te(require("react"),1);var $t=require("react/jsx-runtime"),Re=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("div",{className:"af-relative af-w-full af-overflow-auto",children:(0,$t.jsx)("table",{ref:a,className:V("af-w-full af-caption-bottom af-text-sm",e),...t})}));Re.displayName="Table";var Ce=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("thead",{ref:a,className:V("[&_tr]:af-border-b",e),...t}));Ce.displayName="TableHeader";var Te=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("tbody",{ref:a,className:V("[&_tr:last-child]:af-border-0",e),...t}));Te.displayName="TableBody";var cn=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("tfoot",{ref:a,className:V("af-border-t af-bg-muted/50 af-font-medium [&>tr]:last:af-border-b-0",e),...t}));cn.displayName="TableFooter";var ie=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("tr",{ref:a,className:V("af-border-b af-transition-colors hover:af-bg-muted/50 data-[state=selected]:af-bg-muted",e),...t}));ie.displayName="TableRow";var L=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("th",{ref:a,className:V("af-h-10 af-px-2 af-text-left af-align-middle af-font-medium af-text-muted-foreground [&:has([role=checkbox])]:af-pr-0 [&>[role=checkbox]]:af-translate-y-[2px]",e),...t}));L.displayName="TableHead";var B=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("td",{ref:a,className:V("af-p-2 af-align-middle [&:has([role=checkbox])]:af-pr-0 [&>[role=checkbox]]:af-translate-y-[2px]",e),...t}));B.displayName="TableCell";var dn=ta.forwardRef(({className:e,...t},a)=>(0,$t.jsx)("caption",{ref:a,className:V("af-mt-4 af-text-sm af-text-muted-foreground",e),...t}));dn.displayName="TableCaption";var Tt=te(require("react"),1);function va(){let{api:e}=ee(),[t,a]=Tt.useState([]),[s,r]=Tt.useState(!0),[l,c]=Tt.useState(null),b=Tt.useCallback(async()=>{try{r(!0),c(null);let y=await e.getForms();a(y)}catch(y){c(y instanceof Error?y:new Error("Failed to fetch forms"))}finally{r(!1)}},[e]);Tt.useEffect(()=>{b()},[b]);let u=Tt.useCallback(async y=>{let w=await e.createForm(y);return a(v=>[...v,w]),w},[e]),m=Tt.useCallback(async(y,w)=>{let v=await e.updateForm(y,w);return a(f=>f.map(p=>p.id===y?v:p)),v},[e]),n=Tt.useCallback(async y=>{await e.deleteForm(y),a(w=>w.filter(v=>v.id!==y))},[e]);return{forms:t,isLoading:s,error:l,refetch:b,createForm:u,updateForm:m,deleteForm:n}}var ye=require("react/jsx-runtime");function Aa({onSelectForm:e,className:t}){let{forms:a,isLoading:s,error:r}=va();return s?(0,ye.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,ye.jsx)("div",{className:"af-text-muted-foreground",children:"Loading forms..."})}):r?(0,ye.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,ye.jsxs)("div",{className:"af-text-destructive",children:["Error: ",r.message]})}):a.length===0?(0,ye.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${t||""}`,children:[(0,ye.jsx)(Ia.Inbox,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,ye.jsxs)("p",{className:"af-text-muted-foreground af-text-center",children:["No forms yet.",(0,ye.jsx)("br",{}),"Forms will appear here when the AI creates contact forms on your website."]})]}):(0,ye.jsx)("div",{className:t,children:(0,ye.jsxs)(Re,{children:[(0,ye.jsx)(Ce,{children:(0,ye.jsxs)(ie,{children:[(0,ye.jsx)(L,{children:"Name"}),(0,ye.jsx)(L,{children:"Endpoint"}),(0,ye.jsx)(L,{className:"af-text-right",children:"Actions"})]})}),(0,ye.jsx)(Te,{children:a.map(l=>(0,ye.jsxs)(ie,{children:[(0,ye.jsx)(B,{className:"af-font-medium",children:l.name}),(0,ye.jsx)(B,{children:(0,ye.jsx)("code",{className:"af-text-xs af-bg-muted af-px-1.5 af-py-0.5 af-rounded",children:l.endpoint_url.replace("https://aifeatures.dev","")})}),(0,ye.jsx)(B,{className:"af-text-right",children:(0,ye.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-2",children:[(0,ye.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>e?.(l,"submissions"),children:[(0,ye.jsx)(Ia.Inbox,{className:"af-h-4 af-w-4 af-mr-1"}),"Submissions"]}),(0,ye.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>e?.(l,"settings"),children:[(0,ye.jsx)(Ia.Settings,{className:"af-h-4 af-w-4 af-mr-1"}),"Settings"]})]})})]},l.id))})]})})}var Va=te(require("react"),1),Mt=require("lucide-react");var fa=te(require("react"),1),ce=te(require("@radix-ui/react-select"),1),ba=require("lucide-react");var Ae=require("react/jsx-runtime"),ht=ce.Root;var xt=ce.Value,ot=fa.forwardRef(({className:e,children:t,...a},s)=>(0,Ae.jsxs)(ce.Trigger,{ref:s,className:V("af-flex af-h-9 af-w-full af-items-center af-justify-between af-whitespace-nowrap af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-2 af-text-sm af-shadow-sm af-ring-offset-background placeholder:af-text-muted-foreground focus:af-outline-none focus:af-ring-1 focus:af-ring-ring disabled:af-cursor-not-allowed disabled:af-opacity-50 [&>span]:af-line-clamp-1",e),...a,children:[t,(0,Ae.jsx)(ce.Icon,{asChild:!0,children:(0,Ae.jsx)(ba.ChevronDown,{className:"af-h-4 af-w-4 af-opacity-50"})})]}));ot.displayName=ce.Trigger.displayName;var ir=fa.forwardRef(({className:e,...t},a)=>(0,Ae.jsx)(ce.ScrollUpButton,{ref:a,className:V("af-flex af-cursor-default af-items-center af-justify-center af-py-1",e),...t,children:(0,Ae.jsx)(ba.ChevronUp,{className:"af-h-4 af-w-4"})}));ir.displayName=ce.ScrollUpButton.displayName;var or=fa.forwardRef(({className:e,...t},a)=>(0,Ae.jsx)(ce.ScrollDownButton,{ref:a,className:V("af-flex af-cursor-default af-items-center af-justify-center af-py-1",e),...t,children:(0,Ae.jsx)(ba.ChevronDown,{className:"af-h-4 af-w-4"})}));or.displayName=ce.ScrollDownButton.displayName;var lt=fa.forwardRef(({className:e,children:t,position:a="popper",...s},r)=>(0,Ae.jsx)(ce.Portal,{children:(0,Ae.jsxs)(ce.Content,{ref:r,className:V("af-relative af-z-50 af-max-h-96 af-min-w-[8rem] af-overflow-hidden af-rounded-md af-border af-bg-popover af-text-popover-foreground af-shadow-md data-[state=open]:af-animate-in data-[state=closed]:af-animate-out data-[state=closed]:af-fade-out-0 data-[state=open]:af-fade-in-0 data-[state=closed]:af-zoom-out-95 data-[state=open]:af-zoom-in-95 data-[side=bottom]:af-slide-in-from-top-2 data-[side=left]:af-slide-in-from-right-2 data-[side=right]:af-slide-in-from-left-2 data-[side=top]:af-slide-in-from-bottom-2",a==="popper"&&"data-[side=bottom]:af-translate-y-1 data-[side=left]:af--translate-x-1 data-[side=right]:af-translate-x-1 data-[side=top]:af--translate-y-1",e),position:a,...s,children:[(0,Ae.jsx)(ir,{}),(0,Ae.jsx)(ce.Viewport,{className:V("af-p-1",a==="popper"&&"af-h-[var(--radix-select-trigger-height)] af-w-full af-min-w-[var(--radix-select-trigger-width)]"),children:t}),(0,Ae.jsx)(or,{})]})}));lt.displayName=ce.Content.displayName;var un=fa.forwardRef(({className:e,...t},a)=>(0,Ae.jsx)(ce.Label,{ref:a,className:V("af-px-2 af-py-1.5 af-text-sm af-font-semibold",e),...t}));un.displayName=ce.Label.displayName;var ke=fa.forwardRef(({className:e,children:t,...a},s)=>(0,Ae.jsxs)(ce.Item,{ref:s,className:V("af-relative af-flex af-w-full af-cursor-default af-select-none af-items-center af-rounded-sm af-py-1.5 af-pl-2 af-pr-8 af-text-sm af-outline-none focus:af-bg-accent focus:af-text-accent-foreground data-[disabled]:af-pointer-events-none data-[disabled]:af-opacity-50",e),...a,children:[(0,Ae.jsx)("span",{className:"af-absolute af-right-2 af-flex af-h-3.5 af-w-3.5 af-items-center af-justify-center",children:(0,Ae.jsx)(ce.ItemIndicator,{children:(0,Ae.jsx)(ba.Check,{className:"af-h-4 af-w-4"})})}),(0,Ae.jsx)(ce.ItemText,{children:t})]}));ke.displayName=ce.Item.displayName;var mn=fa.forwardRef(({className:e,...t},a)=>(0,Ae.jsx)(ce.Separator,{ref:a,className:V("af--mx-1 af-my-1 af-h-px af-bg-muted",e),...t}));mn.displayName=ce.Separator.displayName;var Nt=te(require("react"),1);function Fa({formId:e,initialPageSize:t=25}){let{api:a}=ee(),[s,r]=Nt.useState([]),[l,c]=Nt.useState(0),[b,u]=Nt.useState(!0),[m,n]=Nt.useState(null),[y,w]=Nt.useState(0),[v,f]=Nt.useState(t),p=Nt.useCallback(async()=>{if(e)try{u(!0),n(null);let T={limit:v,offset:y*v},U=await a.getSubmissions(e,T);r(U.submissions),c(U.total)}catch(T){n(T instanceof Error?T:new Error("Failed to fetch submissions"))}finally{u(!1)}},[a,e,y,v]);Nt.useEffect(()=>{p()},[p]);let h=Nt.useCallback(async T=>{await a.deleteSubmission(T),r(U=>U.filter(W=>W.id!==T)),c(U=>U-1)},[a]),S=Math.ceil(l/v);return{submissions:s,total:l,isLoading:b,error:m,page:y,pageSize:v,hasNextPage:y<S-1,hasPreviousPage:y>0,setPage:w,setPageSize:T=>{f(T),w(0)},refetch:p,deleteSubmission:h}}var fr=te(require("react"),1),Ye=require("lucide-react");var se=require("react/jsx-runtime");function lr(e){return new Date(e).toLocaleString(void 0,{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function pn(e){if(e===0)return"0 B";let t=1024,a=["B","KB","MB","GB"],s=Math.floor(Math.log(e)/Math.log(t));return`${parseFloat((e/Math.pow(t,s)).toFixed(1))} ${a[s]}`}function Ua({submission:e,onBack:t}){let{api:a}=ee(),[s,r]=fr.useState(null),l=u=>u.id??u.name,c=async u=>{r(l(u));try{await a.downloadAttachment(e.id,u)}catch(m){console.error("Download failed:",m)}finally{r(null)}},b=Object.entries(e.data).filter(([u])=>!u.startsWith("_"));return(0,se.jsxs)("div",{children:[(0,se.jsxs)("div",{className:"af-mb-6",children:[(0,se.jsxs)(N,{variant:"ghost",size:"sm",onClick:t,className:"af-mb-2 af--ml-2",children:[(0,se.jsx)(Ye.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Submissions"]}),(0,se.jsx)("h3",{className:"af-text-lg af-font-semibold",children:"Submission Details"}),(0,se.jsxs)("p",{className:"af-text-sm af-text-muted-foreground",children:["Submitted ",lr(e.metadata.submitted_at)]})]}),(0,se.jsxs)("div",{className:"af-space-y-6",children:[(0,se.jsxs)("div",{children:[(0,se.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Form Data"}),(0,se.jsxs)("div",{className:"af-space-y-3",children:[b.map(([u,m])=>(0,se.jsxs)("div",{className:"af-grid af-grid-cols-3 af-gap-2",children:[(0,se.jsx)("div",{className:"af-text-sm af-text-muted-foreground af-capitalize",children:u.replace(/_/g," ")}),(0,se.jsx)("div",{className:"af-col-span-2 af-text-sm af-break-words",children:typeof m=="string"&&m.includes(`
|
|
3
|
-
`)?(0,se.jsx)("pre",{className:"af-whitespace-pre-wrap af-font-sans af-bg-muted af-p-2 af-rounded af-text-xs",children:m}):String(m)})]},u)),b.length===0&&(0,se.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No data submitted"})]})]}),e.attachments.length>0&&(0,se.jsxs)("div",{children:[(0,se.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Attachments"}),(0,se.jsx)("div",{className:"af-space-y-2",children:e.attachments.map((u,m)=>(0,se.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-p-2 af-bg-muted af-rounded",children:[(0,se.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,se.jsx)(Ye.FileText,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,se.jsx)("span",{className:"af-text-sm",children:u.name}),(0,se.jsxs)("span",{className:"af-text-xs af-text-muted-foreground",children:["(",pn(u.size),")"]})]}),(0,se.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>c(u),disabled:s===l(u),children:[s===l(u)?(0,se.jsx)(Ye.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,se.jsx)(Ye.Download,{className:"af-h-4 af-w-4 af-mr-1"}),s===l(u)?"Downloading...":"Download"]})]},m))})]}),(0,se.jsxs)("div",{children:[(0,se.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Metadata"}),(0,se.jsxs)("div",{className:"af-space-y-2 af-text-sm",children:[(0,se.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,se.jsx)(Ye.Clock,{className:"af-h-4 af-w-4"}),(0,se.jsx)("span",{children:lr(e.metadata.submitted_at)})]}),e.metadata.ip_address&&(0,se.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,se.jsx)(Ye.Globe,{className:"af-h-4 af-w-4"}),(0,se.jsxs)("span",{children:["IP: ",e.metadata.ip_address]})]}),e.metadata.user_agent&&(0,se.jsxs)("div",{className:"af-flex af-items-start af-gap-2 af-text-muted-foreground",children:[(0,se.jsx)(Ye.Monitor,{className:"af-h-4 af-w-4 af-mt-0.5"}),(0,se.jsx)("span",{className:"af-text-xs af-break-all",children:e.metadata.user_agent})]}),e.resend_id&&(0,se.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,se.jsx)(Ye.Mail,{className:"af-h-4 af-w-4"}),(0,se.jsxs)("span",{children:["Email ID: ",e.resend_id]})]})]})]})]})]})}var Z=require("react/jsx-runtime");function gn(e){return new Date(e).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function vn(e){let t=["email","Email","EMAIL","e-mail","mail"];for(let a of t)if(typeof e[a]=="string")return e[a];return null}function bn(e){let t=Object.entries(e).filter(([r])=>!r.startsWith("_")&&!r.toLowerCase().includes("email"));if(t.length===0)return"";let[,a]=t[0],s=String(a);return s.length>50?s.slice(0,50)+"...":s}function Ha({formId:e,className:t,defaultSubmissionId:a}){let{submissions:s,total:r,isLoading:l,error:c,page:b,pageSize:u,hasNextPage:m,hasPreviousPage:n,setPage:y,setPageSize:w}=Fa({formId:e}),[v,f]=Va.useState("list"),[p,h]=Va.useState(null);Va.useEffect(()=>{if(a&&s.length>0){let D=s.find(H=>H.id===a);D&&(h(D),f("detail"))}},[a,s]);let S=D=>{h(D),f("detail")},T=()=>{f("list"),h(null)};if(v==="detail"&&p)return(0,Z.jsx)("div",{className:t,children:(0,Z.jsx)(Ua,{submission:p,onBack:T})});if(l)return(0,Z.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,Z.jsx)("div",{className:"af-text-muted-foreground",children:"Loading submissions..."})});if(c)return(0,Z.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,Z.jsxs)("div",{className:"af-text-destructive",children:["Error: ",c.message]})});if(s.length===0&&b===0)return(0,Z.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${t||""}`,children:[(0,Z.jsx)(Mt.Inbox,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,Z.jsxs)("p",{className:"af-text-muted-foreground af-text-center",children:["No submissions yet.",(0,Z.jsx)("br",{}),"Submissions will appear here when visitors use your form."]})]});let U=b*u+1,W=Math.min((b+1)*u,r);return(0,Z.jsxs)("div",{className:t,children:[(0,Z.jsxs)(Re,{children:[(0,Z.jsx)(Ce,{children:(0,Z.jsxs)(ie,{children:[(0,Z.jsx)(L,{children:"Email"}),(0,Z.jsx)(L,{children:"Preview"}),(0,Z.jsx)(L,{children:"Date"}),(0,Z.jsx)(L,{className:"af-text-right",children:"Actions"})]})}),(0,Z.jsx)(Te,{children:s.map(D=>{let H=vn(D.data),z=bn(D.data);return(0,Z.jsxs)(ie,{children:[(0,Z.jsx)(B,{className:"af-font-medium",children:(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[H||(0,Z.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"}),D.attachments.length>0&&(0,Z.jsxs)("span",{className:"af-flex af-items-center af-gap-1 af-text-muted-foreground",title:`${D.attachments.length} attachment(s)`,children:[(0,Z.jsx)(Mt.Paperclip,{className:"af-h-3 af-w-3"}),(0,Z.jsx)("span",{className:"af-text-xs",children:D.attachments.length})]})]})}),(0,Z.jsx)(B,{className:"af-text-muted-foreground af-max-w-[200px] af-truncate",children:z||(0,Z.jsx)("span",{className:"af-italic",children:"No preview available"})}),(0,Z.jsx)(B,{className:"af-text-muted-foreground",children:gn(D.metadata.submitted_at)}),(0,Z.jsx)(B,{className:"af-text-right",children:(0,Z.jsx)(N,{variant:"ghost",size:"icon",onClick:()=>S(D),title:"View details",children:(0,Z.jsx)(Mt.Eye,{className:"af-h-4 af-w-4"})})})]},D.id)})})]}),(0,Z.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-px-2 af-py-4",children:[(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,Z.jsxs)("span",{children:["Showing ",U,"-",W," of ",r]}),(0,Z.jsxs)(ht,{value:String(u),onValueChange:D=>w(Number(D)),children:[(0,Z.jsx)(ot,{className:"af-w-[70px] af-h-8",children:(0,Z.jsx)(xt,{})}),(0,Z.jsxs)(lt,{children:[(0,Z.jsx)(ke,{value:"10",children:"10"}),(0,Z.jsx)(ke,{value:"25",children:"25"}),(0,Z.jsx)(ke,{value:"50",children:"50"}),(0,Z.jsx)(ke,{value:"100",children:"100"})]})]}),(0,Z.jsx)("span",{children:"per page"})]}),(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,Z.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>y(b-1),disabled:!n,children:[(0,Z.jsx)(Mt.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,Z.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>y(b+1),disabled:!m,children:["Next",(0,Z.jsx)(Mt.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var ct=te(require("react"),1),ca=require("lucide-react");var cr=te(require("react"),1);var dr=require("react/jsx-runtime"),fe=cr.forwardRef(({className:e,type:t,...a},s)=>(0,dr.jsx)("input",{type:t,className:V("af-flex af-h-9 af-w-full af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-1 af-text-sm af-shadow-sm af-transition-colors file:af-border-0 file:af-bg-transparent file:af-text-sm file:af-font-medium file:af-text-foreground placeholder:af-text-muted-foreground focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring disabled:af-cursor-not-allowed disabled:af-opacity-50",e),ref:s,...a}));fe.displayName="Input";var ur=te(require("react"),1),mr=require("class-variance-authority");var pr=require("react/jsx-runtime"),yn=(0,mr.cva)("af-text-sm af-font-medium af-leading-none peer-disabled:af-cursor-not-allowed peer-disabled:af-opacity-70"),le=ur.forwardRef(({className:e,...t},a)=>(0,pr.jsx)("label",{ref:a,className:V(yn(),e),...t}));le.displayName="Label";var gr=require("class-variance-authority");var vr=require("react/jsx-runtime"),hn=(0,gr.cva)("af-inline-flex af-items-center af-rounded-md af-border af-px-2.5 af-py-0.5 af-text-xs af-font-semibold af-transition-colors focus:af-outline-none focus:af-ring-2 focus:af-ring-ring focus:af-ring-offset-2",{variants:{variant:{default:"af-border-transparent af-bg-primary af-text-primary-foreground af-shadow hover:af-bg-primary/80",secondary:"af-border-transparent af-bg-secondary af-text-secondary-foreground hover:af-bg-secondary/80",destructive:"af-border-transparent af-bg-destructive af-text-destructive-foreground af-shadow hover:af-bg-destructive/80",outline:"af-text-foreground",success:"af-border-transparent af-bg-green-100 af-text-green-800",warning:"af-border-transparent af-bg-yellow-100 af-text-yellow-800"}},defaultVariants:{variant:"default"}});function $({className:e,variant:t,...a}){return(0,vr.jsx)("div",{className:V(hn({variant:t}),e),...a})}var wt=te(require("react"),1),Xe=require("lucide-react");var $a=te(require("react"),1),$e=te(require("@radix-ui/react-dialog"),1),br=require("lucide-react");var rt=require("react/jsx-runtime"),zt=$e.Root;var xn=$e.Portal;var yr=$a.forwardRef(({className:e,...t},a)=>(0,rt.jsx)($e.Overlay,{ref:a,className:V("aifeatures-dialog-overlay af-fixed af-inset-0 af-z-50 af-bg-black/80",e),...t}));yr.displayName=$e.Overlay.displayName;var kt=$a.forwardRef(({className:e,children:t,...a},s)=>(0,rt.jsx)(xn,{children:(0,rt.jsxs)("div",{className:"aifeatures-admin",children:[(0,rt.jsx)(yr,{}),(0,rt.jsxs)($e.Content,{ref:s,className:V("aifeatures-dialog-content af-fixed af-left-[50%] af-top-[50%] af-z-50 af-grid af-w-full af-max-w-lg af-translate-x-[-50%] af-translate-y-[-50%] af-gap-4 af-border af-bg-background af-p-6 af-shadow-lg sm:af-rounded-lg",e),...a,children:[t,(0,rt.jsxs)($e.Close,{className:"af-absolute af-right-4 af-top-4 af-rounded-sm af-opacity-70 af-ring-offset-background af-transition-opacity hover:af-opacity-100 focus:af-outline-none focus:af-ring-2 focus:af-ring-ring focus:af-ring-offset-2 disabled:af-pointer-events-none data-[state=open]:af-bg-accent data-[state=open]:af-text-muted-foreground",children:[(0,rt.jsx)(br.X,{className:"af-h-4 af-w-4"}),(0,rt.jsx)("span",{className:"af-sr-only",children:"Close"})]})]})]})}));kt.displayName=$e.Content.displayName;var _t=({className:e,...t})=>(0,rt.jsx)("div",{className:V("af-flex af-flex-col af-space-y-1.5 af-text-center sm:af-text-left",e),...t});_t.displayName="DialogHeader";var Dt=({className:e,...t})=>(0,rt.jsx)("div",{className:V("af-flex af-flex-col-reverse sm:af-flex-row sm:af-justify-end sm:af-space-x-2",e),...t});Dt.displayName="DialogFooter";var Et=$a.forwardRef(({className:e,...t},a)=>(0,rt.jsx)($e.Title,{ref:a,className:V("af-text-lg af-font-semibold af-leading-none af-tracking-tight",e),...t}));Et.displayName=$e.Title.displayName;var Ot=$a.forwardRef(({className:e,...t},a)=>(0,rt.jsx)($e.Description,{ref:a,className:V("af-text-sm af-text-muted-foreground",e),...t}));Ot.displayName=$e.Description.displayName;var ft=te(require("react"),1);function Ma(){let{api:e}=ee(),[t,a]=ft.useState([]),[s,r]=ft.useState(!0),[l,c]=ft.useState(null),b=ft.useCallback(async()=>{try{r(!0),c(null),a(await e.getEventDestinations())}catch(v){c(v instanceof Error?v:new Error("Failed to fetch webhooks"))}finally{r(!1)}},[e]);ft.useEffect(()=>{b()},[b]);let u=ft.useCallback(async v=>{let f=await e.createEventDestination(v);return a(p=>[f,...p]),f},[e]),m=ft.useCallback(async(v,f)=>{let p=await e.updateEventDestination(v,f);return a(h=>h.map(S=>S.id===v?p:S)),p},[e]),n=ft.useCallback(async v=>{await e.deleteEventDestination(v),a(f=>f.filter(p=>p.id!==v))},[e]),y=ft.useCallback(async v=>{let f=await e.rotateEventDestinationSecret(v);return a(p=>p.map(h=>h.id===v?f:h)),f},[e]),w=ft.useCallback(v=>e.getEventDestinationSecret(v),[e]);return{destinations:t,isLoading:s,error:l,refetch:b,createDestination:u,updateDestination:m,deleteDestination:n,rotateSecret:y,getSecret:w}}var aa=te(require("react"),1),za=require("lucide-react");var Me=require("react/jsx-runtime");function ja({destinationId:e,limit:t=25,className:a}){let{api:s}=ee(),[r,l]=aa.useState([]),[c,b]=aa.useState(!0),[u,m]=aa.useState(null),[n,y]=aa.useState(null),w=aa.useCallback(async()=>{try{b(!0),m(null),l(await s.getEventDeliveries(e,t))}catch(f){m(f instanceof Error?f.message:"Failed to load deliveries")}finally{b(!1)}},[s,e,t]);aa.useEffect(()=>{w()},[w]);let v=async f=>{try{y(f.id),m(null);let p=await s.retryEventDelivery(e,f.id);l(h=>h.map(S=>S.id===f.id?p:S))}catch(p){m(p instanceof Error?p.message:"Retry failed")}finally{y(null)}};return c?(0,Me.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground af-py-2",children:[(0,Me.jsx)(za.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}),"Loading deliveries..."]}):u?(0,Me.jsx)("div",{className:"af-text-sm af-text-destructive af-py-2",children:u}):r.length===0?(0,Me.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-py-2",children:"Nothing sent yet. Deliveries appear here as submissions come in."}):(0,Me.jsx)("div",{className:a,children:(0,Me.jsx)("div",{className:"af-divide-y af-border af-rounded",children:r.map(f=>{let p=f.delivered_at!==null;return(0,Me.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-3 af-px-3 af-py-2",children:[(0,Me.jsxs)("div",{className:"af-min-w-0",children:[(0,Me.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[p?(0,Me.jsx)($,{variant:"secondary",children:f.last_status??200}):(0,Me.jsx)($,{variant:"destructive",children:f.last_status??"failed"}),(0,Me.jsx)("span",{className:"af-text-sm af-truncate",children:f.type})]}),(0,Me.jsxs)("p",{className:"af-text-xs af-text-muted-foreground af-mt-0.5 af-font-mono af-truncate",children:[f.event_id," \xB7 ",f.created_at,f.attempts>0&&` \xB7 ${f.attempts} failed ${f.attempts===1?"attempt":"attempts"}`]}),!p&&f.last_error&&(0,Me.jsx)("p",{className:"af-text-xs af-text-destructive af-mt-0.5 af-truncate",children:f.last_error})]}),(0,Me.jsx)(N,{variant:"ghost",size:"sm",title:"Send this event again",disabled:n===f.id,onClick:()=>v(f),children:n===f.id?(0,Me.jsx)(za.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,Me.jsx)(za.RotateCw,{className:"af-h-4 af-w-4"})})]},f.id)})})})}var G=require("react/jsx-runtime");function qa({formId:e,topics:t,description:a,className:s}){let{destinations:r,isLoading:l,error:c,createDestination:b,updateDestination:u,deleteDestination:m,rotateSecret:n,getSecret:y}=Ma(),[w,v]=wt.useState(""),[f,p]=wt.useState(!1),[h,S]=wt.useState(null),[T,U]=wt.useState(null),[W,D]=wt.useState(null),[H,z]=wt.useState(!1),[Q,j]=wt.useState(null),[A,de]=wt.useState(null),[x,oe]=wt.useState(null),we=wt.useMemo(()=>r.filter(F=>F.form_id===e),[r,e]),he=async()=>{let F=w.trim();if(F){if(!F.startsWith("https://")){S("Webhook URLs must start with https://");return}try{p(!0),S(null),await b({url:F,form_id:e,...t&&t.length>0?{topics:t}:{}}),v("")}catch(be){S(be instanceof Error?be.message:"Failed to add webhook")}finally{p(!1)}}},ge=async F=>{try{de(F.id),S(null),await n(F.id),z(!1),D(await y(F.id))}catch(be){S(be instanceof Error?be.message:"Failed to rotate secret")}finally{de(null)}},Le=async F=>{try{de(F.id),S(null),await u(F.id,{status:"active"})}catch(be){S(be instanceof Error?be.message:"Failed to re-enable")}finally{de(null)}},ae=async()=>{if(Q)try{de(Q.id),await m(Q.id),j(null)}catch(F){S(F instanceof Error?F.message:"Failed to delete")}finally{de(null)}},ve=async F=>{if(T===F.id){U(null),D(null);return}try{de(F.id),S(null),z(!1),U(F.id),D(await y(F.id))}catch(be){U(null),S(be instanceof Error?be.message:"Failed to read secret")}finally{de(null)}},_e=async()=>{if(W)try{await navigator.clipboard.writeText(W),z(!0)}catch{}};return(0,G.jsxs)("div",{className:s,children:[(0,G.jsxs)("div",{className:"af-space-y-1",children:[(0,G.jsx)(le,{children:"Webhooks"}),(0,G.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:a??"Send this form's submissions to your own system as they arrive \u2014 a Zapier or Make hook, a CRM, or your own endpoint."})]}),(0,G.jsxs)("div",{className:"af-flex af-gap-2 af-mt-4",children:[(0,G.jsx)(fe,{value:w,placeholder:"https://hooks.zapier.com/...",onChange:F=>v(F.target.value),onKeyDown:F=>{F.key==="Enter"&&(F.preventDefault(),he())},disabled:f}),(0,G.jsxs)(N,{onClick:he,disabled:f||!w.trim(),children:[f?(0,G.jsx)(Xe.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,G.jsx)(Xe.Plus,{className:"af-h-4 af-w-4"}),(0,G.jsx)("span",{className:"af-ml-1",children:"Add"})]})]}),(h||c)&&(0,G.jsx)("div",{className:"af-mt-2 af-text-sm af-text-destructive",children:h||c?.message}),(0,G.jsxs)("div",{className:"af-mt-4 af-space-y-2",children:[l&&(0,G.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,G.jsx)(Xe.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}),"Loading webhooks..."]}),!l&&we.length===0&&(0,G.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No webhooks yet. Submissions still arrive by email and in your dashboard."}),we.map(F=>(0,G.jsxs)("div",{className:"af-rounded af-border af-p-3 af-space-y-2",children:[(0,G.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-3",children:[(0,G.jsxs)("div",{className:"af-min-w-0",children:[(0,G.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,G.jsx)("span",{className:"af-font-mono af-text-sm af-truncate",children:F.url}),F.status==="disabled"?(0,G.jsx)($,{variant:"destructive",children:"Disabled"}):(0,G.jsx)($,{variant:"secondary",children:"Active"})]}),(0,G.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mt-1",children:F.topics.includes("*")?"All events":F.topics.join(", ")})]}),(0,G.jsxs)("div",{className:"af-flex af-items-center af-gap-1 af-shrink-0",children:[(0,G.jsx)(N,{variant:"ghost",size:"sm",onClick:()=>oe(x===F.id?null:F.id),children:x===F.id?"Hide log":"Deliveries"}),(0,G.jsx)(N,{variant:"ghost",size:"sm",onClick:()=>ve(F),disabled:A===F.id,children:T===F.id?"Hide secret":"Show secret"}),(0,G.jsx)(N,{variant:"ghost",size:"sm",title:"Delete webhook",disabled:A===F.id,onClick:()=>j(F),children:(0,G.jsx)(Xe.Trash2,{className:"af-h-4 af-w-4"})})]})]}),F.status==="disabled"&&(0,G.jsxs)("div",{className:"af-flex af-items-start af-gap-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2",children:[(0,G.jsx)(Xe.AlertTriangle,{className:"af-h-4 af-w-4 af-text-destructive af-mt-0.5 af-shrink-0"}),(0,G.jsxs)("div",{className:"af-text-sm",children:[(0,G.jsxs)("p",{children:["We stopped sending after repeated failures."," ",F.disabled_reason]}),(0,G.jsx)(N,{variant:"outline",size:"sm",className:"af-mt-2",disabled:A===F.id,onClick:()=>Le(F),children:"Re-enable"})]})]}),T===F.id&&W&&(0,G.jsxs)("div",{className:"af-rounded af-bg-muted af-px-3 af-py-2 af-space-y-2",children:[(0,G.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Optional. Use this if you want your endpoint to check that a delivery really came from us. Rotating replaces it."}),(0,G.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,G.jsx)(fe,{readOnly:!0,value:W,className:"af-font-mono"}),(0,G.jsx)(N,{variant:"outline",title:"Copy signing secret",onClick:_e,children:H?(0,G.jsx)(Xe.Check,{className:"af-h-4 af-w-4"}):(0,G.jsx)(Xe.Copy,{className:"af-h-4 af-w-4"})}),(0,G.jsx)(N,{variant:"outline",title:"Rotate signing secret",disabled:A===F.id,onClick:()=>ge(F),children:A===F.id?(0,G.jsx)(Xe.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,G.jsx)(Xe.RefreshCw,{className:"af-h-4 af-w-4"})})]})]}),x===F.id&&(0,G.jsx)(ja,{destinationId:F.id})]},F.id))]}),(0,G.jsx)(zt,{open:!!Q,onOpenChange:F=>{!F&&!A&&j(null)},children:(0,G.jsxs)(kt,{children:[(0,G.jsxs)(_t,{children:[(0,G.jsx)(Et,{children:"Delete this webhook?"}),(0,G.jsxs)(Ot,{children:["New submissions will stop being sent to ",Q?.url,". Its delivery history is removed too. Submissions themselves are not affected \u2014 they stay in your dashboard and still go out by email."]})]}),(0,G.jsxs)(Dt,{children:[(0,G.jsx)(N,{variant:"outline",onClick:()=>j(null),disabled:!!A,children:"Cancel"}),(0,G.jsx)(N,{variant:"destructive",onClick:ae,disabled:!!A,children:A?"Deleting...":"Delete"})]})]})})]})}var ne=require("react/jsx-runtime");function Wa({form:e,onSaved:t,className:a}){let{updateForm:s}=va(),[r,l]=ct.useState(!1),[c,b]=ct.useState(null),[u,m]=ct.useState(null),[n,y]=ct.useState(e.name),[w,v]=ct.useState(e.redirect_url||""),[f,p]=ct.useState(e.email_recipients),[h,S]=ct.useState("");ct.useEffect(()=>{y(e.name),v(e.redirect_url||""),p(e.email_recipients),m(null)},[e]);let T=ct.useCallback(async z=>{try{l(!0),m(null);let Q=await s(e.id,z);b(new Date),t?.(Q)}catch(Q){m(Q instanceof Error?Q.message:"Failed to save")}finally{l(!1)}},[e.id,s,t]);ct.useEffect(()=>{if(c){let z=setTimeout(()=>b(null),2e3);return()=>clearTimeout(z)}},[c]);let U=async()=>{let z=h.trim().toLowerCase();if(!z)return;if(!z.includes("@")){m("Please enter a valid email address");return}if(f.includes(z)){m("This email is already added");return}let Q=[...f,z];p(Q),S(""),m(null),await T({email_recipients:Q})},W=async z=>{let Q=f.filter(j=>j!==z);p(Q),await T({email_recipients:Q})},D=async()=>{n!==e.name&&await T({name:n})},H=async()=>{let z=w||null;z!==e.redirect_url&&await T({redirect_url:z})};return(0,ne.jsxs)("div",{className:`af-space-y-6 af-relative ${a||""}`,children:[(r||c)&&(0,ne.jsx)("div",{className:"aifeatures-admin af-fixed af-bottom-4 af-right-4 af-z-50",children:r?(0,ne.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-bg-background af-border af-shadow-lg af-rounded-lg af-px-4 af-py-3",children:[(0,ne.jsx)(ca.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"}),(0,ne.jsx)("span",{className:"af-text-sm",children:"Saving..."})]}):(0,ne.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-bg-green-50 af-border af-border-green-200 af-shadow-lg af-rounded-lg af-px-4 af-py-3",children:[(0,ne.jsx)(ca.Check,{className:"af-h-4 af-w-4 af-text-green-600"}),(0,ne.jsx)("span",{className:"af-text-sm af-text-green-800",children:"Changes saved"})]})}),(0,ne.jsxs)("div",{className:"af-space-y-2",children:[(0,ne.jsx)(le,{htmlFor:"form-name",children:"Form Name"}),(0,ne.jsx)(fe,{id:"form-name",value:n,onChange:z=>y(z.target.value),onBlur:D,placeholder:"Contact Form"}),(0,ne.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"A name to identify this form in your dashboard."})]}),(0,ne.jsxs)("div",{className:"af-space-y-2",children:[(0,ne.jsx)(le,{children:"Email Recipients"}),(0,ne.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mb-2",children:"Form submissions will be sent to these email addresses."}),(0,ne.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-2 af-mb-2",children:[f.map(z=>(0,ne.jsxs)($,{variant:"secondary",className:"af-gap-1",children:[z,(0,ne.jsx)("button",{type:"button",onClick:()=>W(z),className:"af-ml-1 hover:af-text-destructive",disabled:r,children:(0,ne.jsx)(ca.X,{className:"af-h-3 af-w-3"})})]},z)),f.length===0&&(0,ne.jsx)("span",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No recipients configured"})]}),(0,ne.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,ne.jsx)(fe,{type:"email",value:h,onChange:z=>S(z.target.value),placeholder:"Add email address",onKeyDown:z=>{z.key==="Enter"&&(z.preventDefault(),U())},disabled:r}),(0,ne.jsx)(N,{type:"button",variant:"outline",onClick:U,disabled:!h.trim()||r,children:(0,ne.jsx)(ca.Plus,{className:"af-h-4 af-w-4"})})]})]}),(0,ne.jsxs)("div",{className:"af-space-y-2",children:[(0,ne.jsx)(le,{htmlFor:"redirect-url",children:"Redirect URL"}),(0,ne.jsx)(fe,{id:"redirect-url",value:w,onChange:z=>v(z.target.value),onBlur:H,placeholder:"/thank-you"}),(0,ne.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Where to send visitors after a successful submission. Use a site path (/thank-you) or a full URL (https://example.com/thank-you). Leave empty for a default success page."})]}),(0,ne.jsxs)("div",{className:"af-space-y-2",children:[(0,ne.jsx)(le,{children:"Form Endpoint"}),(0,ne.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,ne.jsx)("code",{className:"af-flex-1 af-text-xs af-bg-muted af-px-3 af-py-2 af-rounded af-border",children:e.endpoint_url})}),(0,ne.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Use this URL as the form action attribute."})]}),(0,ne.jsxs)("div",{className:"af-space-y-2",children:[(0,ne.jsx)(le,{children:"Captcha Protection"}),(0,ne.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,ne.jsx)($,{variant:e.turnstile_sitekey?"success":"secondary",children:e.turnstile_sitekey?"Protected":"Not Configured"})}),(0,ne.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Turnstile CAPTCHA is automatically enabled to prevent spam."})]}),(0,ne.jsx)(qa,{formId:e.id}),u&&(0,ne.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:u})]})}var xe=require("react/jsx-runtime");function _s({className:e,submissionId:t}){let{api:a}=ee(),[s,r]=da.useState("list"),[l,c]=da.useState(null),[b,u]=da.useState("submissions"),[m,n]=da.useState(t),[y,w]=da.useState(!!t);da.useEffect(()=>{if(!t)return;async function h(){try{let S=await a.getSubmission(t),T=await a.getForm(S.form_id);c(T),r("detail"),u("submissions"),n(t)}catch(S){console.error("Failed to load submission for deep link:",S)}finally{w(!1)}}h()},[t,a]);let v=(h,S)=>{c(h),r("detail"),u(S),n(void 0)},f=()=>{r("list"),c(null),n(void 0)},p=h=>{c(h)};return y?(0,xe.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${e||""}`,children:(0,xe.jsx)("div",{className:"af-text-muted-foreground",children:"Loading..."})}):(0,xe.jsxs)("div",{className:e,children:[s==="list"&&(0,xe.jsxs)("div",{children:[(0,xe.jsxs)("div",{className:"af-mb-6",children:[(0,xe.jsx)("h2",{className:"af-text-lg af-font-semibold",children:"Forms"}),(0,xe.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Manage your contact forms and view submissions."})]}),(0,xe.jsx)(Aa,{onSelectForm:v})]}),s==="detail"&&l&&(0,xe.jsxs)("div",{children:[(0,xe.jsxs)("div",{className:"af-mb-6",children:[(0,xe.jsxs)(N,{variant:"ghost",size:"sm",onClick:f,className:"af-mb-2 af--ml-2",children:[(0,xe.jsx)(ka.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Forms"]}),(0,xe.jsx)("h2",{className:"af-text-lg af-font-semibold",children:l.name}),(0,xe.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:(0,xe.jsx)("code",{className:"af-text-xs af-bg-muted af-px-1.5 af-py-0.5 af-rounded",children:l.endpoint_url})})]}),(0,xe.jsxs)(ds,{value:b,onValueChange:h=>u(h),children:[(0,xe.jsxs)(Ba,{className:"af-mb-4",children:[(0,xe.jsxs)(Xt,{value:"submissions",className:"af-gap-2",children:[(0,xe.jsx)(ka.Inbox,{className:"af-h-4 af-w-4"}),"Submissions"]}),(0,xe.jsxs)(Xt,{value:"settings",className:"af-gap-2",children:[(0,xe.jsx)(ka.Settings,{className:"af-h-4 af-w-4"}),"Settings"]})]}),(0,xe.jsx)(ea,{value:"submissions",children:(0,xe.jsx)(Ha,{formId:l.id,defaultSubmissionId:m})}),(0,xe.jsx)(ea,{value:"settings",children:(0,xe.jsx)(Wa,{form:l,onSaved:p})})]})]})]})}var Ns=te(require("react"),1),Kt=require("lucide-react");var ms=te(require("react"),1),sa=require("lucide-react");var Nn=new Set(["bif","clp","djf","gnf","jpy","kmf","krw","mga","pyg","rwf","vnd","vuv","xaf","xof","xpf"]);function Lt(e){return Nn.has(e.toLowerCase())}function M(e,t,a){let s=Lt(t)?e:e/100;try{return new Intl.NumberFormat(a,{style:"currency",currency:t.toUpperCase()}).format(s)}catch{return`${s} ${t.toUpperCase()}`}}function St(e){let t=e.automatic_tax&&e.tax_behavior==="exclusive"?e.tax_total??0:0;return e.amount_total+(e.shipping_total??0)+t}function Pt(e,t){let a=typeof e=="string"?parseFloat(e):e;if(!Number.isFinite(a)||a<0)return null;let s=Lt(t)?a:a*100;return Math.round(s)}function qe(e,t){return Lt(t)?e:e/100}var X=require("react/jsx-runtime");function wn(e){if(e.allow_custom_amount){let t=e.min_amount!==null?M(e.min_amount,e.currency):"?",a=e.max_amount!==null?M(e.max_amount,e.currency):"?";return`Buyer chooses (${t} - ${a})`}return e.unit_amount===null?"Varies by variant":M(e.unit_amount,e.currency)}function Sn(e){switch(e.fulfillment.type){case"download":return e.fulfillment.filename||"Digital download";case"redirect":return"Redirect";default:return"-"}}function Ga({productsState:e,onSelectProduct:t,onCreateProduct:a,className:s}){let{products:r,isLoading:l,error:c,deleteProduct:b}=e,[u,m]=ms.useState(null),[n,y]=ms.useState(!1),[w,v]=ms.useState(null),f=async()=>{if(u)try{y(!0),v(null),await b(u.id),m(null)}catch(p){v(p instanceof Error?p.message:"Failed to delete product")}finally{y(!1)}};return l?(0,X.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${s||""}`,children:(0,X.jsx)("div",{className:"af-text-muted-foreground",children:"Loading products..."})}):c?(0,X.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${s||""}`,children:(0,X.jsxs)("div",{className:"af-text-destructive",children:["Error: ",c.message]})}):r.length===0?(0,X.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${s||""}`,children:[(0,X.jsx)(sa.Package,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,X.jsxs)("p",{className:"af-text-muted-foreground af-text-center af-mb-4",children:["No products yet.",(0,X.jsx)("br",{}),"Products will appear here when the AI adds them to your website, or you can create one yourself."]}),(0,X.jsxs)(N,{variant:"outline",onClick:()=>a?.(),children:[(0,X.jsx)(sa.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"New Product"]})]}):(0,X.jsxs)("div",{className:s,children:[(0,X.jsx)("div",{className:"af-flex af-justify-end af-mb-4",children:(0,X.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>a?.(),children:[(0,X.jsx)(sa.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"New Product"]})}),(0,X.jsxs)(Re,{children:[(0,X.jsx)(Ce,{children:(0,X.jsxs)(ie,{children:[(0,X.jsx)(L,{children:"Name"}),(0,X.jsx)(L,{children:"Price"}),(0,X.jsx)(L,{children:"Fulfillment"}),(0,X.jsx)(L,{children:"Status"}),(0,X.jsx)(L,{className:"af-text-right",children:"Actions"})]})}),(0,X.jsx)(Te,{children:r.map(p=>(0,X.jsxs)(ie,{children:[(0,X.jsx)(B,{className:"af-font-medium",children:p.name}),(0,X.jsx)(B,{children:wn(p)}),(0,X.jsx)(B,{className:"af-text-muted-foreground",children:Sn(p)}),(0,X.jsx)(B,{children:(0,X.jsx)($,{variant:p.active?"success":"secondary",children:p.active?"Active":"Inactive"})}),(0,X.jsx)(B,{className:"af-text-right",children:(0,X.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-2",children:[(0,X.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>t?.(p),children:[(0,X.jsx)(sa.Pencil,{className:"af-h-4 af-w-4 af-mr-1"}),"Edit"]}),(0,X.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>{v(null),m(p)},children:[(0,X.jsx)(sa.Trash2,{className:"af-h-4 af-w-4 af-mr-1"}),"Delete"]})]})})]},p.id))})]}),(0,X.jsx)(zt,{open:!!u,onOpenChange:p=>{!p&&!n&&m(null)},children:(0,X.jsxs)(kt,{children:[(0,X.jsxs)(_t,{children:[(0,X.jsx)(Et,{children:"Delete product?"}),(0,X.jsxs)(Ot,{children:['"',u?.name,'" will be removed from your site and can no longer be purchased. Past orders and buyer downloads are not affected.']})]}),w&&(0,X.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:w}),(0,X.jsxs)(Dt,{children:[(0,X.jsx)(N,{variant:"outline",onClick:()=>m(null),disabled:n,children:"Cancel"}),(0,X.jsx)(N,{variant:"destructive",onClick:f,disabled:n,children:n?"Deleting...":"Delete"})]})]})})]})}var De=te(require("react"),1),dt=require("lucide-react");var He=te(require("react"),1),Rt=require("lucide-react");var g=require("react/jsx-runtime");function Ds(e){return e.option_values.length>0?e.option_values.map(t=>t.value).join(" / "):e.title||"Default"}function Ka({productId:e,currency:t,className:a}){let{api:s}=ee(),r=Lt(t)?"1":"0.01",[l,c]=He.useState(null),[b,u]=He.useState(!0),[m,n]=He.useState(null),[y,w]=He.useState(null),[v,f]=He.useState(!1),p=He.useRef(!0);He.useEffect(()=>(p.current=!0,()=>{p.current=!1}),[]);let h=He.useCallback(async()=>{try{let P=await s.getProduct(e);if(!p.current)return;c(P)}catch(P){if(!p.current)return;n(P instanceof Error?P.message:"Failed to load product")}finally{p.current&&u(!1)}},[s,e]);He.useEffect(()=>{h()},[h]);let S=async P=>{w(null),f(!0);try{await P(),await h()}catch(O){p.current&&w(O instanceof Error?O.message:"Something went wrong")}finally{p.current&&f(!1)}},[T,U]=He.useState(""),[W,D]=He.useState(""),[H,z]=He.useState({}),Q=()=>S(async()=>{let P=T.trim(),O=W.split(",").map(ue=>ue.trim()).filter(Boolean);if(!P)throw new Error("Option name is required");if(O.length===0)throw new Error("Add at least one value");await s.createOption(e,{title:P,values:O}),p.current&&(U(""),D(""))}),j=P=>S(async()=>{let O=(H[P.id]??"").trim();if(!O)throw new Error("Enter a value");await s.updateOption(P.id,{add_values:[O]}),p.current&&z(ue=>({...ue,[P.id]:""}))}),A=(P,O)=>S(()=>s.updateOption(P.id,{remove_values:[O]})),de=P=>S(()=>s.deleteOption(P.id)),[x,oe]=He.useState(null),[we,he]=He.useState(null),[ge,Le]=He.useState(!1),[ae,ve]=He.useState(null),_e=l?.options??[],F=l?.variants??[],be=()=>{he(null),oe({variant:null,title:"",sku:"",unitAmount:"",compareAt:"",active:!0,optionValues:Object.fromEntries(_e.map(P=>[P.id,P.values[0]?.value??""])),tracked:!1,onHand:"0",initialOnHand:"0",allowBackorder:!1})},At=P=>{he(null);let O=P.inventory?.on_hand??0,ue=O<0?"":String(O);oe({variant:P,title:P.title??"",sku:P.sku??"",unitAmount:P.unit_amount!=null?String(qe(P.unit_amount,t)):"",compareAt:P.compare_at_amount!=null?String(qe(P.compare_at_amount,t)):"",active:P.active,optionValues:{},tracked:P.inventory?.tracked??!1,onHand:ue,initialOnHand:ue,allowBackorder:P.inventory?.allow_backorder??!1})},Ft=async()=>{if(!x)return;he(null);let P=x.variant?.allow_custom_amount??!1,O=null,ue=null;if(!P){if(O=Pt(x.unitAmount,t),O===null||O<=0){he("Enter a valid price");return}if(x.compareAt.trim()&&(ue=Pt(x.compareAt,t),ue===null||ue<=O)){he("Compare-at price must be greater than the price");return}}let Je=x.onHand.trim()!==x.initialOnHand.trim(),Ze=parseInt(x.onHand,10);if(x.tracked&&Je&&(!Number.isFinite(Ze)||Ze<0)){he("On-hand quantity must be zero or more");return}try{if(Le(!0),x.variant)await s.updateVariant(x.variant.id,{title:x.title.trim()||null,sku:x.sku.trim()||null,...P?{}:{unit_amount:O,compare_at_amount:ue},active:x.active}),await s.setVariantInventory(x.variant.id,{tracked:x.tracked,on_hand:x.tracked&&Je?Ze:void 0,allow_backorder:x.allowBackorder});else{let Qe={};for(let Ke of _e){let Vt=x.optionValues[Ke.id];if(!Vt){he(`Choose a ${Ke.title}`),Le(!1);return}Qe[Ke.title]=Vt}await s.createVariant(e,{title:x.title.trim()||null,sku:x.sku.trim()||null,unit_amount:O,compare_at_amount:ue,active:x.active,option_values:_e.length>0?Qe:void 0,inventory:x.tracked?{tracked:!0,on_hand:Ze,allow_backorder:x.allowBackorder}:{tracked:!1,allow_backorder:x.allowBackorder}})}await h(),p.current&&oe(null)}catch(Qe){if(p.current&&he(Qe instanceof Error?Qe.message:"Failed to save variant"),x.variant)try{let Ke=await s.getProduct(e);p.current&&c(Ke)}catch{}}finally{p.current&&Le(!1)}},Be=()=>S(async()=>{ae&&(await s.deleteVariant(ae.id),p.current&&ve(null))}),vt=x?.variant?.allow_custom_amount??!1,bt=x?.variant!=null&&x.variant.option_values.length===0&&_e.length>0&&!x.variant.active,Ve=x?.variant?.inventory!=null&&x.variant.inventory.on_hand<0?-x.variant.inventory.on_hand:null;function Ut(P){let O=P.inventory;if(!O||!O.tracked)return(0,g.jsx)("span",{className:"af-text-muted-foreground",children:"Unlimited"});let ue=O.available??O.on_hand;return ue<0?(0,g.jsxs)("span",{children:[-ue," backordered"]}):(0,g.jsxs)("span",{children:[ue," in stock",O.allow_backorder&&(0,g.jsx)("span",{className:"af-text-muted-foreground af-text-xs af-ml-1",children:"(backorder)"})]})}return b?(0,g.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-6 ${a||""}`,children:(0,g.jsx)(Rt.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})}):m?(0,g.jsx)("div",{className:a,children:(0,g.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:m})}):(0,g.jsxs)("div",{className:`af-space-y-6 ${a||""}`,children:[(0,g.jsxs)("div",{className:"af-space-y-3",children:[(0,g.jsxs)("div",{children:[(0,g.jsx)(le,{children:"Options"}),(0,g.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Add options like Size or Color, then create a variant for each combination you sell. Simple products don't need options."})]}),_e.length>0&&(0,g.jsx)("div",{className:"af-space-y-3",children:_e.map(P=>(0,g.jsxs)("div",{className:"af-border af-rounded af-p-3 af-space-y-2",children:[(0,g.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,g.jsx)("span",{className:"af-text-sm af-font-medium",children:P.title}),(0,g.jsx)(N,{variant:"ghost",size:"sm",onClick:()=>de(P),disabled:v,children:(0,g.jsx)(Rt.Trash2,{className:"af-h-4 af-w-4"})})]}),(0,g.jsx)("div",{className:"af-flex af-flex-wrap af-gap-2",children:P.values.map(O=>(0,g.jsxs)($,{variant:"secondary",className:"af-gap-1",children:[O.value,(0,g.jsx)("button",{type:"button",onClick:()=>A(P,O.value),className:"af-ml-1 hover:af-text-destructive",disabled:v,title:"Remove value",children:(0,g.jsx)(Rt.X,{className:"af-h-3 af-w-3"})})]},O.id))}),(0,g.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,g.jsx)(fe,{value:H[P.id]??"",onChange:O=>z(ue=>({...ue,[P.id]:O.target.value})),placeholder:"Add a value",className:"af-h-8",onKeyDown:O=>{O.key==="Enter"&&(O.preventDefault(),j(P))},disabled:v}),(0,g.jsx)(N,{type:"button",variant:"outline",size:"sm",onClick:()=>j(P),disabled:v||!(H[P.id]??"").trim(),children:(0,g.jsx)(Rt.Plus,{className:"af-h-4 af-w-4"})})]})]},P.id))}),(0,g.jsxs)("div",{className:"af-flex af-flex-wrap af-items-end af-gap-2",children:[(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{htmlFor:"new-option-title",className:"af-text-xs af-text-muted-foreground",children:"Option name"}),(0,g.jsx)(fe,{id:"new-option-title",value:T,onChange:P=>U(P.target.value),placeholder:"Size",className:"af-w-[140px]",disabled:v})]}),(0,g.jsxs)("div",{className:"af-space-y-1 af-flex-1 af-min-w-[180px]",children:[(0,g.jsx)(le,{htmlFor:"new-option-values",className:"af-text-xs af-text-muted-foreground",children:"Values (comma-separated)"}),(0,g.jsx)(fe,{id:"new-option-values",value:W,onChange:P=>D(P.target.value),placeholder:"S, M, L",disabled:v})]}),(0,g.jsxs)(N,{type:"button",variant:"outline",onClick:Q,disabled:v||!T.trim()||!W.trim(),children:[(0,g.jsx)(Rt.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"Add option"]})]})]}),(0,g.jsxs)("div",{className:"af-space-y-3",children:[(0,g.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,g.jsx)(le,{children:"Variants"}),(0,g.jsxs)(N,{type:"button",variant:"outline",size:"sm",onClick:be,disabled:v||_e.length===0,children:[(0,g.jsx)(Rt.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"Add variant"]})]}),_e.length===0&&(0,g.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Add an option (e.g. Size) above before adding variants."}),F.length===0?(0,g.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No variants yet."}):(0,g.jsxs)(Re,{children:[(0,g.jsx)(Ce,{children:(0,g.jsxs)(ie,{children:[(0,g.jsx)(L,{children:"Variant"}),(0,g.jsx)(L,{children:"SKU"}),(0,g.jsx)(L,{children:"Price"}),(0,g.jsx)(L,{children:"Stock"}),(0,g.jsx)(L,{children:"Status"}),(0,g.jsx)(L,{className:"af-text-right",children:"Actions"})]})}),(0,g.jsx)(Te,{children:F.map(P=>(0,g.jsxs)(ie,{children:[(0,g.jsx)(B,{className:"af-font-medium",children:Ds(P)}),(0,g.jsx)(B,{className:"af-text-muted-foreground",children:P.sku||"-"}),(0,g.jsx)(B,{children:P.unit_amount!=null?(0,g.jsxs)("span",{children:[P.compare_at_amount!=null&&(0,g.jsx)("span",{className:"af-text-muted-foreground af-line-through af-mr-1",children:M(P.compare_at_amount,P.currency)}),M(P.unit_amount,P.currency)]}):P.allow_custom_amount?(0,g.jsx)("span",{className:"af-text-muted-foreground",children:"Buyer chooses"}):"-"}),(0,g.jsx)(B,{children:Ut(P)}),(0,g.jsx)(B,{children:(0,g.jsx)($,{variant:P.active?"success":"secondary",children:P.active?"Active":"Inactive"})}),(0,g.jsx)(B,{className:"af-text-right",children:(0,g.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-1",children:[(0,g.jsx)(N,{variant:"ghost",size:"sm",onClick:()=>At(P),disabled:v,children:"Edit"}),(0,g.jsx)(N,{variant:"ghost",size:"icon",onClick:()=>ve(P),disabled:v,title:"Delete variant",children:(0,g.jsx)(Rt.Trash2,{className:"af-h-4 af-w-4"})})]})})]},P.id))})]})]}),y&&(0,g.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:y}),(0,g.jsx)(zt,{open:x!==null,onOpenChange:P=>{!P&&!ge&&oe(null)},children:(0,g.jsxs)(kt,{children:[(0,g.jsxs)(_t,{children:[(0,g.jsx)(Et,{children:x?.variant?"Edit variant":"Add variant"}),(0,g.jsx)(Ot,{children:"Price, markdown, and stock live on the variant."})]}),x&&(0,g.jsxs)("div",{className:"af-space-y-3",children:[x.variant?x.variant.option_values.length>0&&(0,g.jsxs)("div",{className:"af-text-sm",children:[(0,g.jsx)("span",{className:"af-text-muted-foreground af-mr-2",children:"Options"}),Ds(x.variant)]}):_e.map(P=>(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{className:"af-text-xs af-text-muted-foreground",children:P.title}),(0,g.jsxs)(ht,{value:x.optionValues[P.id]??"",onValueChange:O=>oe(ue=>ue&&{...ue,optionValues:{...ue.optionValues,[P.id]:O}}),children:[(0,g.jsx)(ot,{children:(0,g.jsx)(xt,{placeholder:`Choose ${P.title}`})}),(0,g.jsx)(lt,{children:P.values.map(O=>(0,g.jsx)(ke,{value:O.value,children:O.value},O.id))})]})]},P.id)),vt?(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{className:"af-text-xs af-text-muted-foreground",children:"Price"}),(0,g.jsx)("p",{className:"af-text-sm",children:"Buyer chooses the amount \u2014 this variant has no fixed price."})]}):(0,g.jsxs)("div",{className:"af-flex af-gap-3",children:[(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsxs)(le,{htmlFor:"variant-price",className:"af-text-xs af-text-muted-foreground",children:["Price (",t.toUpperCase(),")"]}),(0,g.jsx)(fe,{id:"variant-price",type:"number",min:"0",step:r,value:x.unitAmount,onChange:P=>oe(O=>O&&{...O,unitAmount:P.target.value}),placeholder:"25.00",className:"af-w-[130px]"})]}),(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{htmlFor:"variant-compare",className:"af-text-xs af-text-muted-foreground",children:"Compare-at (optional)"}),(0,g.jsx)(fe,{id:"variant-compare",type:"number",min:"0",step:r,value:x.compareAt,onChange:P=>oe(O=>O&&{...O,compareAt:P.target.value}),placeholder:"40.00",className:"af-w-[130px]"})]})]}),(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{htmlFor:"variant-sku",className:"af-text-xs af-text-muted-foreground",children:"SKU (optional)"}),(0,g.jsx)(fe,{id:"variant-sku",value:x.sku,onChange:P=>oe(O=>O&&{...O,sku:P.target.value}),placeholder:"TEE-L-BLACK",className:"af-w-[200px]"})]}),(0,g.jsxs)("div",{className:"af-space-y-2 af-border-t af-pt-3",children:[(0,g.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,g.jsx)("input",{type:"checkbox",checked:x.tracked,onChange:P=>oe(O=>O&&{...O,tracked:P.target.checked}),className:"af-h-4 af-w-4"}),"Track inventory"]}),x.tracked&&(0,g.jsxs)("div",{className:"af-space-y-1 af-pl-6",children:[(0,g.jsxs)("div",{className:"af-flex af-items-end af-gap-3",children:[(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsx)(le,{htmlFor:"variant-onhand",className:"af-text-xs af-text-muted-foreground",children:"On hand"}),(0,g.jsx)(fe,{id:"variant-onhand",type:"number",min:"0",step:"1",value:x.onHand,onChange:P=>oe(O=>O&&{...O,onHand:P.target.value}),placeholder:Ve!=null?"0":void 0,className:"af-w-[100px]"})]}),(0,g.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm af-mb-2",children:[(0,g.jsx)("input",{type:"checkbox",checked:x.allowBackorder,onChange:P=>oe(O=>O&&{...O,allowBackorder:P.target.checked}),className:"af-h-4 af-w-4"}),"Allow backorder"]})]}),Ve!=null&&(0,g.jsxs)("p",{className:"af-text-xs af-text-muted-foreground af-max-w-[300px]",children:["Currently ",Ve," backordered. Leave the field empty to keep the backorder count, or enter a quantity (0 or more) to reset stock and clear it."]})]})]}),(0,g.jsxs)("div",{className:"af-space-y-1",children:[(0,g.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,g.jsx)("input",{type:"checkbox",checked:x.active,onChange:P=>oe(O=>O&&{...O,active:P.target.checked}),className:"af-h-4 af-w-4",disabled:bt}),"Active (buyers can purchase this variant)"]}),bt&&(0,g.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-pl-6",children:"This is the original pre-options variant \u2014 it has no option values, so buyers can't select it. It stays inactive while the product has options; delete all options to use it again."})]}),we&&(0,g.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:we})]}),(0,g.jsxs)(Dt,{children:[(0,g.jsx)(N,{variant:"outline",onClick:()=>oe(null),disabled:ge,children:"Cancel"}),(0,g.jsxs)(N,{onClick:Ft,disabled:ge,children:[ge?(0,g.jsx)(Rt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):null,x?.variant?"Save":"Add variant"]})]})]})}),(0,g.jsx)(zt,{open:ae!==null,onOpenChange:P=>{!P&&!v&&ve(null)},children:(0,g.jsxs)(kt,{children:[(0,g.jsxs)(_t,{children:[(0,g.jsx)(Et,{children:"Delete variant?"}),(0,g.jsx)(Ot,{children:ae&&`"${Ds(ae)}" will no longer be purchasable. Past orders are unaffected.`})]}),(0,g.jsxs)(Dt,{children:[(0,g.jsx)(N,{variant:"outline",onClick:()=>ve(null),disabled:v,children:"Cancel"}),(0,g.jsx)(N,{variant:"destructive",onClick:Be,disabled:v,children:"Delete"})]})]})})]})}var _=require("react/jsx-runtime");function Pn(e){if(e===0)return"0 B";let t=1024,a=["B","KB","MB","GB"],s=Math.floor(Math.log(e)/Math.log(t));return`${parseFloat((e/Math.pow(t,s)).toFixed(1))} ${a[s]}`}function Ja({productsState:e,product:t,currency:a,onSaved:s,className:r}){let{createProduct:l,updateProduct:c,attachProductFile:b,attachProductFileFromUrl:u}=e,m=!!t,n=t?.currency??a,y=Lt(n)?"1":"0.01",w=t!=null&&t.unit_amount===null&&!t.allow_custom_amount,[v,f]=De.useState(!1),[p,h]=De.useState(null),S=De.useRef(!0);De.useEffect(()=>(S.current=!0,()=>{S.current=!1}),[]);let[T,U]=De.useState(t?.name??""),[W,D]=De.useState(t?.description??""),[H,z]=De.useState(t?.allow_custom_amount?"custom":"fixed"),[Q,j]=De.useState(t?.unit_amount!=null?String(qe(t.unit_amount,n)):""),[A,de]=De.useState(t?.min_amount!=null?String(qe(t.min_amount,n)):""),[x,oe]=De.useState(t?.max_amount!=null?String(qe(t.max_amount,n)):""),[we,he]=De.useState(String(t?.max_quantity??10)),ge=De.useRef({pricingMode:t?.allow_custom_amount?"custom":"fixed",unitAmount:t?.unit_amount!=null?String(qe(t.unit_amount,n)):"",minAmount:t?.min_amount!=null?String(qe(t.min_amount,n)):"",maxAmount:t?.max_amount!=null?String(qe(t.max_amount,n)):"",maxQuantity:String(t?.max_quantity??10)}).current,[Le,ae]=De.useState(t?.collect_shipping_address??!1),[ve,_e]=De.useState(t?.active??!0),[F,be]=De.useState(t?.images??[]),[At,Ft]=De.useState(""),[Be,vt]=De.useState(t?.fulfillment??null),[bt,Ve]=De.useState(!1),[Ut,P]=De.useState(""),[O,ue]=De.useState(null),Je=De.useRef(null),Ze=v||bt,Qe=()=>{let J=At.trim();if(J){if(F.includes(J)){h("This image URL is already added");return}be(Pe=>[...Pe,J]),Ft(""),h(null)}},Ke=J=>{be(Pe=>Pe.filter(Ee=>Ee!==J))},Vt=async()=>{if(h(null),!T.trim()){h("Product name is required");return}let J=null,Pe=null,Ee=null,ia=!m||H!==ge.pricingMode||(H==="fixed"?Q!==ge.unitAmount:A!==ge.minAmount||x!==ge.maxAmount),Ht=!m||we!==ge.maxQuantity,Ta=w||H==="fixed";if(!(w||!ia))if(H==="fixed"){if(J=Pt(Q,n),J===null||J<=0){h("Enter a valid price");return}}else{if(Pe=Pt(A,n),Ee=Pt(x,n),Pe===null||Ee===null){h("Enter valid minimum and maximum amounts");return}if(Ee<Pe){h("Maximum amount must be at least the minimum amount");return}}let yt=parseInt(we,10);if(Ta&&Ht&&(!Number.isFinite(yt)||yt<1)){h("Max quantity must be at least 1");return}try{f(!0);let Ie;if(m&&t){let je={name:T.trim(),description:W.trim()||null,images:F,collect_shipping_address:Le,active:ve};!w&&ia&&(je.allow_custom_amount=H==="custom",H==="fixed"?je.unit_amount=J:(je.min_amount=Pe,je.max_amount=Ee)),Ta&&Ht&&(je.max_quantity=yt),Ie=await c(t.id,je),ge.pricingMode=Ie.allow_custom_amount?"custom":"fixed",ge.unitAmount=Ie.unit_amount!=null?String(qe(Ie.unit_amount,n)):"",ge.minAmount=Ie.min_amount!=null?String(qe(Ie.min_amount,n)):"",ge.maxAmount=Ie.max_amount!=null?String(qe(Ie.max_amount,n)):"",ge.maxQuantity=String(Ie.max_quantity)}else{let je={name:T.trim(),allow_custom_amount:H==="custom",collect_shipping_address:Le,active:ve};W.trim()&&(je.description=W.trim()),F.length>0&&(je.images=F),H==="fixed"?(je.unit_amount=J,je.max_quantity=yt):(je.min_amount=Pe,je.max_amount=Ee),Ie=await l(je)}if(!S.current)return;vt(Ie.fulfillment),s?.(Ie)}catch(Ie){if(!S.current)return;h(Ie instanceof Error?Ie.message:"Failed to save product")}finally{S.current&&f(!1)}},Ra=async J=>{let Pe=J.target.files?.[0];if(!(!Pe||!t))try{Ve(!0),ue(null);let Ee=await b(t.id,Pe);if(!S.current)return;vt(Ee.fulfillment),s?.(Ee)}catch(Ee){if(!S.current)return;ue(Ee instanceof Error?Ee.message:"Failed to upload file")}finally{S.current&&(Ve(!1),Je.current&&(Je.current.value=""))}},Ca=async()=>{let J=Ut.trim();if(!(!J||!t))try{Ve(!0),ue(null);let Pe=await u(t.id,J);if(!S.current)return;vt(Pe.fulfillment),P(""),s?.(Pe)}catch(Pe){if(!S.current)return;ue(Pe instanceof Error?Pe.message:"Failed to fetch file")}finally{S.current&&Ve(!1)}};return(0,_.jsxs)("div",{className:`af-space-y-6 ${r||""}`,children:[(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{htmlFor:"product-name",children:"Name"}),(0,_.jsx)(fe,{id:"product-name",value:T,onChange:J=>U(J.target.value),placeholder:"My product"})]}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{htmlFor:"product-description",children:"Description"}),(0,_.jsx)("textarea",{id:"product-description",value:W,onChange:J=>D(J.target.value),rows:3,placeholder:"Shown on the Stripe checkout page.",className:"af-flex af-w-full af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-2 af-text-sm af-shadow-sm placeholder:af-text-muted-foreground focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring"})]}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{children:"Pricing"}),w?(0,_.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"This product is priced per variant. Set prices in the Options & Variants editor below."}):(0,_.jsxs)(_.Fragment,{children:[(0,_.jsxs)(ht,{value:H,onValueChange:J=>z(J),children:[(0,_.jsx)(ot,{className:"af-w-[260px]",children:(0,_.jsx)(xt,{})}),(0,_.jsxs)(lt,{children:[(0,_.jsx)(ke,{value:"fixed",children:"Fixed price"}),(0,_.jsx)(ke,{value:"custom",children:"Buyer chooses the amount"})]})]}),H==="fixed"?(0,_.jsx)("div",{className:"af-flex af-items-end af-gap-3",children:(0,_.jsxs)("div",{className:"af-space-y-1",children:[(0,_.jsxs)(le,{htmlFor:"product-price",className:"af-text-xs af-text-muted-foreground",children:["Price (",n.toUpperCase(),")"]}),(0,_.jsx)(fe,{id:"product-price",type:"number",min:"0",step:y,value:Q,onChange:J=>j(J.target.value),placeholder:"29.00",className:"af-w-[140px]"})]})}):(0,_.jsxs)("div",{className:"af-space-y-1",children:[(0,_.jsxs)("div",{className:"af-flex af-items-end af-gap-3",children:[(0,_.jsxs)("div",{className:"af-space-y-1",children:[(0,_.jsxs)(le,{htmlFor:"product-min-amount",className:"af-text-xs af-text-muted-foreground",children:["Minimum (",n.toUpperCase(),")"]}),(0,_.jsx)(fe,{id:"product-min-amount",type:"number",min:"0",step:y,value:A,onChange:J=>de(J.target.value),placeholder:"1.00",className:"af-w-[140px]"})]}),(0,_.jsxs)("div",{className:"af-space-y-1",children:[(0,_.jsxs)(le,{htmlFor:"product-max-amount",className:"af-text-xs af-text-muted-foreground",children:["Maximum (",n.toUpperCase(),")"]}),(0,_.jsx)(fe,{id:"product-max-amount",type:"number",min:"0",step:y,value:x,onChange:J=>oe(J.target.value),placeholder:"500.00",className:"af-w-[140px]"})]})]}),(0,_.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Buyers pick their own amount within these bounds (tip jar, pay-what-you-want). Quantity is always 1."})]}),(0,_.jsxs)("p",{className:"af-text-xs af-text-muted-foreground",children:["Prices are in your site currency (",n.toUpperCase(),"), which follows the country of your payment account."]})]}),(w||H==="fixed")&&(0,_.jsxs)("div",{className:"af-space-y-1",children:[(0,_.jsx)(le,{htmlFor:"product-max-quantity",className:"af-text-xs af-text-muted-foreground",children:"Max quantity per order"}),(0,_.jsx)(fe,{id:"product-max-quantity",type:"number",min:"1",step:"1",value:we,onChange:J=>he(J.target.value),className:"af-w-[140px]"})]})]}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{children:"Images"}),(0,_.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mb-2",children:"Image URLs shown on the Stripe checkout page."}),(0,_.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-2 af-mb-2",children:[F.map(J=>(0,_.jsxs)($,{variant:"secondary",className:"af-gap-1 af-max-w-[320px]",children:[(0,_.jsx)("span",{className:"af-truncate",children:J}),(0,_.jsx)("button",{type:"button",onClick:()=>Ke(J),className:"af-ml-1 hover:af-text-destructive",disabled:v,children:(0,_.jsx)(dt.X,{className:"af-h-3 af-w-3"})})]},J)),F.length===0&&(0,_.jsx)("span",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No images"})]}),(0,_.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,_.jsx)(fe,{type:"url",value:At,onChange:J=>Ft(J.target.value),placeholder:"https://example.com/image.jpg",onKeyDown:J=>{J.key==="Enter"&&(J.preventDefault(),Qe())},disabled:v}),(0,_.jsx)(N,{type:"button",variant:"outline",onClick:Qe,disabled:!At.trim()||v,children:(0,_.jsx)(dt.Plus,{className:"af-h-4 af-w-4"})})]})]}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{children:"Digital File"}),(0,_.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Attach a file (PDF, ZIP, EPUB, image, audio, or video; max 50 MB) and buyers get a download link after paying."}),m?(0,_.jsxs)("div",{className:"af-space-y-3",children:[Be?.type==="download"&&(0,_.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-p-2 af-bg-muted af-rounded",children:[(0,_.jsx)(dt.FileText,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,_.jsx)("span",{className:"af-text-sm",children:Be.filename}),Be.size!=null&&(0,_.jsxs)("span",{className:"af-text-xs af-text-muted-foreground",children:["(",Pn(Be.size),")"]})]}),Be?.type==="redirect"&&Be.url&&(0,_.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-p-2 af-bg-muted af-rounded",children:[(0,_.jsx)(dt.Link2,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,_.jsxs)("span",{className:"af-text-sm af-truncate",children:["Buyers are sent to ",Be.url]})]}),(0,_.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,_.jsx)("input",{ref:Je,type:"file",onChange:Ra,className:"af-hidden",disabled:Ze}),(0,_.jsxs)(N,{type:"button",variant:"outline",size:"sm",onClick:()=>Je.current?.click(),disabled:Ze,children:[bt?(0,_.jsx)(dt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,_.jsx)(dt.Upload,{className:"af-h-4 af-w-4 af-mr-1"}),Be?.type==="download"?"Replace file":"Upload file"]}),(0,_.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:"or"}),(0,_.jsx)(fe,{type:"url",value:Ut,onChange:J=>P(J.target.value),placeholder:"https://example.com/file.pdf",className:"af-flex-1",disabled:bt}),(0,_.jsx)(N,{type:"button",variant:"outline",size:"sm",onClick:Ca,disabled:!Ut.trim()||Ze,children:"Fetch from URL"})]}),O&&(0,_.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:O})]}):(0,_.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"Save the product first, then attach a file."})]}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsx)(le,{children:"Settings"}),(0,_.jsxs)("div",{className:"af-space-y-2",children:[(0,_.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,_.jsx)("input",{type:"checkbox",checked:Le,onChange:J=>ae(J.target.checked),className:"af-h-4 af-w-4"}),"Collect a shipping address at checkout"]}),(0,_.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,_.jsx)("input",{type:"checkbox",checked:ve,onChange:J=>_e(J.target.checked),className:"af-h-4 af-w-4"}),"Active (buyers can purchase this product)"]})]})]}),m&&t&&(0,_.jsxs)("div",{className:"af-space-y-2 af-border-t af-pt-6",children:[(0,_.jsx)(le,{children:"Options & Variants"}),(0,_.jsx)(Ka,{productId:t.id,currency:n})]}),p&&(0,_.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:p}),(0,_.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,_.jsxs)(N,{type:"button",onClick:Vt,disabled:Ze,children:[v?(0,_.jsx)(dt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):null,m?"Save changes":"Create product"]})})]})}var We=te(require("react"),1),mt=require("lucide-react");var Oe=te(require("react"),1);var Rn=["pending","paid","failed","expired","refunded","partially_refunded","disputed"];function Za({initialPageSize:e=25,refreshKey:t}={}){let{api:a}=ee(),[s,r]=Oe.useState([]),[l,c]=Oe.useState(0),[b,u]=Oe.useState(!0),[m,n]=Oe.useState(null),[y,w]=Oe.useState(0),[v,f]=Oe.useState(e),[p,h]=Oe.useState(void 0),[S,T]=Oe.useState(""),[U,W]=Oe.useState(""),[D,H]=Oe.useState(""),[z,Q]=Oe.useState(!1),[j,A]=Oe.useState(!1),[de,x]=Oe.useState(void 0),oe=de?de.join(","):"",we=Oe.useMemo(()=>{if(!oe)return;let ae=new Set(oe.split(","));return Rn.filter(ve=>!ae.has(ve))},[oe]),he=Oe.useRef(0),ge=Oe.useCallback(async()=>{let ae=++he.current;try{u(!0),n(null);let ve={limit:v,offset:y*v};p?ve.status=p:we?ve.excludeStatus=we:z||(ve.excludeStatus=["pending","expired"]),S.trim()&&(ve.customerEmail=S.trim()),U.trim()&&(ve.from=U.trim()),D.trim()&&(ve.to=D.trim()),j&&(ve.unfulfilledOnly=!0);let _e=await a.getOrders(ve);if(ae!==he.current)return;r(_e.orders),c(_e.total)}catch(ve){if(ae!==he.current)return;n(ve instanceof Error?ve:new Error("Failed to fetch orders"))}finally{ae===he.current&&u(!1)}},[a,y,v,p,we,z,j,S,U,D,t]);Oe.useEffect(()=>{ge()},[ge]);let Le=Math.ceil(l/v);return{orders:s,total:l,isLoading:b,error:m,page:y,pageSize:v,hasNextPage:y<Le-1,hasPreviousPage:y>0,setPage:w,setPageSize:ae=>{f(ae),w(0)},status:p,setStatus:ae=>{h(ae),w(0)},statusIn:de,setStatusIn:ae=>{x(ae&&ae.length>0?ae:void 0),w(0)},customerEmail:S,setCustomerEmail:ae=>{T(ae),w(0)},from:U,setFrom:ae=>{W(ae),w(0)},to:D,setTo:ae=>{H(ae),w(0)},showAbandoned:z,setShowAbandoned:ae=>{Q(ae),w(0)},unfulfilledOnly:j,setUnfulfilledOnly:ae=>{A(ae),w(0)},clearFilters:()=>{h(void 0),x(void 0),Q(!1),A(!1),T(""),W(""),H(""),w(0)},refetch:ge}}var Fe=te(require("react"),1),me=require("lucide-react");var hr=te(require("react"),1),jt=te(require("@radix-ui/react-popover"),1);var ps=require("react/jsx-runtime"),xr=jt.Root,Nr=jt.Trigger;var Es=hr.forwardRef(({className:e,align:t="end",sideOffset:a=4,...s},r)=>(0,ps.jsx)(jt.Portal,{children:(0,ps.jsx)("div",{className:"aifeatures-admin",children:(0,ps.jsx)(jt.Content,{ref:r,align:t,sideOffset:a,className:V("af-z-50 af-w-64 af-rounded-md af-border af-bg-popover af-p-1 af-text-popover-foreground af-shadow-md af-outline-none",e),...s})})}));Es.displayName=jt.Content.displayName;var Qa=require("react/jsx-runtime"),Cn={pending:"Pending",paid:"Paid",failed:"Failed",expired:"Expired",refunded:"Refunded",partially_refunded:"Partially refunded",disputed:"Disputed"};function Tn(e){return Cn[e]??e}function wr(e){switch(e){case"paid":return"success";case"pending":return"secondary";case"refunded":case"partially_refunded":return"warning";case"disputed":return"destructive";default:return"outline"}}function ya({status:e,className:t}){return(0,Qa.jsx)($,{variant:wr(e),className:t,children:Tn(e)})}function ua({status:e}){return(0,Qa.jsx)(ya,{status:e})}function _a({order:e,className:t}){return e.line_items.some(a=>a.collectShippingAddress===!0)?typeof e.merchant_fulfilled_at=="string"?(0,Qa.jsx)($,{variant:"outline",className:t,children:"Shipped"}):e.merchant_fulfilled_at===null&&(e.status==="paid"||e.status==="partially_refunded"||e.status==="disputed")?(0,Qa.jsx)($,{variant:"warning",className:t,children:"Needs shipping"}):null:null}function ra(e){return typeof e.order_number=="number"?`#${e.order_number}`:`#${e.id.slice(0,8)}`}var o=require("react/jsx-runtime"),Cr="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",ha="af-text-xs af-text-muted-foreground";function Os(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function kn(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function Tr(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return String(e)}}var gs=24,_n=["paid","partially_refunded"],Dn=["paid","partially_refunded","disputed"],En=/^[A-Za-z0-9 _-]*$/,Sr=100;function Is(e,t,a,s,r){let l=_n.includes(s);return t==="sent"?{label:"Sent",variant:"success",detail:null}:t==="sending"?{label:"Sending",variant:"secondary",detail:"Going out right now."}:t==="failed"?l?{label:"Retrying",variant:"warning",detail:`This did not go out yet. We have used ${a} of ${gs} tries and keep retrying automatically.`}:{label:"Retries paused",variant:"warning",detail:`This did not go out (${a} of ${gs} tries used). Nothing is being retried while the order is ${s.replace(/_/g," ")} \u2014 retries resume by themselves if the order goes back to paid.`}:t==="failed_final"?a<gs&&e==="merchant"?{label:"No recipient set",variant:"destructive",detail:"Nobody was told about this sale: this site has no notification email address set. Add one in your site settings and future sales will notify you."}:a<gs&&e==="buyer"&&!r?{label:"No buyer email",variant:"destructive",detail:"This order has no buyer email address, so no receipt could be sent. Send the buyer their details another way."}:{label:"Not delivered",variant:"destructive",detail:`We stopped after ${a} attempt${a===1?"":"s"}. This will not be retried automatically \u2014 it needs you to act.`}:t==="suppressed"?{label:"Held back",variant:"outline",detail:`We deliberately did not send this ${s==="refunded"?"because the order was refunded":s==="disputed"?"because the order is disputed":"because the order was not in a sendable state"}. It sends automatically if the order becomes sellable again.`}:t==="none"?{label:"Not sent yet",variant:"secondary",detail:null}:{label:String(t),variant:"secondary",detail:null}}function Pr({title:e,recipient:t,status:a,attempts:s,side:r,orderStatus:l,action:c}){let b=Is(r,a,s,l,t);return(0,o.jsxs)("div",{children:[(0,o.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,o.jsxs)("div",{className:"af-min-w-0",children:[(0,o.jsx)("div",{className:"af-text-sm",children:e}),t&&(0,o.jsx)("div",{className:"af-text-xs af-text-muted-foreground af-break-all",children:t})]}),(0,o.jsx)($,{variant:b.variant,className:"af-shrink-0",children:b.label})]}),b.detail&&(0,o.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mt-1",children:b.detail}),c&&(0,o.jsx)("div",{className:"af-mt-2",children:c})]})}function Rr(e){let t=[];typeof e.name=="string"&&e.name&&t.push(e.name);let a=kn(e.address)?e.address:null;if(a){for(let r of["line1","line2"]){let l=a[r];typeof l=="string"&&l&&t.push(l)}let s=["city","state","postal_code"].map(r=>a[r]).filter(r=>typeof r=="string"&&!!r).join(", ");s&&t.push(s),typeof a.country=="string"&&a.country&&t.push(a.country)}return t.length>0?t:Object.entries(e).map(([s,r])=>`${s}: ${Tr(r)}`)}function On(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function As({value:e,label:t,children:a,className:s,size:r="icon"}){let[l,c]=Fe.useState(!1);if(!On())return null;let b=async()=>{try{await navigator.clipboard.writeText(e),c(!0),setTimeout(()=>c(!1),1500)}catch{}},u=l?(0,o.jsx)(me.Check,{className:"af-h-3 af-w-3"}):(0,o.jsx)(me.Copy,{className:"af-h-3 af-w-3"});return r==="icon"?(0,o.jsx)(N,{variant:"ghost",size:"icon",className:V("af-h-6 af-w-6 af-shrink-0",s),onClick:b,title:t,"aria-label":t,children:u}):(0,o.jsxs)(N,{variant:"outline",size:"sm",className:s,onClick:b,"aria-label":t,children:[l?(0,o.jsx)(me.Check,{className:"af-h-3.5 af-w-3.5 af-mr-1"}):(0,o.jsx)(me.ClipboardCopy,{className:"af-h-3.5 af-w-3.5 af-mr-1"}),l?"Copied":a]})}function Ls({label:e,value:t}){return(0,o.jsxs)("div",{children:[(0,o.jsx)("div",{className:ha,children:e}),(0,o.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,o.jsx)("code",{className:"af-text-xs af-font-mono af-break-all",children:t}),(0,o.jsx)(As,{value:t,label:`Copy ${e}`})]})]})}function Ln({src:e}){let[t,a]=Fe.useState(!1);return!e||t?(0,o.jsx)("div",{"aria-hidden":"true",className:"af-flex af-h-10 af-w-10 af-shrink-0 af-items-center af-justify-center af-rounded-md af-border af-bg-muted",children:(0,o.jsx)(me.Package,{className:"af-h-4 af-w-4 af-text-muted-foreground"})}):(0,o.jsx)("img",{src:e,alt:"","aria-hidden":"true",onError:()=>a(!0),className:"af-h-10 af-w-10 af-shrink-0 af-rounded-md af-border af-object-cover"})}function ma({title:e,action:t,children:a,className:s}){return(0,o.jsxs)("section",{className:V("af-rounded-lg af-border af-p-4",s),children:[(0,o.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-gap-2",children:[(0,o.jsx)("h4",{className:Cr,children:e}),t]}),(0,o.jsx)("div",{className:"af-mt-3",children:a})]})}function pa({label:e,emphasis:t,children:a}){return(0,o.jsxs)("div",{className:V("af-flex af-items-baseline af-justify-between af-gap-6",t&&"af-mt-2 af-border-t af-pt-2"),children:[(0,o.jsx)("span",{className:t?"af-text-base af-font-semibold":ha,children:e}),(0,o.jsx)("span",{className:V("af-text-right af-tabular-nums",t?"af-text-base af-font-semibold":"af-text-sm"),children:a})]})}function Bs({onSelect:e,disabled:t,icon:a,children:s}){return(0,o.jsxs)(N,{variant:"ghost",size:"sm",className:"af-w-full af-justify-start af-font-normal",onClick:e,disabled:t,children:[(0,o.jsx)("span",{className:"af-mr-2 af-flex af-h-4 af-w-4 af-items-center af-justify-center",children:a}),s]})}function Bn(e){let t=[];if(t.push({key:"placed",text:"Order placed",at:e.created_at}),e.paid_at&&t.push({key:"paid",text:`Payment received \u2014 ${M(St(e),e.currency)}`,at:e.paid_at}),e.buyer_email_status!=="none"){let a=Is("buyer",e.buyer_email_status,e.buyer_email_attempts,e.status,e.customer_email);t.push({key:"buyer-email",text:e.buyer_email_status==="sent"?`Receipt emailed${e.customer_email?` to ${e.customer_email}`:""}`:`Buyer receipt \u2014 ${a.label.toLowerCase()}`,at:null,tone:a.variant==="destructive"?"destructive":a.variant==="warning"?"warning":void 0})}if(e.merchant_email_status!=="none"){let a=Is("merchant",e.merchant_email_status,e.merchant_email_attempts,e.status,null);t.push({key:"merchant-email",text:e.merchant_email_status==="sent"?"You were notified of this sale":`Sale alert to you \u2014 ${a.label.toLowerCase()}`,at:null,tone:a.variant==="destructive"?"destructive":a.variant==="warning"?"warning":void 0})}return typeof e.merchant_fulfilled_at=="string"&&t.push({key:"shipped",text:e.tracking_number?`Marked shipped \u2014 tracking ${e.tracking_number}`:"Marked shipped",at:e.merchant_fulfilled_at}),e.amount_refunded>0&&t.push({key:"refunded",text:`Refunded ${M(e.amount_refunded,e.currency)}`,at:e.refunded_at,tone:"warning"}),e.status==="disputed"&&t.push({key:"disputed",text:"Payment disputed by the buyer",at:null,tone:"destructive"}),t}function Ya({order:e,onBack:t,onRefund:a,onOrderUpdated:s,allowFulfillment:r=!1,onPrevOrder:l,onNextOrder:c,hasPrevOrder:b=!1,hasNextOrder:u=!1}){let{api:m}=ee(),[n,y]=Fe.useState(e),[w,v]=Fe.useState(null),[f,p]=Fe.useState(null),[h,S]=Fe.useState(!1),[T,U]=Fe.useState(!1),[W,D]=Fe.useState(!1),[H,z]=Fe.useState(!1),[Q,j]=Fe.useState(""),[A,de]=Fe.useState(!1),[x,oe]=Fe.useState(null),[we,he]=Fe.useState(!1),[ge,Le]=Fe.useState(""),[ae,ve]=Fe.useState([]),[_e,F]=Fe.useState(!1),[be,At]=Fe.useState(!1),[Ft,Be]=Fe.useState(null);Fe.useEffect(()=>{y(e)},[e]);let vt=C=>{y(C),s?.(C)},bt=Object.entries(n.metadata??{}),Ve=St(n)-n.amount_refunded,Ut=(n.status==="paid"||n.status==="partially_refunded")&&Ve>0,P=C=>(typeof C.unitAmount=="number"?C.unitAmount:0)*(typeof C.quantity=="number"&&C.quantity>0?C.quantity:1),O=Math.max(0,(n.shipping_total??0)-(n.shipping_refunded??0)),ue=ae.reduce((C,Se)=>C+P(n.line_items[Se]??{}),0)+(_e?O:0),Je=n.tax_total??0,Ze=n.automatic_tax&&n.tax_behavior==="exclusive"&&Je>0&&n.amount_total+(n.shipping_total??0)>0?Math.min(Math.max(0,Je-(n.tax_refunded??0)),Math.round(Je*ue/(n.amount_total+(n.shipping_total??0)))):0,Qe=ue+Ze,Ke=ae.length>0||_e,Vt=Ke?Qe:Pt(ge,n.currency),Ra=Lt(n.currency)?"1":"0.01",Ca=async()=>{try{S(!0),v(null),p(null),await m.resendOrderReceipt(n.id);let C=await m.getOrder(n.id);vt(C),p("Receipt email queued.")}catch(C){v(C instanceof Error?C.message:"Failed to resend receipt")}finally{S(!1)}},J=async()=>{try{U(!0),v(null),p(null);let C=await m.resetOrderDownloads(n.id);vt(C),D(!1),p("Download count reset.")}catch(C){v(C instanceof Error?C.message:"Failed to reset downloads")}finally{U(!1)}},Pe=n.line_items.some(C=>C.collectShippingAddress===!0),Ee=typeof n.merchant_fulfilled_at=="string",ia=Dn.includes(n.status),Ht=Q.trim(),Ta=En.test(Ht)&&Ht.length<=Sr,yt=async C=>{try{de(!0),oe(null),p(null);let Se=await m.setOrderFulfillment(n.id,{fulfilled:C,...C&&Ht?{trackingNumber:Ht}:{}});vt(Se),j(""),p(C?"Marked as shipped.":"Marked as not shipped.")}catch(Se){Se instanceof Yt&&Se.status===409?oe(`This order is ${n.status.replace(/_/g," ")}, so it can't be marked here. Refresh to see its current state.`):oe(Se instanceof Error?Se.message:"Failed to update the order")}finally{de(!1)}},Ie=()=>{Be(null),Le(String(qe(Ve,n.currency))),ve([]),F(!1),he(!0)},je=C=>ve(Se=>Se.includes(C)?Se.filter(st=>st!==C):[...Se,C]),Oa=async(C,Se)=>{if(a)try{At(!0),Be(null),await a(n,C,Se);let st=await m.getOrder(n.id);vt(st),he(!1),p("Refund issued.")}catch(st){Be(st instanceof Error?st.message:"Refund failed")}finally{At(!1)}},I=async()=>{if(Ke){if(Qe<=0){Be("The lines you picked were charged nothing");return}if(Qe>Ve){Be(`That selection exceeds the refundable remainder (${M(Ve,n.currency)})`);return}await Oa(Qe,{...ae.length>0?{line_items:[...ae].sort((Se,st)=>Se-st)}:{},..._e?{shipping:!0}:{}});return}let C=Pt(ge,n.currency);if(C===null||C<=0){Be("Enter a valid refund amount");return}if(C>Ve){Be(`Amount exceeds the refundable remainder (${M(Ve,n.currency)})`);return}await Oa(C,{amount:C})},at=()=>Oa(Ve,{full:!0}),qs=n.fulfillment_type==="download",Ws=r&&Pe,Jr=Bn(n),Zr=n.shipping_address?Rr(n.shipping_address).join(`
|
|
4
|
-
`):"",Gs=n.shipping_total!=null,Ks=!!n.automatic_tax&&n.tax_behavior==="exclusive"&&(n.tax_total??0)>0,Qr=!(n.automatic_tax&&n.tax_behavior==="exclusive")&&(n.tax_total??0)>0,Yr=Gs||Ks,La=[];return La.push((0,o.jsx)(Bs,{icon:h?(0,o.jsx)(me.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,o.jsx)(me.Mail,{className:"af-h-4 af-w-4"}),disabled:h||n.fulfillment_status!=="fulfilled",onSelect:()=>{z(!1),Ca()},children:"Resend receipt"},"resend")),qs&&La.push((0,o.jsx)(Bs,{icon:(0,o.jsx)(me.RotateCcw,{className:"af-h-4 af-w-4"}),disabled:T,onSelect:()=>{z(!1),D(!0)},children:"Reset download count"},"reset")),Ut&&La.push((0,o.jsxs)("div",{children:[(0,o.jsx)(Bs,{icon:(0,o.jsx)(me.Undo2,{className:"af-h-4 af-w-4"}),disabled:!a,onSelect:()=>{z(!1),Ie()},children:"Refund\u2026"}),!a&&(0,o.jsx)("p",{className:"af-px-3 af-pb-1 af-text-xs af-text-muted-foreground",children:"Refunds are issued from your site dashboard."})]},"refund")),(0,o.jsxs)("div",{children:[(0,o.jsxs)("div",{className:"af-mb-6",children:[(0,o.jsxs)(N,{variant:"ghost",size:"sm",onClick:t,className:"af-mb-2 af--ml-2",children:[(0,o.jsx)(me.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Orders"]}),(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-start af-justify-between af-gap-3",children:[(0,o.jsxs)("div",{className:"af-min-w-0",children:[(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,o.jsx)("h3",{className:"af-text-xl af-font-semibold",children:ra(n)}),(0,o.jsx)(ya,{status:n.status}),r&&(0,o.jsx)(_a,{order:n}),n.is_test&&(0,o.jsx)($,{variant:"outline",children:"Test"})]}),(0,o.jsxs)("p",{className:"af-mt-1 af-text-sm af-text-muted-foreground",children:["Placed ",Os(n.created_at)]}),n.is_test&&(0,o.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Simulated test purchase \u2014 no payment was collected. Refunding it is simulated too."})]}),(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(l||c)&&(0,o.jsxs)("div",{className:"af-flex af-items-center",children:[(0,o.jsx)(N,{variant:"outline",size:"icon",className:"af-h-9 af-w-9 af-rounded-r-none",onClick:l,disabled:!b,"aria-label":"Previous order",children:(0,o.jsx)(me.ChevronLeft,{className:"af-h-4 af-w-4"})}),(0,o.jsx)(N,{variant:"outline",size:"icon",className:"af-h-9 af-w-9 af-rounded-l-none af--ml-px",onClick:c,disabled:!u,"aria-label":"Next order",children:(0,o.jsx)(me.ChevronRight,{className:"af-h-4 af-w-4"})})]}),Ws&&(Ee?(0,o.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,o.jsxs)("span",{className:"af-text-sm af-text-muted-foreground",children:["Shipped \u2713",n.merchant_fulfilled_at?` \xB7 ${Os(n.merchant_fulfilled_at)}`:""]}),ia&&(0,o.jsxs)(N,{variant:"ghost",size:"sm",onClick:()=>yt(!1),disabled:A,children:[A?(0,o.jsx)(me.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,o.jsx)(me.Undo2,{className:"af-h-4 af-w-4 af-mr-1"}),"Undo"]})]}):ia?(0,o.jsxs)("div",{className:"af-flex af-flex-nowrap af-items-center af-gap-2",children:[(0,o.jsx)(fe,{value:Q,onChange:C=>j(C.target.value),placeholder:"Tracking number (optional)",maxLength:Sr,className:"af-w-[190px] af-shrink-0","aria-label":"Tracking number (optional)",disabled:A}),(0,o.jsxs)(N,{variant:"default",className:"af-shrink-0",onClick:()=>yt(!0),disabled:A||!Ta,children:[A?(0,o.jsx)(me.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,o.jsx)(me.Truck,{className:"af-h-4 af-w-4 af-mr-1"}),"Mark as shipped"]})]}):null),La.length>0&&(0,o.jsxs)(xr,{open:H,onOpenChange:z,children:[(0,o.jsx)(Nr,{asChild:!0,children:(0,o.jsx)(N,{variant:"outline",size:"icon","aria-label":"More actions",children:(0,o.jsx)(me.MoreHorizontal,{className:"af-h-4 af-w-4"})})}),(0,o.jsx)(Es,{children:(0,o.jsx)("div",{className:"af-space-y-0.5",children:La})})]})]})]}),Ws&&!Ee&&!Ta&&(0,o.jsx)("p",{className:"af-mt-2 af-text-xs af-text-destructive",children:"Tracking numbers can use letters, numbers, spaces, dashes and underscores \u2014 up to 100 characters."}),x&&(0,o.jsx)("div",{className:"af-mt-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2 af-text-sm af-text-destructive",children:x}),f&&(0,o.jsx)("div",{className:"af-mt-2 af-rounded af-border af-border-green-200 af-bg-green-50 af-px-3 af-py-2 af-text-sm af-text-green-800",children:f}),w&&(0,o.jsx)("div",{className:"af-mt-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2 af-text-sm af-text-destructive",children:w})]}),(0,o.jsxs)("div",{className:"af-grid af-gap-6 lg:af-grid-cols-[1fr_320px]",children:[(0,o.jsxs)("div",{className:"af-space-y-6",children:[n.line_items.length>0&&(0,o.jsx)(ma,{title:"Items",children:(0,o.jsx)("ul",{className:"af-space-y-3",children:n.line_items.map((C,Se)=>{let st=typeof C.name=="string"&&C.name?C.name:"Item",Js=typeof C.quantity=="number"&&C.quantity>0?C.quantity:1,Zs=typeof C.unitAmount=="number";return(0,o.jsxs)("li",{className:"af-flex af-items-start af-gap-3",children:[(0,o.jsx)(Ln,{src:typeof C.imageUrl=="string"&&C.imageUrl?C.imageUrl:void 0}),(0,o.jsxs)("div",{className:"af-min-w-0 af-flex-1",children:[(0,o.jsxs)("div",{className:"af-text-sm af-font-medium",children:[st,typeof C.variantTitle=="string"&&C.variantTitle&&(0,o.jsxs)("span",{className:"af-font-normal af-text-muted-foreground",children:[" \u2014 ",C.variantTitle]})]}),(0,o.jsxs)("div",{className:ha,children:[Js," \xD7"," ",Zs?M(C.unitAmount,n.currency):"\u2014"]}),C.refunded===!0&&(0,o.jsx)($,{variant:"secondary",className:"af-mt-1",children:C.refundReason==="oversold"?"Sold out \u2014 refunded":"Refunded"})]}),(0,o.jsx)("div",{className:"af-shrink-0 af-text-sm af-tabular-nums",children:Zs?M(C.unitAmount*Js,n.currency):"\u2014"})]},Se)})})}),bt.length>0&&(0,o.jsx)(ma,{title:"Order details",children:(0,o.jsx)("dl",{className:"af-space-y-2",children:bt.map(([C,Se])=>(0,o.jsxs)("div",{children:[(0,o.jsx)("dt",{className:V(ha,"af-capitalize"),children:C.replace(/_/g," ")}),(0,o.jsx)("dd",{className:"af-text-sm af-break-words",children:Tr(Se)})]},C))})}),(0,o.jsx)(ma,{title:"Payment",children:(0,o.jsxs)("div",{className:"af-ml-auto af-w-full sm:af-max-w-xs",children:[Yr&&(0,o.jsx)(pa,{label:"Subtotal",children:M(n.amount_total,n.currency)}),Gs&&(0,o.jsx)(pa,{label:"Shipping",children:n.shipping_total===0?"Free":M(n.shipping_total??0,n.currency)}),Ks&&(0,o.jsx)(pa,{label:"Tax",children:M(n.tax_total??0,n.currency)}),(0,o.jsx)(pa,{label:"Total",emphasis:!0,children:M(St(n),n.currency)}),Qr&&(0,o.jsx)(pa,{label:"incl. tax",children:M(n.tax_total??0,n.currency)}),n.base_currency!=null&&n.base_amount_total!=null&&(0,o.jsxs)(pa,{label:"Your price",children:[M(n.base_amount_total,n.base_currency),(0,o.jsxs)("span",{className:"af-block af-text-xs af-text-muted-foreground",children:["buyer paid in ",n.currency.toUpperCase()]})]}),n.amount_refunded>0&&(0,o.jsxs)(o.Fragment,{children:[(0,o.jsxs)(pa,{label:"Refunded",children:["\u2212",M(n.amount_refunded,n.currency),(n.shipping_refunded??0)>0&&(0,o.jsxs)("span",{className:"af-block af-text-xs af-text-muted-foreground",children:["incl."," ",M(n.shipping_refunded??0,n.currency)," ","shipping"]})]}),(0,o.jsx)(pa,{label:"Net",children:(0,o.jsx)("span",{className:"af-font-medium",children:M(Math.max(0,St(n)-n.amount_refunded),n.currency)})})]})]})}),(0,o.jsx)(ma,{title:"Timeline",children:(0,o.jsx)("ol",{className:"af-space-y-3",children:Jr.map(C=>(0,o.jsxs)("li",{className:"af-flex af-gap-3",children:[(0,o.jsx)("span",{"aria-hidden":"true",className:V("af-mt-1.5 af-h-2 af-w-2 af-shrink-0 af-rounded-full",C.tone==="destructive"?"af-bg-destructive":C.tone==="warning"?"af-bg-yellow-500":"af-bg-muted-foreground/40")}),(0,o.jsxs)("div",{className:"af-min-w-0",children:[(0,o.jsx)("div",{className:"af-text-sm af-break-words",children:C.text}),C.at&&(0,o.jsx)("div",{className:"af-text-xs af-text-muted-foreground",children:Os(C.at)})]})]},C.key))})})]}),(0,o.jsxs)("div",{className:"af-space-y-6",children:[(0,o.jsx)(ma,{title:"Customer",children:(0,o.jsxs)("div",{className:"af-space-y-2",children:[(0,o.jsxs)("div",{children:[(0,o.jsx)("div",{className:ha,children:"Email"}),(0,o.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,o.jsx)("span",{className:"af-min-w-0 af-break-all af-text-sm",children:n.customer_email||(0,o.jsx)("span",{className:"af-italic af-text-muted-foreground",children:"Unknown"})}),n.customer_email&&(0,o.jsx)(As,{value:n.customer_email,label:"Copy email"})]})]}),n.customer_name&&(0,o.jsxs)("div",{children:[(0,o.jsx)("div",{className:ha,children:"Name"}),(0,o.jsx)("div",{className:"af-text-sm",children:n.customer_name})]}),n.customer&&(0,o.jsxs)("div",{children:[(0,o.jsx)("div",{className:ha,children:"Customer record"}),(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,o.jsx)("span",{className:"af-break-all af-text-sm",children:n.customer.name||n.customer.email}),(0,o.jsx)($,{variant:"secondary",children:"Linked"})]})]})]})}),n.shipping_address&&(0,o.jsx)(ma,{title:"Ship to",action:(0,o.jsx)(As,{value:Zr,label:"Copy address",size:"sm",children:"Copy address"}),children:(0,o.jsx)("address",{className:"af-space-y-0.5 af-text-sm af-not-italic",children:Rr(n.shipping_address).map((C,Se)=>(0,o.jsx)("div",{children:C},Se))})}),(0,o.jsx)(ma,{title:"Emails",children:(0,o.jsxs)("div",{className:"af-space-y-4",children:[(0,o.jsx)(Pr,{side:"buyer",title:"Receipt to buyer",recipient:n.customer_email,status:n.buyer_email_status,attempts:n.buyer_email_attempts,orderStatus:n.status,action:(0,o.jsxs)(N,{variant:"outline",size:"sm",onClick:Ca,disabled:h||n.fulfillment_status!=="fulfilled",children:[h?(0,o.jsx)(me.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,o.jsx)(me.Mail,{className:"af-h-4 af-w-4 af-mr-1"}),"Resend"]})}),(0,o.jsx)(Pr,{side:"merchant",title:"Sale alert to you",recipient:null,status:n.merchant_email_status,attempts:n.merchant_email_attempts,orderStatus:n.status})]})}),qs&&(0,o.jsx)(ma,{title:"Download",children:(0,o.jsxs)("div",{className:"af-space-y-2",children:[(0,o.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,o.jsx)(me.Download,{className:"af-h-4 af-w-4"}),(0,o.jsxs)("span",{children:["Downloaded ",n.download_count," time",n.download_count===1?"":"s"]})]}),n.download_url&&(0,o.jsx)("a",{href:n.download_url,target:"_blank",rel:"noreferrer",className:"af-inline-block af-text-sm af-underline af-underline-offset-2",children:"Buyer download link"})]})}),(0,o.jsxs)("details",{className:"af-rounded-lg af-border af-p-4",children:[(0,o.jsx)("summary",{className:V(Cr,"af-cursor-pointer af-select-none"),children:"Technical details"}),(0,o.jsxs)("div",{className:"af-mt-3 af-space-y-2",children:[(0,o.jsx)(Ls,{label:"Order ID",value:n.id}),n.stripe_checkout_session_id&&(0,o.jsx)(Ls,{label:"Checkout session",value:n.stripe_checkout_session_id}),n.stripe_payment_intent_id&&(0,o.jsx)(Ls,{label:"Payment",value:n.stripe_payment_intent_id})]})]})]})]}),(0,o.jsx)(zt,{open:W,onOpenChange:C=>{!C&&!T&&D(!1)},children:(0,o.jsxs)(kt,{children:[(0,o.jsxs)(_t,{children:[(0,o.jsx)(Et,{children:"Reset download count?"}),(0,o.jsx)(Ot,{children:"The buyer's download link stops working after it reaches the download limit. Resetting the count lets them download again."})]}),(0,o.jsxs)(Dt,{children:[(0,o.jsx)(N,{variant:"outline",onClick:()=>D(!1),disabled:T,children:"Cancel"}),(0,o.jsx)(N,{onClick:J,disabled:T,children:T?"Resetting...":"Reset"})]})]})}),(0,o.jsx)(zt,{open:we,onOpenChange:C=>{!C&&!be&&he(!1)},children:(0,o.jsxs)(kt,{children:[(0,o.jsxs)(_t,{children:[(0,o.jsx)(Et,{children:"Refund order"}),(0,o.jsxs)(Ot,{children:["Refund up to ",M(Ve,n.currency)," to the buyer's original payment method. Stripe's processing fee is not returned."]})]}),(0,o.jsxs)("div",{className:"af-space-y-4",children:[(n.line_items.length>0||O>0)&&(0,o.jsxs)("div",{className:"af-space-y-2",children:[(0,o.jsx)(le,{children:"Items"}),(0,o.jsxs)("div",{className:"af-space-y-2",children:[n.line_items.map((C,Se)=>{let st=C.refunded===!0;return(0,o.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,o.jsx)("input",{type:"checkbox",checked:ae.includes(Se),onChange:()=>je(Se),disabled:be||st,className:"af-h-4 af-w-4"}),(0,o.jsxs)("span",{className:st?"af-text-muted-foreground af-line-through":void 0,children:[typeof C.name=="string"&&C.name?C.name:"Item",typeof C.variantTitle=="string"&&C.variantTitle?` \u2014 ${C.variantTitle}`:"",typeof C.quantity=="number"&&C.quantity>1?` \xD7 ${C.quantity}`:""]}),(0,o.jsx)("span",{className:"af-ml-auto af-text-muted-foreground",children:st?"Refunded":M(P(C),n.currency)})]},Se)}),O>0&&(0,o.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,o.jsx)("input",{type:"checkbox",checked:_e,onChange:C=>F(C.target.checked),disabled:be,className:"af-h-4 af-w-4"}),(0,o.jsx)("span",{children:"Shipping"}),(0,o.jsx)("span",{className:"af-ml-auto af-text-muted-foreground",children:M(O,n.currency)})]})]})]}),(0,o.jsxs)("div",{className:"af-space-y-2",children:[(0,o.jsxs)(le,{htmlFor:"refund-amount",children:["Amount (",n.currency.toUpperCase(),")"]}),(0,o.jsx)(fe,{id:"refund-amount",type:"number",min:"0",step:Ra,value:Ke?String(qe(Qe,n.currency)):ge,onChange:C=>Le(C.target.value),disabled:be||Ke}),Ke&&(0,o.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Priced from the order. Clear the ticks above to refund a different amount."})]})]}),Ft&&(0,o.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:Ft}),(0,o.jsxs)(Dt,{children:[(0,o.jsx)(N,{variant:"outline",onClick:()=>he(!1),disabled:be,children:"Cancel"}),(0,o.jsx)(N,{variant:"outline",onClick:at,disabled:be,children:"Refund everything"}),(0,o.jsx)(N,{variant:"destructive",onClick:I,disabled:be,children:be?"Refunding...":Vt!==null&&Vt>0?`Refund ${M(Vt,n.currency)}`:"Refund"})]})]})})]})}var vs=te(require("react"),1),bs=require("lucide-react");var ze=require("react/jsx-runtime");function Bt({title:e,onBackToWebsite:t,hideBackButton:a=!1,children:s,className:r}){let l=vs.useRef(null);return vs.useEffect(()=>{l.current?.focus()},[]),(0,ze.jsxs)("div",{className:V("af-bg-background",r),children:[(0,ze.jsxs)("div",{className:"af-sticky af-top-0 af-z-10 lg:af-static af-flex af-items-center af-justify-between af-gap-2 af-border-b af-bg-background af-px-4 af-py-2",children:[(0,ze.jsx)("h2",{ref:l,tabIndex:-1,"aria-live":"polite",className:"af-text-lg af-font-semibold focus:af-outline-none",children:e}),t&&!a&&(0,ze.jsxs)(N,{type:"button",variant:"outline",size:"sm",onClick:t,children:[(0,ze.jsx)(bs.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to website"]})]}),(0,ze.jsx)("div",{className:"af-p-4",children:s})]})}function K({className:e}){return(0,ze.jsx)("div",{"aria-hidden":"true",className:V("af-h-4 af-w-16 af-animate-pulse af-rounded af-bg-muted",e)})}function et({label:e,children:t,detail:a,valueClassName:s}){return(0,ze.jsxs)("div",{className:"af-rounded-md af-border af-px-3 af-py-2",children:[(0,ze.jsx)("div",{className:"af-text-xs af-text-muted-foreground",children:e}),(0,ze.jsx)("div",{className:V("af-mt-1 af-text-lg af-font-semibold",s),children:t}),a&&(0,ze.jsx)("div",{className:"af-mt-0.5 af-text-xs af-text-muted-foreground",children:a})]})}function ut({message:e,onRetry:t}){return(0,ze.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,ze.jsx)("span",{children:e}),(0,ze.jsxs)(N,{type:"button",variant:"ghost",size:"sm",onClick:t,children:[(0,ze.jsx)(bs.RefreshCw,{className:"af-h-4 af-w-4 af-mr-1"}),"Try again"]})]})}function qt({icon:e,children:t}){return(0,ze.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[e,(0,ze.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:t})]})}var d=require("react/jsx-runtime"),In={needs_shipping:"Needs shipping",all:"All",paid:"Paid",refunds:"Refunds & disputes"},kr={needs_shipping:{unfulfilledOnly:!0},all:{unfulfilledOnly:!1},paid:{statusIn:["paid","partially_refunded"],unfulfilledOnly:!1},refunds:{statusIn:["refunded","partially_refunded","disputed"],unfulfilledOnly:!1}};function _r(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function Dr(e){let t=Array.isArray(e.line_items)?e.line_items:[],a=t[0];if(!a)return"\u2014";let s=typeof a.name=="string"&&a.name?a.name:"Item",r=typeof a.quantity=="number"&&a.quantity>0?a.quantity:1,l=r>1?`${s} \xD7${r}`:s;return t.length>1?`${l} +${t.length-1} more`:l}function ys(e){return e.customer?.name||e.customer_name||null}function xa({onRefund:e,className:t,refreshKey:a,initialOrderId:s,showFulfillment:r=!1,emptyMessage:l,onDetailOpenChange:c}){let{api:b}=ee(),u=Za({refreshKey:a}),{orders:m,total:n,isLoading:y,error:w,page:v,pageSize:f,hasNextPage:p,hasPreviousPage:h,setPage:S,setPageSize:T,customerEmail:U,setCustomerEmail:W,setStatusIn:D,from:H,setFrom:z,to:Q,setTo:j,showAbandoned:A,setShowAbandoned:de,setUnfulfilledOnly:x,refetch:oe}=u,[we,he]=We.useState("list"),[ge,Le]=We.useState(null),[ae,ve]=We.useState(""),[_e,F]=We.useState(""),[be,At]=We.useState(r?"needs_shipping":"all"),[Ft,Be]=We.useState(!1),[vt,bt]=We.useState(!!s),[Ve,Ut]=We.useState(null);We.useEffect(()=>{if(!s)return;let I=!1;return bt(!0),Ut(null),b.getOrder(s).then(at=>{I||(Le(at),he("detail"))}).catch(()=>{I||Ut("Couldn't open that order \u2014 showing all orders.")}).finally(()=>{I||bt(!1)}),()=>{I=!0}},[b,s]);let P=We.useCallback(I=>{At(I);let at=kr[I];D(at.statusIn),x(at.unfulfilledOnly)},[D,x]);We.useEffect(()=>{r&&x(!0)},[]);let O=We.useRef(!r);We.useEffect(()=>{O.current||y||(O.current=!0,m.length===0&&P("all"))},[y,m.length,P]);let ue=we==="detail"&&!!ge;We.useEffect(()=>{c?.(ue)},[ue,c]);let Je=(H?1:0)+(Q?1:0)+(A?1:0),Ze=Je>0||!!U||!!_e,Qe=!!H&&!!Q&&H>Q,Ke=I=>{Le(I),he("detail")},Vt=()=>{he("list"),Le(null),oe()},Ra=I=>{let at=I.trim();if(!at){F(""),U&&W("");return}if(at.startsWith("#")||/^\d+$/.test(at)){F(at),U&&W("");return}F(""),W(at)},Ca=()=>{ve(""),F(""),W(""),z(""),j(""),de(!1),Be(!1)},J=_e.replace(/^#/,"").toLowerCase(),Pe=J?m.filter(I=>String(I.order_number??"")===J||I.id.toLowerCase().startsWith(J)):m,Ee=ge?m.findIndex(I=>I.id===ge.id):-1,ia=Ee>0,Ht=Ee>=0&&Ee<m.length-1;if(vt)return(0,d.jsx)("div",{className:t,children:(0,d.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,d.jsx)(mt.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})})});if(we==="detail"&&ge)return(0,d.jsx)("div",{className:t,children:(0,d.jsx)(Ya,{order:ge,onBack:Vt,onRefund:e,onOrderUpdated:I=>Le(I),allowFulfillment:r,onPrevOrder:ia?()=>Le(m[Ee-1]):void 0,onNextOrder:Ht?()=>Le(m[Ee+1]):void 0,hasPrevOrder:ia,hasNextOrder:Ht},ge.id)});let yt=(0,d.jsxs)("div",{className:"af-mb-4 af-space-y-3",children:[Ve&&(0,d.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:Ve}),(0,d.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,d.jsx)("div",{role:"group","aria-label":"Order views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:(r?["needs_shipping","all","paid","refunds"]:["all","paid","refunds"]).map(I=>(0,d.jsx)("button",{type:"button","aria-pressed":be===I,onClick:()=>P(I),className:V("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",be===I?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:In[I]},I))}),(0,d.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,d.jsx)(mt.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,d.jsx)(fe,{type:"text",value:ae,onChange:I=>ve(I.target.value),onBlur:()=>Ra(ae),onKeyDown:I=>{I.key==="Enter"&&(I.preventDefault(),Ra(ae))},placeholder:"Search email, name, or order #","aria-label":"Search orders by email, name, or order number",className:"af-pl-8"})]}),(0,d.jsxs)(N,{type:"button",variant:Je>0?"secondary":"ghost",size:"sm",onClick:()=>Be(I=>!I),"aria-expanded":Ft,children:[(0,d.jsx)(mt.SlidersHorizontal,{className:"af-h-4 af-w-4 af-mr-1"}),"Filters",Je>0&&(0,d.jsx)($,{variant:"secondary",className:"af-ml-2",children:Je})]}),Ze&&(0,d.jsx)(N,{type:"button",variant:"ghost",size:"sm",onClick:Ca,children:"Clear filters"})]}),(Ft||!!H||!!Q)&&(0,d.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2 af-rounded-md af-border af-px-3 af-py-2",children:[(0,d.jsx)(le,{htmlFor:"orders-from",className:"af-text-xs af-text-muted-foreground",children:"From"}),(0,d.jsx)(fe,{id:"orders-from",type:"date",value:H,max:Q||void 0,onChange:I=>z(I.target.value),className:"af-w-[170px]"}),(0,d.jsx)(le,{htmlFor:"orders-to",className:"af-text-xs af-text-muted-foreground",children:"To"}),(0,d.jsx)(fe,{id:"orders-to",type:"date",value:Q,min:H||void 0,onChange:I=>j(I.target.value),className:"af-w-[170px]"}),!kr[be].statusIn&&(0,d.jsx)(N,{type:"button",variant:A?"secondary":"ghost",size:"sm",onClick:()=>de(!A),"aria-pressed":A,children:A?"Hide abandoned":"Show abandoned"})]}),Qe&&(0,d.jsx)("p",{className:"af-text-xs af-text-destructive",children:"The end date is before the start date, so nothing can match."})]});if(y)return(0,d.jsxs)("div",{className:t,children:[yt,(0,d.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(I=>(0,d.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,d.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,d.jsx)(K,{className:"af-h-4 af-w-16"}),(0,d.jsx)(K,{className:"af-h-4 af-w-14"})]}),(0,d.jsx)(K,{className:"af-mt-2 af-h-3 af-w-40"}),(0,d.jsx)(K,{className:"af-mt-2 af-h-3 af-w-24"})]},I))}),(0,d.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,d.jsxs)(Re,{children:[(0,d.jsx)(Ce,{children:(0,d.jsxs)(ie,{children:[(0,d.jsx)(L,{children:"Order"}),(0,d.jsx)(L,{children:"Date"}),(0,d.jsx)(L,{children:"Customer"}),(0,d.jsx)(L,{children:"Items"}),(0,d.jsx)(L,{className:"af-text-right",children:"Total"}),(0,d.jsx)(L,{children:"Status"}),(0,d.jsx)(L,{className:"af-w-8"})]})}),(0,d.jsx)(Te,{"aria-hidden":"true",children:[0,1,2,3,4].map(I=>(0,d.jsxs)(ie,{children:[(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-h-4 af-w-14"})}),(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-h-4 af-w-40"})}),(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-h-4 af-w-32"})}),(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-ml-auto af-h-4 af-w-16"})}),(0,d.jsx)(B,{children:(0,d.jsx)(K,{className:"af-h-5 af-w-16"})}),(0,d.jsx)(B,{})]},I))})]})}),(0,d.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading orders"})]});if(w)return(0,d.jsxs)("div",{className:t,children:[yt,(0,d.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-gap-2 af-py-8",children:[(0,d.jsx)("div",{className:"af-text-sm af-text-muted-foreground",children:"Couldn't load orders."}),(0,d.jsx)(N,{type:"button",variant:"ghost",size:"sm",onClick:()=>oe(),children:"Try again"})]})]});let Ie=be!=="all";if(Pe.length===0&&v===0)return(0,d.jsxs)("div",{className:t,children:[(Ze||Ie)&&yt,(0,d.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[(0,d.jsx)(mt.Receipt,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,d.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:Ze||Ie?"No orders match these filters.":l??(0,d.jsxs)(d.Fragment,{children:["No orders yet.",(0,d.jsx)("br",{}),"Orders will appear here when someone buys from your website."]})}),!Ze&&!Ie&&(0,d.jsx)(N,{type:"button",variant:"ghost",size:"sm",className:"af-mt-2",onClick:()=>de(!0),children:"Show abandoned checkouts"})]})]});let je=v*f+1,Oa=Math.min((v+1)*f,n);return(0,d.jsxs)("div",{className:t,children:[yt,(0,d.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:Pe.map(I=>(0,d.jsxs)("button",{type:"button",onClick:()=>Ke(I),className:"af-block af-w-full af-rounded-md af-border af-p-3 af-text-left hover:af-bg-muted/50",children:[(0,d.jsxs)("div",{className:"af-flex af-items-baseline af-justify-between af-gap-2",children:[(0,d.jsx)("span",{className:"af-font-medium",children:ra(I)}),(0,d.jsx)("span",{className:"af-shrink-0 af-font-medium af-tabular-nums",children:M(St(I),I.currency)})]}),(0,d.jsxs)("div",{className:"af-mt-1 af-min-w-0",children:[(0,d.jsxs)("div",{className:"af-truncate af-text-sm",children:[I.customer_email||(0,d.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"}),ys(I)&&(0,d.jsxs)("span",{className:"af-text-muted-foreground",children:[" \xB7 ",ys(I)]})]}),(0,d.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:Dr(I)}),I.amount_refunded>0&&(0,d.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["\u2212",M(I.amount_refunded,I.currency)," refunded"]})]}),(0,d.jsxs)("div",{className:"af-mt-2 af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,d.jsx)(ya,{status:I.status}),I.is_test&&(0,d.jsx)($,{variant:"outline",children:"Test"}),r&&(0,d.jsx)(_a,{order:I}),(0,d.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:_r(I.created_at)})]})]},I.id))}),(0,d.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,d.jsxs)(Re,{children:[(0,d.jsx)(Ce,{children:(0,d.jsxs)(ie,{children:[(0,d.jsx)(L,{children:"Order"}),(0,d.jsx)(L,{children:"Date"}),(0,d.jsx)(L,{children:"Customer"}),(0,d.jsx)(L,{children:"Items"}),(0,d.jsx)(L,{className:"af-text-right",children:"Total"}),(0,d.jsx)(L,{children:"Status"}),(0,d.jsx)(L,{className:"af-w-8"})]})}),(0,d.jsx)(Te,{children:Pe.map(I=>(0,d.jsxs)(ie,{onClick:()=>Ke(I),className:"af-cursor-pointer focus-within:af-bg-muted/50",children:[(0,d.jsxs)(B,{className:"af-whitespace-nowrap af-font-medium",children:[(0,d.jsx)("button",{type:"button",onClick:at=>{at.stopPropagation(),Ke(I)},"aria-label":`Open order ${ra(I)}`,className:"af-rounded-sm af-text-left hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:ra(I)}),I.is_test&&(0,d.jsx)($,{variant:"outline",className:"af-ml-2",children:"Test"})]}),(0,d.jsx)(B,{className:"af-whitespace-nowrap af-text-muted-foreground",children:_r(I.created_at)}),(0,d.jsxs)(B,{className:"af-max-w-[220px]",children:[(0,d.jsx)("div",{className:"af-truncate",children:I.customer_email||(0,d.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"})}),ys(I)&&(0,d.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:ys(I)})]}),(0,d.jsx)(B,{className:"af-max-w-[220px]",children:(0,d.jsx)("div",{className:"af-truncate af-text-muted-foreground",children:Dr(I)})}),(0,d.jsxs)(B,{className:"af-whitespace-nowrap af-text-right",children:[(0,d.jsx)("div",{className:"af-font-medium af-tabular-nums",children:M(St(I),I.currency)}),I.amount_refunded>0&&(0,d.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["\u2212",M(I.amount_refunded,I.currency)," ","refunded"]})]}),(0,d.jsx)(B,{children:(0,d.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,d.jsx)(ya,{status:I.status}),r&&(0,d.jsx)(_a,{order:I})]})}),(0,d.jsx)(B,{className:"af-w-8 af-text-right",children:(0,d.jsx)(mt.ChevronRight,{"aria-hidden":"true",className:"af-h-4 af-w-4 af-text-muted-foreground/50"})})]},I.id))})]})}),(0,d.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,d.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,d.jsx)("span",{children:J?`${Pe.length} matching this page`:`Showing ${je}-${Oa} of ${n}`}),(0,d.jsxs)(ht,{value:String(f),onValueChange:I=>T(Number(I)),children:[(0,d.jsx)(ot,{className:"af-w-[70px] af-h-8",children:(0,d.jsx)(xt,{})}),(0,d.jsxs)(lt,{children:[(0,d.jsx)(ke,{value:"10",children:"10"}),(0,d.jsx)(ke,{value:"25",children:"25"}),(0,d.jsx)(ke,{value:"50",children:"50"}),(0,d.jsx)(ke,{value:"100",children:"100"})]})]}),(0,d.jsx)("span",{children:"per page"})]}),(0,d.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,d.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>S(v-1),disabled:!h,children:[(0,d.jsx)(mt.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,d.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>S(v+1),disabled:!p,children:["Next",(0,d.jsx)(mt.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var xs=te(require("react"),1),Wt=require("lucide-react");var nt=te(require("react"),1);function Xa({initialPageSize:e=25}={}){let{api:t}=ee(),[a,s]=nt.useState([]),[r,l]=nt.useState(0),[c,b]=nt.useState(!0),[u,m]=nt.useState(null),[n,y]=nt.useState(0),[w,v]=nt.useState(e),[f,p]=nt.useState(""),h=nt.useRef(0),S=nt.useCallback(async()=>{let U=++h.current;try{b(!0),m(null);let W={limit:w,offset:n*w};f.trim()&&(W.email=f.trim());let D=await t.getCustomers(W);if(U!==h.current)return;s(D.customers),l(D.total)}catch(W){if(U!==h.current)return;m(W instanceof Error?W:new Error("Failed to fetch customers"))}finally{U===h.current&&b(!1)}},[t,n,w,f]);nt.useEffect(()=>{S()},[S]);let T=Math.ceil(r/w);return{customers:a,total:r,isLoading:c,error:u,page:n,pageSize:w,hasNextPage:n<T-1,hasPreviousPage:n>0,setPage:y,setPageSize:U=>{v(U),y(0)},email:f,setEmail:U=>{p(U),y(0)},refetch:S}}var Na=te(require("react"),1),hs=require("lucide-react");var Y=require("react/jsx-runtime");function Er(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function An(e){let t=[];e.name&&t.push(e.name),t.push(e.line1),e.line2&&t.push(e.line2);let a=[e.city,e.state,e.postal_code].filter(Boolean).join(", ");return a&&t.push(a),e.country&&t.push(e.country),t}function es({customer:e,onBack:t,className:a}){let{api:s}=ee(),[r,l]=Na.useState(null),[c,b]=Na.useState([]),[u,m]=Na.useState(!0),[n,y]=Na.useState(null);Na.useEffect(()=>{let f=!1;return m(!0),y(null),Promise.all([s.getCustomer(e.id),s.getOrders({customerEmail:e.email,excludeStatus:["pending","expired"],limit:100})]).then(([p,h])=>{f||(l(p),b(h.orders))}).catch(p=>{f||y(p instanceof Error?p.message:"Failed to load customer")}).finally(()=>{f||m(!1)}),()=>{f=!0}},[s,e.id,e.email]);let w=r?.addresses??[],v=r?.order_count??c.length;return(0,Y.jsxs)("div",{className:a,children:[(0,Y.jsxs)("div",{className:"af-mb-6",children:[(0,Y.jsxs)(N,{variant:"ghost",size:"sm",onClick:t,className:"af-mb-2 af--ml-2",children:[(0,Y.jsx)(hs.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Customers"]}),(0,Y.jsx)("h3",{className:"af-text-lg af-font-semibold",children:e.name||e.email}),(0,Y.jsxs)("p",{className:"af-text-sm af-text-muted-foreground",children:["Customer since ",Er(e.created_at)]})]}),u?(0,Y.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,Y.jsx)(hs.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})}):n?(0,Y.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:n}):(0,Y.jsxs)("div",{className:"af-space-y-6",children:[(0,Y.jsxs)("div",{children:[(0,Y.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Contact"}),(0,Y.jsxs)("div",{className:"af-space-y-1 af-text-sm",children:[(0,Y.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,Y.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Email"}),(0,Y.jsx)("span",{children:e.email})]}),e.name&&(0,Y.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,Y.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Name"}),(0,Y.jsx)("span",{children:e.name})]}),e.phone&&(0,Y.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,Y.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Phone"}),(0,Y.jsx)("span",{children:e.phone})]}),(0,Y.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,Y.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Orders"}),(0,Y.jsx)("span",{children:v})]})]})]}),w.length>0&&(0,Y.jsxs)("div",{children:[(0,Y.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Addresses"}),(0,Y.jsx)("div",{className:"af-flex af-flex-wrap af-gap-3",children:w.map(f=>(0,Y.jsxs)("div",{className:"af-text-sm af-bg-muted af-rounded af-p-3 af-space-y-0.5",children:[f.is_default&&(0,Y.jsx)($,{variant:"secondary",className:"af-mb-1",children:"Default"}),An(f).map((p,h)=>(0,Y.jsx)("div",{children:p},h))]},f.id))})]}),(0,Y.jsxs)("div",{children:[(0,Y.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Orders"}),c.length===0?(0,Y.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No orders."}):(0,Y.jsxs)(Re,{children:[(0,Y.jsx)(Ce,{children:(0,Y.jsxs)(ie,{children:[(0,Y.jsx)(L,{children:"Date"}),(0,Y.jsx)(L,{children:"Amount"}),(0,Y.jsx)(L,{children:"Status"})]})}),(0,Y.jsx)(Te,{children:c.map(f=>(0,Y.jsxs)(ie,{children:[(0,Y.jsx)(B,{className:"af-text-muted-foreground",children:Er(f.created_at)}),(0,Y.jsx)(B,{children:M(St(f),f.currency)}),(0,Y.jsx)(B,{children:(0,Y.jsx)(ua,{status:f.status})})]},f.id))})]})]})]})]})}var q=require("react/jsx-runtime");function Fn(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function ts({className:e}){let{customers:t,total:a,isLoading:s,error:r,page:l,pageSize:c,hasNextPage:b,hasPreviousPage:u,setPage:m,setPageSize:n,email:y,setEmail:w,refetch:v}=Xa(),[f,p]=xs.useState("list"),[h,S]=xs.useState(null),[T,U]=xs.useState(""),W=j=>{S(j),p("detail")},D=()=>{p("list"),S(null),v()};if(f==="detail"&&h)return(0,q.jsx)("div",{className:e,children:(0,q.jsx)(es,{customer:h,onBack:D})});let H=(0,q.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-mb-4",children:[(0,q.jsx)(fe,{type:"email",value:T,onChange:j=>U(j.target.value),placeholder:"Search by email",className:"af-w-[260px]",onKeyDown:j=>{j.key==="Enter"&&(j.preventDefault(),w(T.trim()))}}),(0,q.jsxs)(N,{type:"button",variant:"outline",size:"sm",onClick:()=>w(T.trim()),children:[(0,q.jsx)(Wt.Search,{className:"af-h-4 af-w-4 af-mr-1"}),"Search"]}),y&&(0,q.jsx)(N,{type:"button",variant:"ghost",size:"sm",onClick:()=>{U(""),w("")},children:"Clear"})]});if(s)return(0,q.jsxs)("div",{className:e,children:[H,(0,q.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,q.jsx)("div",{className:"af-text-muted-foreground",children:"Loading customers..."})})]});if(r)return(0,q.jsxs)("div",{className:e,children:[H,(0,q.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,q.jsxs)("div",{className:"af-text-destructive",children:["Error: ",r.message]})})]});if(t.length===0&&l===0)return(0,q.jsxs)("div",{className:e,children:[y&&H,(0,q.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[(0,q.jsx)(Wt.Users,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,q.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:y?"No customers match that email.":(0,q.jsxs)(q.Fragment,{children:["No customers yet.",(0,q.jsx)("br",{}),"Customers appear here after their first purchase."]})})]})]});let z=l*c+1,Q=Math.min((l+1)*c,a);return(0,q.jsxs)("div",{className:e,children:[H,(0,q.jsxs)(Re,{children:[(0,q.jsx)(Ce,{children:(0,q.jsxs)(ie,{children:[(0,q.jsx)(L,{children:"Email"}),(0,q.jsx)(L,{children:"Name"}),(0,q.jsx)(L,{children:"Customer since"}),(0,q.jsx)(L,{className:"af-text-right",children:"Actions"})]})}),(0,q.jsx)(Te,{children:t.map(j=>(0,q.jsxs)(ie,{children:[(0,q.jsx)(B,{className:"af-font-medium",children:j.email}),(0,q.jsx)(B,{children:j.name||(0,q.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"-"})}),(0,q.jsx)(B,{className:"af-text-muted-foreground",children:Fn(j.created_at)}),(0,q.jsx)(B,{className:"af-text-right",children:(0,q.jsx)(N,{variant:"ghost",size:"icon",onClick:()=>W(j),title:"View customer",children:(0,q.jsx)(Wt.Eye,{className:"af-h-4 af-w-4"})})})]},j.id))})]}),(0,q.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-px-2 af-py-4",children:[(0,q.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,q.jsxs)("span",{children:["Showing ",z,"-",Q," of ",a]}),(0,q.jsxs)(ht,{value:String(c),onValueChange:j=>n(Number(j)),children:[(0,q.jsx)(ot,{className:"af-w-[70px] af-h-8",children:(0,q.jsx)(xt,{})}),(0,q.jsxs)(lt,{children:[(0,q.jsx)(ke,{value:"10",children:"10"}),(0,q.jsx)(ke,{value:"25",children:"25"}),(0,q.jsx)(ke,{value:"50",children:"50"}),(0,q.jsx)(ke,{value:"100",children:"100"})]})]}),(0,q.jsx)("span",{children:"per page"})]}),(0,q.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,q.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>m(l-1),disabled:!u,children:[(0,q.jsx)(Wt.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,q.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>m(l+1),disabled:!b,children:["Next",(0,q.jsx)(Wt.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var Or=te(require("react"),1),pt=require("lucide-react");var Gt=te(require("react"),1);function wa(){let{api:e}=ee(),[t,a]=Gt.useState(null),[s,r]=Gt.useState(!0),[l,c]=Gt.useState(null),b=Gt.useRef(0),u=Gt.useCallback(async()=>{let m=++b.current;try{r(!0),c(null);let n=await e.getPaymentsStatus();if(m!==b.current)return;a(n)}catch(n){if(m!==b.current)return;c(n instanceof Error?n:new Error("Failed to fetch payments status"))}finally{m===b.current&&r(!1)}},[e]);return Gt.useEffect(()=>{u()},[u]),{status:t,isLoading:s,error:l,refetch:u}}var re=require("react/jsx-runtime");function as({onConnectStripe:e,onUpgradePlan:t,className:a}){let{status:s,isLoading:r,error:l,refetch:c}=wa(),[b,u]=Or.useState(!1),m=async()=>{u(!0);try{await c()}finally{u(!1)}};return r&&!s?(0,re.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,re.jsx)("div",{className:"af-text-muted-foreground",children:"Loading payment status..."})}):l||!s?(0,re.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,re.jsxs)("div",{className:"af-text-destructive",children:["Error: ",l?.message||"Failed to load payment status"]})}):s.status==="active"?(0,re.jsx)("div",{className:a,children:(0,re.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-green-50 af-border af-border-green-200 af-rounded-lg af-p-4",children:[(0,re.jsx)(pt.CheckCircle2,{className:"af-h-5 af-w-5 af-text-green-600 af-mt-0.5"}),(0,re.jsxs)("div",{children:[(0,re.jsx)("p",{className:"af-text-sm af-font-medium af-text-green-800",children:"Payments are active"}),(0,re.jsxs)("p",{className:"af-text-sm af-text-green-800",children:["Your Stripe account is connected and your site can accept payments in ",s.currency.toUpperCase(),". Payouts go directly to your Stripe account."]})]})]})}):(0,re.jsxs)("div",{className:`af-space-y-4 ${a||""}`,children:[s.reason==="needs_stripe"&&(0,re.jsxs)("div",{className:"af-space-y-3",children:[(0,re.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-muted af-rounded-lg af-p-4",children:[(0,re.jsx)(pt.CreditCard,{className:"af-h-5 af-w-5 af-text-muted-foreground af-mt-0.5"}),(0,re.jsxs)("div",{children:[(0,re.jsx)("p",{className:"af-text-sm af-font-medium",children:"Set up payments to start selling"}),(0,re.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"It only takes a few minutes. You'll enter your details with Stripe, who handle the payments, and money from sales goes straight to your own bank."})]})]}),e?(0,re.jsxs)(N,{onClick:e,children:[(0,re.jsx)(pt.CreditCard,{className:"af-h-4 af-w-4 af-mr-1"}),s.stripe_account_connected?"Finish setting up":"Set up payments"]}):(0,re.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Set up payments from your site dashboard to start selling."})]}),s.reason==="needs_paid_plan"&&(0,re.jsxs)("div",{className:"af-space-y-3",children:[(0,re.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-muted af-rounded-lg af-p-4",children:[(0,re.jsx)(pt.AlertTriangle,{className:"af-h-5 af-w-5 af-text-muted-foreground af-mt-0.5"}),(0,re.jsxs)("div",{children:[(0,re.jsx)("p",{className:"af-text-sm af-font-medium",children:"Selling requires a paid plan"}),(0,re.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Your products are saved as drafts. Upgrade to a paid plan to start accepting payments on your site."})]})]}),t?(0,re.jsx)(N,{onClick:t,children:"Upgrade plan"}):(0,re.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Upgrade your plan from your site dashboard to start selling."})]}),s.reason==="account_disabled"&&(0,re.jsxs)("div",{className:"af-space-y-3",children:[(0,re.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-yellow-50 af-border af-border-yellow-200 af-rounded-lg af-p-4",children:[(0,re.jsx)(pt.AlertTriangle,{className:"af-h-5 af-w-5 af-text-yellow-600 af-mt-0.5"}),(0,re.jsxs)("div",{children:[(0,re.jsx)("p",{className:"af-text-sm af-font-medium af-text-yellow-800",children:"Payments are paused"}),s.stripe_account_connected?(0,re.jsx)("p",{className:"af-text-sm af-text-yellow-800",children:"Your Stripe account can't currently accept charges. Check your Stripe account for outstanding requirements or notifications \u2014 once Stripe re-enables charges, payments resume here automatically."}):(0,re.jsx)("p",{className:"af-text-sm af-text-yellow-800",children:"Payments are currently paused for this site. If you think this is a mistake, get in touch with support and we'll look into it with you."})]})]}),(0,re.jsxs)("div",{className:"af-flex af-gap-2",children:[e&&s.stripe_account_connected&&(0,re.jsxs)(N,{onClick:e,children:[(0,re.jsx)(pt.CreditCard,{className:"af-h-4 af-w-4 af-mr-1"}),"Finish setting up"]}),(0,re.jsxs)(N,{variant:"outline",onClick:m,disabled:b,children:[b?(0,re.jsx)(pt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,re.jsx)(pt.RefreshCw,{className:"af-h-4 af-w-4 af-mr-1"}),"Refresh status"]})]})]})]})}var tt=te(require("react"),1);function Fs(e,t,a){return e.map(s=>s.id===t?{...s,...a}:s)}function Sa({includeStats:e=!1,refreshKey:t}={}){let{api:a}=ee(),[s,r]=tt.useState([]),[l,c]=tt.useState(!0),[b,u]=tt.useState(null),m=tt.useRef(0),n=tt.useCallback(async()=>{let h=++m.current;try{c(!0),u(null);let S=await a.getProducts(e?{include:"stats"}:{});if(h!==m.current)return;r(S)}catch(S){if(h!==m.current)return;u(S instanceof Error?S:new Error("Failed to fetch products"))}finally{h===m.current&&c(!1)}},[a,e,t]);tt.useEffect(()=>{n()},[n]);let y=tt.useCallback(async h=>{let S=await a.createProduct(h);return r(T=>[S,...T]),S},[a]),w=tt.useCallback(async(h,S)=>{let T=await a.updateProduct(h,S);return r(U=>Fs(U,h,T)),T},[a]),v=tt.useCallback(async h=>{await a.deleteProduct(h),r(S=>S.filter(T=>T.id!==h))},[a]),f=tt.useCallback(async(h,S)=>{let T=await a.attachProductFile(h,S);return r(U=>Fs(U,h,T)),T},[a]),p=tt.useCallback(async(h,S)=>{let T=await a.attachProductFileFromUrl(h,S);return r(U=>Fs(U,h,T)),T},[a]);return{products:s,isLoading:l,error:b,refetch:n,createProduct:y,updateProduct:w,deleteProduct:v,attachProductFile:f,attachProductFileFromUrl:p}}var pe=require("react/jsx-runtime");function Us({className:e,defaultTab:t="products",onConnectStripe:a,onRefund:s,onUpgradePlan:r}){let[l,c]=Ns.useState(t),[b,u]=Ns.useState("list"),[m,n]=Ns.useState(null),y=Sa(),{status:w,error:v}=wa(),f=w?.currency,p=U=>{n(U),u("edit")},h=()=>{n(null),u("create")},S=()=>{u("list"),n(null)},T=U=>{n(U),u("edit")};return(0,pe.jsxs)("div",{className:e,children:[(0,pe.jsxs)("div",{className:"af-mb-6",children:[(0,pe.jsx)("h2",{className:"af-text-lg af-font-semibold",children:"Payments"}),(0,pe.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Manage your products, orders, and payment setup."})]}),(0,pe.jsxs)(ds,{value:l,onValueChange:U=>c(U),children:[(0,pe.jsxs)(Ba,{className:"af-mb-4",children:[(0,pe.jsxs)(Xt,{value:"products",className:"af-gap-2",children:[(0,pe.jsx)(Kt.Package,{className:"af-h-4 af-w-4"}),"Products"]}),(0,pe.jsxs)(Xt,{value:"orders",className:"af-gap-2",children:[(0,pe.jsx)(Kt.Receipt,{className:"af-h-4 af-w-4"}),"Orders"]}),(0,pe.jsxs)(Xt,{value:"customers",className:"af-gap-2",children:[(0,pe.jsx)(Kt.Users,{className:"af-h-4 af-w-4"}),"Customers"]}),(0,pe.jsxs)(Xt,{value:"setup",className:"af-gap-2",children:[(0,pe.jsx)(Kt.Settings,{className:"af-h-4 af-w-4"}),"Setup"]})]}),(0,pe.jsx)(ea,{value:"products",children:b==="list"?(0,pe.jsx)(Ga,{productsState:y,onSelectProduct:p,onCreateProduct:h}):(0,pe.jsxs)("div",{children:[(0,pe.jsxs)("div",{className:"af-mb-6",children:[(0,pe.jsxs)(N,{variant:"ghost",size:"sm",onClick:S,className:"af-mb-2 af--ml-2",children:[(0,pe.jsx)(Kt.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Products"]}),(0,pe.jsx)("h3",{className:"af-text-lg af-font-semibold",children:b==="create"?"New Product":m?.name})]}),b==="create"&&!f?(0,pe.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:v?(0,pe.jsxs)("div",{className:"af-text-destructive",children:["Error: ",v.message]}):(0,pe.jsx)("div",{className:"af-text-muted-foreground",children:"Loading..."})}):(0,pe.jsx)(Ja,{productsState:y,product:b==="edit"?m:null,currency:f??"usd",onSaved:T},b==="edit"?m?.id??"edit":"new")]})}),(0,pe.jsx)(ea,{value:"orders",children:(0,pe.jsx)(xa,{onRefund:s})}),(0,pe.jsx)(ea,{value:"customers",children:(0,pe.jsx)(ts,{})}),(0,pe.jsx)(ea,{value:"setup",children:(0,pe.jsx)(as,{onConnectStripe:a,onUpgradePlan:r})})]})]})}var Br=te(require("react"),1);var Jt=te(require("react"),1);function ss({refreshKey:e}={}){let{api:t}=ee(),[a,s]=Jt.useState(null),[r,l]=Jt.useState(!0),[c,b]=Jt.useState(null),u=Jt.useRef(0),m=Jt.useCallback(async()=>{let n=++u.current;try{l(!0),b(null);let y=await t.getBusinessSummary({tzOffsetMinutes:-new Date().getTimezoneOffset()});if(n!==u.current)return;s(y)}catch(y){if(n!==u.current)return;b(y instanceof Error?y:new Error("Failed to fetch summary"))}finally{n===u.current&&l(!1)}},[t,e]);return Jt.useEffect(()=>{m()},[m]),{summary:a,isLoading:r,error:c,refetch:m}}var Ne=require("react/jsx-runtime");function Lr(e,t){if(e.settled.complete)return{headline:M(e.settled.gross,e.settled.currency),extra:[]};let a=e.totals[0];return a?{headline:M(a.gross,a.currency),extra:e.totals.slice(1).map(s=>`+ ${M(s.gross,s.currency)} in ${s.currency.toUpperCase()}`)}:{headline:M(0,t),extra:[]}}function Vs({refreshKey:e,initialOrderId:t,isOwner:a=!1,onRefund:s,onBackToWebsite:r,className:l}){let{summary:c,isLoading:b,error:u,refetch:m}=ss({refreshKey:e}),[n,y]=Br.useState(!1),w=(0,Ne.jsx)("div",{className:"af-mb-6",children:u&&!c?(0,Ne.jsx)(ut,{message:"Couldn't load your revenue summary.",onRetry:()=>m()}):(0,Ne.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-3 sm:af-grid-cols-3",children:b||!c?(0,Ne.jsxs)(Ne.Fragment,{children:[(0,Ne.jsx)(et,{label:"This week",children:(0,Ne.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,Ne.jsx)(et,{label:"Today",children:(0,Ne.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,Ne.jsx)(et,{label:"Needs attention",children:(0,Ne.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,Ne.jsx)(Ne.Fragment,{children:(()=>{let v=Lr(c.revenue.d7,c.currency),f=Lr(c.revenue.today,c.currency);return(0,Ne.jsxs)(Ne.Fragment,{children:[(0,Ne.jsx)(et,{label:"This week",detail:v.extra.length>0?(0,Ne.jsx)("div",{children:v.extra.map(p=>(0,Ne.jsx)("div",{children:p},p))}):void 0,children:v.headline}),(0,Ne.jsx)(et,{label:"Today",detail:f.extra.length>0?(0,Ne.jsx)("div",{children:f.extra.map(p=>(0,Ne.jsx)("div",{children:p},p))}):void 0,children:f.headline}),(0,Ne.jsx)(et,{label:"Needs attention",valueClassName:c.needs_attention.count>0?"af-text-2xl af-font-semibold af-tabular-nums af-text-destructive":"af-text-2xl af-font-semibold af-tabular-nums af-text-muted-foreground",detail:c.needs_attention.count===0?"All clear":c.needs_attention.items[0]?.title??void 0,children:c.needs_attention.count})]})})()})})});return(0,Ne.jsxs)(Bt,{title:"Orders",onBackToWebsite:r,hideBackButton:n,className:l,children:[w,(0,Ne.jsx)(xa,{refreshKey:e,initialOrderId:t,onDetailOpenChange:y,showFulfillment:!0,onRefund:a?s:void 0,emptyMessage:"Orders will appear here \u2014 your store is live."})]})}var na=te(require("react"),1),Da=require("lucide-react");var Zt=te(require("react"),1),Ts=require("lucide-react");var Ge=require("react/jsx-runtime"),Ir="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",ga="af-text-xs af-text-muted-foreground",ws="af-text-sm af-font-medium af-tabular-nums",Un=3;function Vn(e){let t=e.pricing,a=t?.currency??e.currency;if(t?.custom_amount??e.allow_custom_amount){let u=e.min_amount??t?.min_amount??null,m=e.max_amount??t?.max_amount??null;return u!==null&&m!==null?{text:`Buyer chooses (${M(u,a)} \u2013 ${M(m,a)})`,compareAt:null}:{text:"Buyer chooses",compareAt:null}}let r=t?.min_amount??e.unit_amount,l=t?.max_amount??e.unit_amount;if(r===null||l===null)return{text:"Varies by variant",compareAt:null};let c=r===l?M(r,a):`${M(r,a)} \u2013 ${M(l,a)}`,b=t?.compare_at_amount??null;return{text:c,compareAt:b!==null&&b>l?M(b,a):null}}function Ss({product:e,className:t}){let a=Vn(e);return(0,Ge.jsxs)("div",{className:t,children:[(0,Ge.jsx)("span",{className:ws,children:a.text}),a.compareAt&&(0,Ge.jsx)("span",{className:"af-ml-2 af-text-xs af-text-muted-foreground af-line-through af-tabular-nums",children:a.compareAt})]})}function Ps({product:e}){let t=e.inventory;return t?t.tracked?t.out_of_stock?(0,Ge.jsx)($,{variant:"destructive",children:"Out of stock"}):t.low_stock?(0,Ge.jsxs)($,{variant:"warning",children:["Low stock \xB7 ",t.available]}):t.allow_backorder?(0,Ge.jsx)($,{variant:"secondary",children:"Backorder"}):t.available===null?null:(0,Ge.jsxs)($,{variant:"secondary",children:["In stock \xB7 ",t.available]}):null:e.stats?.low_stock?(0,Ge.jsx)($,{variant:"warning",children:"Low stock"}):null}function Rs(e){switch(e.fulfillment.type){case"download":return"Digital download";case"redirect":return"Redirect";default:return e.collect_shipping_address?"Ships":null}}function Cs(e){return e.map(t=>M(t.amount,t.currency)).join(" + ")}function Ar(e){return e.allow_custom_amount?e.min_amount!==null&&e.max_amount!==null?`Buyer chooses (${M(e.min_amount,e.currency)} \u2013 ${M(e.max_amount,e.currency)})`:"Buyer chooses":e.unit_amount===null?"\u2014":M(e.unit_amount,e.currency)}function Fr({variant:e}){let t=e.inventory;if(!t||!t.tracked)return(0,Ge.jsx)("span",{className:"af-text-muted-foreground",children:"Not tracked"});let a=t.available??0;return t.allow_backorder?(0,Ge.jsxs)("span",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,Ge.jsx)("span",{className:"af-tabular-nums",children:Math.max(0,a)}),(0,Ge.jsx)($,{variant:"secondary",children:a<0?`${Math.abs(a)} on backorder`:"Backorder"})]}):a<=0?(0,Ge.jsx)($,{variant:"destructive",children:"Out of stock"}):(0,Ge.jsxs)("span",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,Ge.jsx)("span",{className:"af-tabular-nums",children:a}),a<=Un&&(0,Ge.jsx)($,{variant:"warning",children:"Low"})]})}var E=require("react/jsx-runtime");function rs({title:e,children:t,className:a}){return(0,E.jsxs)("section",{className:V("af-rounded-lg af-border af-p-4",a),children:[(0,E.jsx)("h4",{className:Ir,children:e}),(0,E.jsx)("div",{className:"af-mt-3",children:t})]})}function Ur({src:e,className:t}){let[a,s]=Zt.useState(!1);return Zt.useEffect(()=>s(!1),[e]),!e||a?(0,E.jsx)("div",{"aria-hidden":"true",className:V("af-flex af-items-center af-justify-center af-bg-muted",t),children:(0,E.jsx)(Ts.ImageOff,{className:"af-h-8 af-w-8 af-text-muted-foreground"})}):(0,E.jsx)("img",{src:e,alt:"",loading:"lazy",onError:()=>s(!0),className:V("af-object-cover",t)})}function Vr({product:e,showStats:t=!1,onBack:a}){let{api:s}=ee(),[r,l]=Zt.useState(null),[c,b]=Zt.useState(!0),[u,m]=Zt.useState(null),[n,y]=Zt.useState(0),[w,v]=Zt.useState(0);Zt.useEffect(()=>{let D=!1;return b(!0),m(null),s.getProduct(e.id).then(H=>{D||l(H)}).catch(H=>{D||m(H instanceof Error?H:new Error("Failed to load product"))}).finally(()=>{D||b(!1)}),()=>{D=!0}},[s,e.id,n]);let f=r?{...e,...r}:e,p=f.images.length>0?f.images:e.images,h=p[w]??p[0],S=Rs(f),T=t?f.stats:void 0,U=r?.variants??[],W=U.length>1;return(0,E.jsxs)("div",{children:[(0,E.jsxs)("div",{className:"af-mb-6",children:[(0,E.jsxs)(N,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,E.jsx)(Ts.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Products"]}),(0,E.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,E.jsx)("h3",{className:"af-text-xl af-font-semibold af-break-words",children:f.name}),(0,E.jsx)($,{variant:f.active?"success":"secondary",children:f.active?"Active":"Draft"}),(0,E.jsx)(Ps,{product:f})]}),(0,E.jsx)(Ss,{product:f,className:"af-mt-1"}),!f.active&&(0,E.jsx)("p",{className:"af-mt-1 af-text-xs af-text-muted-foreground",children:"Drafts are hidden from your website and can't be bought."})]}),(0,E.jsxs)("div",{className:"af-grid af-gap-6 lg:af-grid-cols-[280px_1fr]",children:[(0,E.jsxs)("div",{className:"af-space-y-2",children:[(0,E.jsx)(Ur,{src:h,className:"af-h-48 af-w-full af-rounded-md af-border"}),p.length>1&&(0,E.jsx)("div",{className:"af-flex af-flex-wrap af-gap-2",children:p.map((D,H)=>(0,E.jsx)("button",{type:"button",onClick:()=>v(H),"aria-label":`Show photo ${H+1} of ${p.length}`,"aria-pressed":H===w,className:V("af-rounded-md af-border focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",H===w?"af-ring-2 af-ring-ring":"af-opacity-80 hover:af-opacity-100"),children:(0,E.jsx)(Ur,{src:D,className:"af-h-12 af-w-12 af-rounded-md"})},`${D}-${H}`))})]}),(0,E.jsxs)("div",{className:"af-space-y-6",children:[f.description&&(0,E.jsx)(rs,{title:"Description",children:(0,E.jsx)("p",{className:"af-whitespace-pre-wrap af-break-words af-text-sm",children:f.description})}),c?(0,E.jsxs)(rs,{title:"Variants",children:[(0,E.jsx)("div",{className:"af-space-y-2","aria-hidden":"true",children:[0,1,2].map(D=>(0,E.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-gap-4",children:[(0,E.jsx)(K,{className:"af-h-4 af-w-28"}),(0,E.jsx)(K,{className:"af-h-4 af-w-20"}),(0,E.jsx)(K,{className:"af-h-4 af-w-16"})]},D))}),(0,E.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading product details"})]}):u?(0,E.jsx)(ut,{message:"Couldn't load this product's variants.",onRetry:()=>y(D=>D+1)}):W?(0,E.jsx)(rs,{title:`Variants \xB7 ${U.length}`,children:(0,E.jsx)("div",{className:"af-overflow-x-auto",children:(0,E.jsxs)(Re,{children:[(0,E.jsx)(Ce,{children:(0,E.jsxs)(ie,{children:[(0,E.jsx)(L,{children:"Variant"}),(0,E.jsx)(L,{children:"SKU"}),(0,E.jsx)(L,{className:"af-text-right",children:"Price"}),(0,E.jsx)(L,{children:"Stock"})]})}),(0,E.jsx)(Te,{children:U.map(D=>(0,E.jsxs)(ie,{children:[(0,E.jsxs)(B,{className:"af-font-medium",children:[D.title||D.option_values.map(H=>H.value).join(" / ")||"\u2014",!D.active&&(0,E.jsx)($,{variant:"secondary",className:"af-ml-2",children:"Hidden"})]}),(0,E.jsx)(B,{className:"af-text-muted-foreground",children:D.sku?(0,E.jsx)("code",{className:"af-font-mono af-text-xs af-break-all",children:D.sku}):"\u2014"}),(0,E.jsxs)(B,{className:"af-whitespace-nowrap af-text-right",children:[(0,E.jsx)("div",{className:ws,children:Ar(D)}),D.compare_at_amount!==null&&D.unit_amount!==null&&D.compare_at_amount>D.unit_amount&&(0,E.jsx)("div",{className:"af-text-xs af-text-muted-foreground af-line-through af-tabular-nums",children:M(D.compare_at_amount,D.currency)})]}),(0,E.jsx)(B,{className:"af-text-sm",children:(0,E.jsx)(Fr,{variant:D})})]},D.id))})]})})}):null,(0,E.jsx)(rs,{title:"Details",children:(0,E.jsxs)("dl",{className:"af-space-y-2",children:[(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Delivery"}),(0,E.jsx)("dd",{className:"af-text-sm",children:S==="Digital download"?f.fulfillment.filename??"Digital download":S??"Nothing to deliver"})]}),f.fulfillment.type==="redirect"&&f.fulfillment.url&&(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Sends buyers to"}),(0,E.jsx)("dd",{className:"af-break-all af-text-sm",children:f.fulfillment.url})]}),(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Shipping address"}),(0,E.jsx)("dd",{className:"af-text-sm",children:f.collect_shipping_address?"Collected at checkout":"Not collected"})]}),(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Most one buyer can order"}),(0,E.jsx)("dd",{className:"af-text-sm af-tabular-nums",children:f.max_quantity})]})]})}),T&&(0,E.jsx)(rs,{title:"Last 30 days",children:(0,E.jsxs)("dl",{className:"af-grid af-grid-cols-3 af-gap-3",children:[(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Units sold"}),(0,E.jsx)("dd",{className:"af-text-sm af-font-medium af-tabular-nums",children:T.units_sold})]}),(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Orders"}),(0,E.jsx)("dd",{className:"af-text-sm af-font-medium af-tabular-nums",children:T.orders})]}),(0,E.jsxs)("div",{children:[(0,E.jsx)("dt",{className:ga,children:"Gross"}),(0,E.jsx)("dd",{className:ws,children:T.gross.length>0?Cs(T.gross):"\u2014"})]})]})}),(0,E.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Ask the AI to change anything here \u2014 price, stock, photos, wording."})]})]})]})}var k=require("react/jsx-runtime"),Hn={all:"All",active:"Active",draft:"Draft",low_stock:"Low stock",out_of_stock:"Out of stock"},$n=["all","active","draft","low_stock","out_of_stock"];function Hr(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`,a=new Date(t).getTime();return Number.isNaN(a)?0:a}function $r(e){return e.inventory?.low_stock??e.stats?.low_stock??!1}function Mn(e){return e.inventory?.out_of_stock??!1}function zn(e,t){switch(t){case"active":return e.active;case"draft":return!e.active;case"low_stock":return $r(e);case"out_of_stock":return Mn(e);default:return!0}}function jn({product:e,showStats:t,onOpen:a}){let s=e.images[0],r=t?e.stats:void 0,l=Rs(e),c=e.variant_count??0;return(0,k.jsxs)("button",{type:"button",onClick:a,"aria-label":`Open ${e.name}`,className:"af-block af-w-full af-overflow-hidden af-rounded-md af-border af-text-left af-transition-colors hover:af-bg-muted/50 focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:[s?(0,k.jsx)("img",{src:s,alt:"",loading:"lazy",className:"af-h-36 af-w-full af-object-cover"}):(0,k.jsx)("div",{"aria-hidden":"true",className:"af-flex af-h-36 af-w-full af-items-center af-justify-center af-bg-muted",children:(0,k.jsx)(Da.ImageOff,{className:"af-h-8 af-w-8 af-text-muted-foreground"})}),(0,k.jsxs)("div",{className:"af-space-y-2 af-p-3",children:[(0,k.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,k.jsxs)("div",{className:"af-min-w-0",children:[(0,k.jsx)("div",{className:"af-truncate af-text-sm af-font-medium af-text-foreground",children:e.name}),(0,k.jsx)(Ss,{product:e,className:"af-mt-0.5"})]}),!e.active&&(0,k.jsx)($,{variant:"secondary",className:"af-shrink-0",children:"Draft"})]}),(0,k.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,k.jsx)(Ps,{product:e}),c>1&&(0,k.jsxs)($,{variant:"outline",children:[c," variants"]}),l&&(0,k.jsx)($,{variant:"outline",children:l})]}),r&&(0,k.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:[r.units_sold," sold",r.gross.length>0?` \xB7 ${Cs(r.gross)}`:""," ",(0,k.jsx)("span",{className:"af-text-muted-foreground/70",children:"last 30 days"})]})]})]})}function qn(){return(0,k.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-4 sm:af-grid-cols-2 lg:af-grid-cols-3",children:[0,1,2,3,4,5].map(e=>(0,k.jsxs)("div",{"aria-hidden":"true",className:"af-overflow-hidden af-rounded-md af-border",children:[(0,k.jsx)("div",{className:"af-h-36 af-w-full af-animate-pulse af-bg-muted"}),(0,k.jsxs)("div",{className:"af-space-y-2 af-p-3",children:[(0,k.jsx)(K,{className:"af-w-32"}),(0,k.jsx)(K,{className:"af-w-16"}),(0,k.jsx)(K,{className:"af-h-5 af-w-24"})]})]},e))})}function Hs({refreshKey:e,showStats:t=!1,onBackToWebsite:a,className:s}){let{products:r,isLoading:l,error:c,refetch:b}=Sa({includeStats:t,refreshKey:e}),[u,m]=na.useState(null),[n,y]=na.useState("all"),[w,v]=na.useState(""),[f,p]=na.useState("newest"),h=na.useMemo(()=>{if(!t)return null;let A=null;for(let de of r)!de.stats||de.stats.units_sold===0||(!A||de.stats.units_sold>(A.stats?.units_sold??0))&&(A=de);return A},[r,t]),S=na.useMemo(()=>r.filter($r).length,[r]),T=w.trim().toLowerCase(),U=na.useMemo(()=>{let de=[...r.filter(x=>zn(x,n)&&(!T||x.name.toLowerCase().includes(T)))];return f==="name"?de.sort((x,oe)=>x.name.localeCompare(oe.name)):f==="best_selling"?de.sort((x,oe)=>(oe.stats?.units_sold??0)-(x.stats?.units_sold??0)):de.sort((x,oe)=>Hr(oe.created_at)-Hr(x.created_at)),de},[r,n,T,f]),W=n!=="all"||!!T,D=()=>{y("all"),v("")},H=l||c!==null&&r.length===0,z=(0,k.jsx)("div",{className:"af-mb-6",children:(0,k.jsx)("div",{className:V("af-grid af-grid-cols-1 af-gap-3",t?"sm:af-grid-cols-3":"sm:af-grid-cols-2"),children:H?(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(et,{label:"Products",children:(0,k.jsx)(K,{className:"af-h-6 af-w-10"})}),t?(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(et,{label:"Best seller",children:(0,k.jsx)(K,{className:"af-h-6 af-w-28"})}),(0,k.jsx)(et,{label:"Low stock",children:(0,k.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,k.jsx)(et,{label:"Active",children:(0,k.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(et,{label:"Products",children:r.length}),t?(0,k.jsxs)(k.Fragment,{children:[(0,k.jsx)(et,{label:"Best seller",valueClassName:"af-text-sm af-font-medium",detail:h?`${h.stats?.units_sold??0} sold, last 30 days`:void 0,children:h?(0,k.jsx)("span",{className:"af-block af-truncate af-text-sm af-font-medium",children:h.name}):(0,k.jsx)("span",{className:"af-block af-text-sm af-font-medium af-text-muted-foreground",children:"No sales yet"})}),(0,k.jsx)(et,{label:"Low stock",children:S})]}):(0,k.jsx)(et,{label:"Active",children:r.filter(A=>A.active).length})]})})}),Q=(0,k.jsxs)("div",{className:"af-mb-4 af-space-y-3",children:[(0,k.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,k.jsx)("div",{role:"group","aria-label":"Product views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:$n.map(A=>(0,k.jsx)("button",{type:"button","aria-pressed":n===A,onClick:()=>y(A),className:V("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",n===A?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:Hn[A]},A))}),(0,k.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,k.jsx)(Da.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,k.jsx)(fe,{type:"text",value:w,onChange:A=>v(A.target.value),placeholder:"Search products","aria-label":"Search products by name",className:"af-pl-8"})]}),(0,k.jsxs)(ht,{value:f,onValueChange:A=>p(A),children:[(0,k.jsx)(ot,{className:"af-h-9 af-w-[150px]","aria-label":"Sort products",children:(0,k.jsx)(xt,{})}),(0,k.jsxs)(lt,{children:[(0,k.jsx)(ke,{value:"newest",children:"Newest"}),(0,k.jsx)(ke,{value:"name",children:"Name A\u2013Z"}),t&&(0,k.jsx)(ke,{value:"best_selling",children:"Best selling"})]})]}),W&&(0,k.jsx)(N,{type:"button",variant:"ghost",size:"sm",onClick:D,children:"Clear filters"})]}),l&&r.length===0?(0,k.jsx)(K,{className:"af-h-4 af-w-28"}):(0,k.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:W?`${U.length} of ${r.length} products`:`${r.length} ${r.length===1?"product":"products"}${S>0?` \xB7 ${S} low stock`:""}`})]}),j;return u?j=(0,k.jsx)(Vr,{product:u,showStats:t,onBack:()=>{m(null),b()}},u.id):l&&r.length===0?j=(0,k.jsxs)(k.Fragment,{children:[Q,(0,k.jsx)(qn,{}),(0,k.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading products"})]}):c&&r.length===0?j=(0,k.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,k.jsx)(ut,{message:"Couldn't load your products.",onRetry:()=>b()})}):r.length===0?j=(0,k.jsxs)(qt,{icon:(0,k.jsx)(Da.Package,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:["No products yet.",(0,k.jsx)("br",{}),"Ask the AI to add one \u2014 try \u201Cadd a $25 t-shirt in S, M and L\u201D."]}):j=(0,k.jsxs)(k.Fragment,{children:[Q,c&&(0,k.jsx)("div",{className:"af-mb-4",children:(0,k.jsx)(ut,{message:"Couldn't refresh your products \u2014 showing the last version loaded.",onRetry:()=>b()})}),U.length===0?(0,k.jsxs)(qt,{children:["No products match these filters.",(0,k.jsx)("br",{}),(0,k.jsx)(N,{type:"button",variant:"ghost",size:"sm",className:"af-mt-2",onClick:D,children:"Show all products"})]}):(0,k.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-4 sm:af-grid-cols-2 lg:af-grid-cols-3",children:U.map(A=>(0,k.jsx)(jn,{product:A,showStats:t,onOpen:()=>m(A)},A.id))})]}),(0,k.jsxs)(Bt,{title:"Products",onBackToWebsite:a,hideBackButton:!!u,className:s,children:[!u&&z,j]})}var It=te(require("react"),1),Ue=require("lucide-react");var gt=te(require("react"),1);function ns({initialPageSize:e=25,refreshKey:t}={}){let{api:a}=ee(),[s,r]=gt.useState([]),[l,c]=gt.useState(!0),[b,u]=gt.useState(null),[m,n]=gt.useState(0),[y]=gt.useState(e),[w,v]=gt.useState(""),f=gt.useRef(0),p=gt.useCallback(async()=>{let h=++f.current;try{c(!0),u(null);let S={limit:y,offset:m*y};w.trim()&&(S.q=w.trim());let T=await a.getPeople(S);if(h!==f.current)return;r(T.people)}catch(S){if(h!==f.current)return;u(S instanceof Error?S:new Error("Failed to fetch people"))}finally{h===f.current&&c(!1)}},[a,m,y,w,t]);return gt.useEffect(()=>{p()},[p]),{people:s,isLoading:l,error:b,page:m,pageSize:y,hasNextPage:s.length===y,hasPreviousPage:m>0,setPage:n,q:w,setQ:h=>{v(h),n(0)},refetch:p}}var Qt=te(require("react"),1);function is(e,{refreshKey:t}={}){let{api:a}=ee(),[s,r]=Qt.useState(null),[l,c]=Qt.useState(e!==null),[b,u]=Qt.useState(null),m=Qt.useRef(0),n=Qt.useCallback(async()=>{if(e===null){r(null),c(!1),u(null);return}let y=++m.current;try{c(!0),u(null);let w=await a.getPerson(e);if(y!==m.current)return;r(w)}catch(w){if(y!==m.current)return;u(w instanceof Error?w:new Error("Failed to fetch person"))}finally{y===m.current&&c(!1)}},[a,e,t]);return Qt.useEffect(()=>{n()},[n]),{person:s,isLoading:l,error:b,refetch:n}}var i=require("react/jsx-runtime"),$s="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",Wn="af-text-xs af-text-muted-foreground";function Ea(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function Ms(e,t){let a=t?`Re: ${t}`:"Re: your message";return`mailto:${encodeURIComponent(e)}?subject=${encodeURIComponent(a)}`}function Gn(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function Kn({value:e,label:t}){let[a,s]=It.useState(!1);return Gn()?(0,i.jsxs)(N,{variant:"outline",size:"sm",onClick:async()=>{try{await navigator.clipboard.writeText(e),s(!0),setTimeout(()=>s(!1),1500)}catch{}},"aria-label":t,className:"af-shrink-0",children:[a?(0,i.jsx)(Ue.Check,{className:"af-h-3.5 af-w-3.5 af-mr-1"}):(0,i.jsx)(Ue.ClipboardCopy,{className:"af-h-3.5 af-w-3.5 af-mr-1"}),a?"Copied":"Copy email"]}):null}function Mr(e){return(0,i.jsxs)("span",{className:"af-inline-flex af-flex-wrap af-gap-1",children:[e.sources.includes("customer")&&(0,i.jsxs)($,{variant:"success",children:["Customer \xB7 ",e.order_count," ",e.order_count===1?"order":"orders"]}),e.sources.includes("lead")&&(0,i.jsx)($,{variant:"secondary",children:"Contact form"})]})}function zr(e){return e.name?e.name:e.email?e.email:(0,i.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"Form submission"})}var Jn=/^(message|messages|comment|comments|enquiry|inquiry|question|questions|details|notes|note|body)$/i,Zn=40;function Qn(e){let t=Object.entries(e).filter(([r])=>!r.startsWith("_")),s=t.find(([r,l])=>Jn.test(r)&&typeof l=="string"&&l.trim())??t.filter(([,r])=>typeof r=="string"&&r.trim().length>=Zn).sort((r,l)=>String(l[1]).trim().length-String(r[1]).trim().length)[0];return s?{message:String(s[1]),rest:t.filter(([r])=>r!==s[0])}:{message:null,rest:t}}function Yn(e){return e.replace(/_/g," ")}function Xn(e){if(typeof e=="string")return e;if(e==null)return"";try{return JSON.stringify(e)}catch{return String(e)}}function ei({data:e}){let{message:t,rest:a}=Qn(e);return t===null&&a.length===0?(0,i.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No data submitted"}):(0,i.jsxs)("div",{className:"af-space-y-3",children:[t!==null&&(0,i.jsx)("p",{className:"af-whitespace-pre-wrap af-break-words af-text-sm",children:t}),a.length>0&&(0,i.jsx)("dl",{className:"af-space-y-1",children:a.map(([s,r])=>(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-x-2",children:[(0,i.jsx)("dt",{className:V(Wn,"af-w-28 af-shrink-0 af-capitalize"),children:Yn(s)}),(0,i.jsx)("dd",{className:"af-min-w-0 af-break-words af-text-sm",children:Xn(r)})]},s))})]})}function jr({formName:e,createdAt:t,data:a}){return(0,i.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,i.jsxs)("div",{className:"af-mb-2 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,i.jsx)($,{variant:"outline",children:"Form submission (unverified)"}),e&&(0,i.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:e}),(0,i.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:Ea(t)})]}),(0,i.jsx)(ei,{data:a})]})}function ti(e){return e.charged_total??e.amount_total}function ai({email:e,refreshKey:t,onBack:a}){let{person:s,isLoading:r,error:l,refetch:c}=is(e,{refreshKey:t}),b=Ms(e,s?.submissions[0]?.form_name);return(0,i.jsxs)("div",{children:[(0,i.jsxs)("div",{className:"af-mb-6",children:[(0,i.jsxs)(N,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,i.jsx)(Ue.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"People"]}),(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-start af-justify-between af-gap-3",children:[(0,i.jsxs)("div",{className:"af-min-w-0",children:[r?(0,i.jsx)(K,{className:"af-h-7 af-w-48"}):(0,i.jsx)("h3",{className:"af-text-xl af-font-semibold af-break-all",children:s?.name||e}),(0,i.jsxs)("div",{className:"af-mt-1 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,i.jsx)("span",{className:"af-break-all af-text-sm af-text-muted-foreground",children:e}),(0,i.jsx)(Kn,{value:e,label:"Copy email address"})]})]}),(0,i.jsx)(N,{asChild:!0,children:(0,i.jsxs)("a",{href:b,children:[(0,i.jsx)(Ue.Mail,{className:"af-h-4 af-w-4 af-mr-1"}),"Reply"]})})]})]}),r?(0,i.jsxs)("div",{className:"af-space-y-3","aria-hidden":"true",children:[[0,1,2].map(u=>(0,i.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,i.jsx)(K,{className:"af-w-40"}),(0,i.jsx)(K,{className:"af-mt-2 af-w-64"})]},u)),(0,i.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading this person"})]}):l||!s?(0,i.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,i.jsx)(ut,{message:"Couldn't load this person.",onRetry:()=>c()})}):(0,i.jsxs)("div",{className:"af-space-y-6",children:[s.customer&&(0,i.jsxs)("div",{children:[(0,i.jsx)("h4",{className:V($s,"af-mb-3"),children:"Customer"}),(0,i.jsxs)("dl",{className:"af-space-y-1 af-text-sm",children:[s.customer.name&&(0,i.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,i.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Name"}),(0,i.jsx)("dd",{children:s.customer.name})]}),s.customer.phone&&(0,i.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,i.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Phone"}),(0,i.jsx)("dd",{children:s.customer.phone})]}),(0,i.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,i.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Customer since"}),(0,i.jsx)("dd",{children:Ea(s.customer.created_at)})]})]})]}),s.orders.length>0&&(0,i.jsxs)("div",{children:[(0,i.jsx)("h4",{className:V($s,"af-mb-3"),children:"Purchases"}),(0,i.jsx)("div",{className:"af-overflow-x-auto",children:(0,i.jsxs)(Re,{children:[(0,i.jsx)(Ce,{children:(0,i.jsxs)(ie,{children:[(0,i.jsx)(L,{children:"Order"}),(0,i.jsx)(L,{children:"Date"}),(0,i.jsx)(L,{className:"af-text-right",children:"Total"}),(0,i.jsx)(L,{children:"Status"})]})}),(0,i.jsx)(Te,{children:s.orders.map(u=>(0,i.jsxs)(ie,{children:[(0,i.jsx)(B,{className:"af-whitespace-nowrap af-font-medium",children:ra(u)}),(0,i.jsx)(B,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Ea(u.created_at)}),(0,i.jsx)(B,{className:"af-whitespace-nowrap af-text-right af-font-medium af-tabular-nums",children:M(ti(u),u.currency)}),(0,i.jsx)(B,{children:(0,i.jsx)(ua,{status:u.status})})]},u.id))})]})}),(0,i.jsx)("p",{className:"af-mt-2 af-text-xs af-text-muted-foreground",children:"Ask the AI to open one of these orders for the full details."})]}),s.submissions.length>0&&(0,i.jsxs)("div",{children:[(0,i.jsx)("h4",{className:V($s,"af-mb-3"),children:"Messages"}),(0,i.jsx)("div",{className:"af-space-y-3",children:s.submissions.map(u=>(0,i.jsx)(jr,{formName:u.form_name,createdAt:u.created_at,data:u.data},u.id))})]})]})]})}function si({submissionId:e,onBack:t}){let{api:a}=ee(),[s,r]=It.useState(null),[l,c]=It.useState(!0),[b,u]=It.useState(null),[m,n]=It.useState(0);return It.useEffect(()=>{let y=!1;return c(!0),u(null),a.getSubmission(e).then(w=>{y||r(w)}).catch(w=>{y||u(w instanceof Error?w:new Error("Failed to load submission"))}).finally(()=>{y||c(!1)}),()=>{y=!0}},[a,e,m]),(0,i.jsxs)("div",{children:[(0,i.jsxs)("div",{className:"af-mb-6",children:[(0,i.jsxs)(N,{variant:"ghost",size:"sm",onClick:t,className:"af-mb-2 af--ml-2",children:[(0,i.jsx)(Ue.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"People"]}),(0,i.jsx)("h3",{className:"af-text-xl af-font-semibold",children:"Form submission"}),l?(0,i.jsx)(K,{className:"af-mt-1 af-h-4 af-w-32"}):s&&(0,i.jsx)("p",{className:"af-mt-1 af-text-sm af-text-muted-foreground",children:Ea(s.metadata.submitted_at)})]}),l?(0,i.jsxs)("div",{className:"af-rounded-md af-border af-p-3","aria-hidden":"true",children:[(0,i.jsx)(K,{className:"af-w-40"}),(0,i.jsx)(K,{className:"af-mt-2 af-w-64"}),(0,i.jsx)(K,{className:"af-mt-2 af-w-52"}),(0,i.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading this submission"})]}):b||!s?(0,i.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,i.jsx)(ut,{message:"Couldn't load this submission.",onRetry:()=>n(y=>y+1)})}):(0,i.jsxs)("div",{className:"af-space-y-3",children:[(0,i.jsx)(jr,{formName:null,createdAt:s.metadata.submitted_at,data:s.data}),(0,i.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"No email address came with this submission, so there is nobody to reply to."})]})]})}var ri={all:"All",customers:"Customers",leads:"Leads"},ni=["all","customers","leads"];function ii(e,t){return t==="customers"?e.sources.includes("customer"):t==="leads"?e.sources.includes("lead"):!0}function zs({refreshKey:e,onBackToWebsite:t,className:a}){let{people:s,isLoading:r,error:l,page:c,pageSize:b,hasNextPage:u,hasPreviousPage:m,setPage:n,q:y,setQ:w,refetch:v}=ns({refreshKey:e}),[f,p]=It.useState({kind:"list"}),[h,S]=It.useState(""),[T,U]=It.useState("all"),W=j=>{j.email?p({kind:"person",email:j.email}):j.submission_id&&p({kind:"submission",submissionId:j.submission_id})},D=()=>{p({kind:"list"}),v()},H=j=>{let A=j.trim();A!==y&&w(A)},z=s.filter(j=>ii(j,T)),Q;if(f.kind==="person")Q=(0,i.jsx)(ai,{email:f.email,refreshKey:e,onBack:D});else if(f.kind==="submission")Q=(0,i.jsx)(si,{submissionId:f.submissionId,onBack:D});else{let j=(0,i.jsxs)("div",{className:"af-mb-4 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,i.jsx)("div",{role:"group","aria-label":"People views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:ni.map(A=>(0,i.jsx)("button",{type:"button","aria-pressed":T===A,onClick:()=>U(A),className:V("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",T===A?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:ri[A]},A))}),(0,i.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,i.jsx)(Ue.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,i.jsx)(fe,{type:"text",value:h,onChange:A=>S(A.target.value),onBlur:()=>H(h),onKeyDown:A=>{A.key==="Enter"&&(A.preventDefault(),H(h))},placeholder:"Search by name or email","aria-label":"Search people by name or email",className:"af-pl-8"})]}),y&&(0,i.jsx)(N,{type:"button",variant:"ghost",size:"sm",onClick:()=>{S(""),w("")},children:"Clear search"})]});if(r)Q=(0,i.jsxs)("div",{children:[j,(0,i.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(A=>(0,i.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,i.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,i.jsx)(K,{className:"af-h-4 af-w-32"}),(0,i.jsx)(K,{className:"af-h-4 af-w-16"})]}),(0,i.jsx)(K,{className:"af-mt-2 af-h-5 af-w-28"}),(0,i.jsx)(K,{className:"af-mt-2 af-h-3 af-w-56"})]},A))}),(0,i.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,i.jsxs)(Re,{children:[(0,i.jsx)(Ce,{children:(0,i.jsxs)(ie,{children:[(0,i.jsx)(L,{children:"Person"}),(0,i.jsx)(L,{children:"Came from"}),(0,i.jsx)(L,{children:"Latest message"}),(0,i.jsx)(L,{children:"Last activity"}),(0,i.jsx)(L,{className:"af-w-8"}),(0,i.jsx)(L,{className:"af-w-8"})]})}),(0,i.jsx)(Te,{"aria-hidden":"true",children:[0,1,2,3,4].map(A=>(0,i.jsxs)(ie,{children:[(0,i.jsx)(B,{children:(0,i.jsx)(K,{className:"af-h-4 af-w-40"})}),(0,i.jsx)(B,{children:(0,i.jsx)(K,{className:"af-h-5 af-w-24"})}),(0,i.jsx)(B,{children:(0,i.jsx)(K,{className:"af-h-4 af-w-56"})}),(0,i.jsx)(B,{children:(0,i.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,i.jsx)(B,{}),(0,i.jsx)(B,{})]},A))})]})}),(0,i.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading people"})]});else if(l)Q=(0,i.jsxs)("div",{children:[j,(0,i.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,i.jsx)(ut,{message:"Couldn't load your leads.",onRetry:()=>v()})})]});else if(z.length===0&&c===0){let A=!!y||T!=="all";Q=(0,i.jsxs)("div",{children:[A&&j,A?(0,i.jsx)(qt,{children:y?"Nobody matches that search.":T==="customers"?"Nobody has bought anything yet.":"No form submissions yet."}):(0,i.jsx)(qt,{icon:(0,i.jsx)(Ue.Users,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:"Form submissions and customers will show up here."})]})}else if(s.length===0)Q=(0,i.jsxs)("div",{children:[j,(0,i.jsx)(qt,{children:"No more results."}),(0,i.jsx)("div",{className:"af-flex af-justify-end af-px-2 af-py-4",children:(0,i.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>n(c-1),children:[(0,i.jsx)(Ue.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]})})]});else{let A=c*b+1,de=c*b+s.length;Q=(0,i.jsxs)("div",{children:[j,(0,i.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:z.map((x,oe)=>(0,i.jsxs)("div",{onClick:()=>W(x),className:"af-cursor-pointer af-rounded-md af-border af-p-3 hover:af-bg-muted/50",children:[(0,i.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,i.jsxs)("div",{className:"af-min-w-0",children:[(0,i.jsx)("button",{type:"button",onClick:we=>{we.stopPropagation(),W(x)},"aria-label":`Open ${x.name||x.email||"this form submission"}`,className:"af-block af-max-w-full af-truncate af-rounded-sm af-text-left af-text-sm af-font-medium hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:zr(x)}),x.name&&x.email&&(0,i.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:x.email})]}),(0,i.jsxs)("div",{className:"af-flex af-shrink-0 af-items-center af-gap-1",children:[(0,i.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:Ea(x.last_activity_at)}),x.email&&(0,i.jsx)(N,{asChild:!0,variant:"ghost",size:"icon",className:"af-h-8 af-w-8",children:(0,i.jsx)("a",{href:Ms(x.email),onClick:we=>we.stopPropagation(),"aria-label":`Email ${x.email}`,children:(0,i.jsx)(Ue.Mail,{className:"af-h-4 af-w-4"})})})]})]}),(0,i.jsx)("div",{className:"af-mt-2",children:Mr(x)}),x.latest_message_excerpt&&(0,i.jsx)("p",{className:"af-mt-2 af-line-clamp-2 af-break-words af-text-xs af-text-muted-foreground",children:x.latest_message_excerpt})]},x.email??x.submission_id??oe))}),(0,i.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,i.jsxs)(Re,{children:[(0,i.jsx)(Ce,{children:(0,i.jsxs)(ie,{children:[(0,i.jsx)(L,{children:"Person"}),(0,i.jsx)(L,{children:"Came from"}),(0,i.jsx)(L,{children:"Latest message"}),(0,i.jsx)(L,{children:"Last activity"}),(0,i.jsx)(L,{className:"af-w-8",children:(0,i.jsx)("span",{className:"af-sr-only",children:"Reply"})}),(0,i.jsx)(L,{className:"af-w-8"})]})}),(0,i.jsx)(Te,{children:z.map((x,oe)=>(0,i.jsxs)(ie,{onClick:()=>W(x),className:"af-cursor-pointer focus-within:af-bg-muted/50",children:[(0,i.jsxs)(B,{className:"af-max-w-[260px] af-font-medium",children:[(0,i.jsx)("button",{type:"button",onClick:we=>{we.stopPropagation(),W(x)},"aria-label":`Open ${x.name||x.email||"this form submission"}`,className:"af-block af-max-w-full af-truncate af-rounded-sm af-text-left hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:zr(x)}),x.name&&x.email&&(0,i.jsx)("div",{className:"af-truncate af-text-xs af-font-normal af-text-muted-foreground",children:x.email})]}),(0,i.jsx)(B,{children:Mr(x)}),(0,i.jsx)(B,{className:"af-max-w-[320px]",children:x.latest_message_excerpt?(0,i.jsx)("span",{className:"af-line-clamp-1 af-break-words af-text-sm af-text-muted-foreground",children:x.latest_message_excerpt}):(0,i.jsx)("span",{className:"af-text-muted-foreground",children:"\u2014"})}),(0,i.jsx)(B,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Ea(x.last_activity_at)}),(0,i.jsx)(B,{className:"af-w-8",children:x.email&&(0,i.jsx)(N,{asChild:!0,variant:"ghost",size:"icon",className:"af-h-8 af-w-8",children:(0,i.jsx)("a",{href:Ms(x.email),onClick:we=>we.stopPropagation(),"aria-label":`Email ${x.email}`,children:(0,i.jsx)(Ue.Mail,{className:"af-h-4 af-w-4"})})})}),(0,i.jsx)(B,{className:"af-w-8 af-text-right",children:(0,i.jsx)(Ue.ChevronRight,{"aria-hidden":"true",className:"af-h-4 af-w-4 af-text-muted-foreground/50"})})]},x.email??x.submission_id??oe))})]})}),(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,i.jsx)("span",{className:"af-text-sm af-text-muted-foreground",children:T==="all"?`Showing ${A}-${de}`:`${z.length} of ${s.length} on this page`}),(0,i.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,i.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>n(c-1),disabled:!m,children:[(0,i.jsx)(Ue.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,i.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>n(c+1),disabled:!u,children:["Next",(0,i.jsx)(Ue.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}}return(0,i.jsx)(Bt,{title:"Leads",onBackToWebsite:t,hideBackButton:f.kind!=="list",className:a,children:Q})}var Pa=require("lucide-react");var it=te(require("react"),1);function os({initialPageSize:e=25,refreshKey:t}={}){let{api:a}=ee(),[s,r]=it.useState([]),[l,c]=it.useState(0),[b,u]=it.useState(!0),[m,n]=it.useState(null),[y,w]=it.useState(0),[v,f]=it.useState(e),[p,h]=it.useState(void 0),S=it.useRef(0),T=it.useCallback(async()=>{let W=++S.current;try{u(!0),n(null);let D={limit:v,offset:y*v};p&&(D.status=p);let H=await a.getSubscriptions(D);if(W!==S.current)return;r(H.subscriptions),c(H.total)}catch(D){if(W!==S.current)return;n(D instanceof Error?D:new Error("Failed to fetch subscriptions"))}finally{W===S.current&&u(!1)}},[a,y,v,p,t]);it.useEffect(()=>{T()},[T]);let U=Math.ceil(l/v);return{subscriptions:s,total:l,isLoading:b,error:m,page:y,pageSize:v,hasNextPage:y<U-1,hasPreviousPage:y>0,setPage:w,setPageSize:W=>{f(W),w(0)},status:p,setStatus:W=>{h(W),w(0)},refetch:T}}var R=require("react/jsx-runtime");function qr(e){let t=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(t).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function Wr({sub:e}){return e.status==="canceled"?(0,R.jsx)($,{variant:"outline",children:"Canceled"}):e.status==="past_due"?(0,R.jsx)($,{variant:"destructive",children:"Past due"}):e.status==="active"?e.cancel_at_period_end?(0,R.jsx)($,{variant:"warning",children:"Scheduled to cancel"}):(0,R.jsx)($,{variant:"success",children:"Active"}):(0,R.jsx)($,{variant:"outline",title:e.status,children:"Managed in Stripe"})}function oi(e,t){let a=M(t,e.currency);return e.plan_interval_count===1?`${a} / ${e.plan_interval}`:`${a} every ${e.plan_interval_count} ${e.plan_interval}s`}function Gr({sub:e}){let t=e.current_amount!==e.plan_amount;return(0,R.jsxs)("div",{children:[(0,R.jsx)("div",{className:"af-font-medium af-tabular-nums",children:oi(e,e.current_amount)}),t&&(0,R.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["signed up at ",M(e.plan_amount,e.currency)]})]})}function Kr(e){return e.status==="canceled"?"\u2014":e.cancel_at_period_end?e.current_period_end?`Ends ${qr(e.current_period_end)}`:"Ends at period end":e.current_period_end?qr(e.current_period_end):"\u2014"}function js({refreshKey:e,onBackToWebsite:t,className:a}){let{subscriptions:s,total:r,isLoading:l,error:c,page:b,pageSize:u,hasNextPage:m,hasPreviousPage:n,setPage:y,refetch:w}=os({refreshKey:e}),v;if(l)v=(0,R.jsxs)("div",{children:[(0,R.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(f=>(0,R.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,R.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,R.jsx)(K,{className:"af-h-4 af-w-40"}),(0,R.jsx)(K,{className:"af-h-4 af-w-20"})]}),(0,R.jsx)(K,{className:"af-mt-2 af-h-3 af-w-32"}),(0,R.jsx)(K,{className:"af-mt-2 af-h-5 af-w-24"})]},f))}),(0,R.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,R.jsxs)(Re,{children:[(0,R.jsx)(Ce,{children:(0,R.jsxs)(ie,{children:[(0,R.jsx)(L,{children:"Subscriber"}),(0,R.jsx)(L,{children:"Plan"}),(0,R.jsx)(L,{className:"af-text-right",children:"Amount"}),(0,R.jsx)(L,{children:"Renews"}),(0,R.jsx)(L,{children:"Status"})]})}),(0,R.jsx)(Te,{"aria-hidden":"true",children:[0,1,2,3,4].map(f=>(0,R.jsxs)(ie,{children:[(0,R.jsx)(B,{children:(0,R.jsx)(K,{className:"af-h-4 af-w-44"})}),(0,R.jsx)(B,{children:(0,R.jsx)(K,{className:"af-h-4 af-w-32"})}),(0,R.jsx)(B,{className:"af-text-right",children:(0,R.jsx)(K,{className:"af-ml-auto af-h-4 af-w-24"})}),(0,R.jsx)(B,{children:(0,R.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,R.jsx)(B,{children:(0,R.jsx)(K,{className:"af-h-5 af-w-20"})})]},f))})]})}),(0,R.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading subscriptions"})]});else if(c)v=(0,R.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,R.jsx)(ut,{message:"Couldn't load your subscriptions.",onRetry:()=>w()})});else if(s.length===0&&b===0)v=(0,R.jsx)(qt,{icon:(0,R.jsx)(Pa.Repeat,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:"Subscriptions appear here when a customer subscribes to one of your products."});else if(s.length===0)v=(0,R.jsxs)("div",{children:[(0,R.jsx)(qt,{children:"No more results."}),(0,R.jsx)("div",{className:"af-flex af-justify-end af-px-2 af-py-4",children:(0,R.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>y(b-1),children:[(0,R.jsx)(Pa.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]})})]});else{let f=b*u+1,p=b*u+s.length;v=(0,R.jsxs)("div",{children:[(0,R.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:s.map(h=>(0,R.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,R.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,R.jsxs)("div",{className:"af-min-w-0",children:[(0,R.jsx)("div",{className:"af-truncate af-text-sm af-font-medium",children:h.customer_email||(0,R.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"})}),(0,R.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:h.plan_title||"Untitled plan"})]}),(0,R.jsx)("div",{className:"af-shrink-0 af-text-right",children:(0,R.jsx)(Gr,{sub:h})})]}),(0,R.jsxs)("div",{className:"af-mt-2 af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,R.jsx)(Wr,{sub:h}),h.is_test&&(0,R.jsx)($,{variant:"outline",children:"Test"}),(0,R.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:Kr(h)})]})]},h.id))}),(0,R.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,R.jsxs)(Re,{children:[(0,R.jsx)(Ce,{children:(0,R.jsxs)(ie,{children:[(0,R.jsx)(L,{children:"Subscriber"}),(0,R.jsx)(L,{children:"Plan"}),(0,R.jsx)(L,{className:"af-text-right",children:"Amount"}),(0,R.jsx)(L,{children:"Renews"}),(0,R.jsx)(L,{children:"Status"})]})}),(0,R.jsx)(Te,{children:s.map(h=>(0,R.jsxs)(ie,{children:[(0,R.jsx)(B,{className:"af-max-w-[240px]",children:(0,R.jsx)("div",{className:"af-truncate af-font-medium",children:h.customer_email||(0,R.jsx)("span",{className:"af-italic af-text-muted-foreground",children:"No email"})})}),(0,R.jsx)(B,{className:"af-max-w-[220px]",children:(0,R.jsx)("div",{className:"af-truncate",children:h.plan_title||(0,R.jsx)("span",{className:"af-text-muted-foreground",children:"Untitled plan"})})}),(0,R.jsx)(B,{className:"af-whitespace-nowrap af-text-right",children:(0,R.jsx)(Gr,{sub:h})}),(0,R.jsx)(B,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Kr(h)}),(0,R.jsx)(B,{children:(0,R.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,R.jsx)(Wr,{sub:h}),h.is_test&&(0,R.jsx)($,{variant:"outline",children:"Test"})]})})]},h.id))})]})}),(0,R.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,R.jsx)("span",{className:"af-text-sm af-text-muted-foreground",children:`Showing ${f}-${p} of ${r}`}),(0,R.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,R.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>y(b-1),disabled:!n,children:[(0,R.jsx)(Pa.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,R.jsxs)(N,{variant:"outline",size:"sm",onClick:()=>y(b+1),disabled:!m,children:["Next",(0,R.jsx)(Pa.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}return(0,R.jsx)(Bt,{title:"Subscriptions",onBackToWebsite:t,className:a,children:v})}0&&(module.exports={AifeaturesApiError,AifeaturesProvider,CustomerDetail,CustomersList,FormSettings,FormSubmissions,FormsDashboard,FormsList,LeadsView,OrderDetail,OrderStatusBadge,OrdersList,OrdersView,PaymentsDashboard,PaymentsSetup,ProductSettings,ProductsList,ProductsView,SubmissionDetail,SubscriptionsView,VariantsEditor,ViewShell,WebhookDeliveries,WebhookSettings,chargedTotal,createApiClient,formatPrice,isZeroDecimalCurrency,toMajorUnits,toMinorUnits,useAifeatures,useAifeaturesContext,useBusinessSummary,useCustomers,useEventDestinations,useForms,useOrders,usePaymentsStatus,usePeople,usePerson,useProducts,useSubmissions,useSubscriptions});
|
|
2
|
+
"use strict";var Fn=Object.create;var Cs=Object.defineProperty;var Un=Object.getOwnPropertyDescriptor;var $n=Object.getOwnPropertyNames;var Vn=Object.getPrototypeOf,Hn=Object.prototype.hasOwnProperty;var Mn=(e,a)=>{for(var s in a)Cs(e,s,{get:a[s],enumerable:!0})},Tr=(e,a,s,t)=>{if(a&&typeof a=="object"||typeof a=="function")for(let r of $n(a))!Hn.call(e,r)&&r!==s&&Cs(e,r,{get:()=>a[r],enumerable:!(t=Un(a,r))||t.enumerable});return e};var se=(e,a,s)=>(s=e!=null?Fn(Vn(e)):{},Tr(a||!e||!e.__esModule?Cs(s,"default",{value:e,enumerable:!0}):s,e)),zn=e=>Tr(Cs({},"__esModule",{value:!0}),e);var ao={};Mn(ao,{AifeaturesApiError:()=>ma,AifeaturesProvider:()=>kr,CustomerDetail:()=>ys,CustomersList:()=>hs,FormSettings:()=>fs,FormSubmissions:()=>rs,FormsDashboard:()=>Js,FormsList:()=>es,InvoiceDetail:()=>Hs,InvoiceLabelBadge:()=>Wa,InvoicesList:()=>Ua,InvoicesView:()=>hr,LeadsView:()=>vr,OrderDetail:()=>vs,OrderStatusBadge:()=>Ca,OrdersList:()=>Ba,OrdersView:()=>ur,PaymentsDashboard:()=>dr,PaymentsSetup:()=>xs,ProductSettings:()=>ms,ProductsList:()=>ds,ProductsView:()=>mr,RetainerStatusBadge:()=>zs,RetainersPanel:()=>$a,SubmissionDetail:()=>as,SubscriptionsView:()=>br,VariantsEditor:()=>us,ViewShell:()=>Ct,WebhookDeliveries:()=>ls,WebhookSettings:()=>cs,chargedTotal:()=>Ft,createApiClient:()=>Ts,formatPrice:()=>z,isZeroDecimalCurrency:()=>Jt,toMajorUnits:()=>rt,toMinorUnits:()=>Ut,useAifeatures:()=>te,useAifeaturesContext:()=>te,useBusinessSummary:()=>Ns,useCustomers:()=>bs,useEventDestinations:()=>is,useForms:()=>Ia,useInvoices:()=>Fa,useOrders:()=>ps,usePaymentsStatus:()=>Va,usePeople:()=>Ss,usePerson:()=>Ps,useProducts:()=>Ha,useRetainers:()=>Ma,useSubmissions:()=>ts,useSubscriptions:()=>Rs});module.exports=zn(ao);var wa=se(require("react"),1);var ma=class extends Error{constructor(s,t,r){super(s);this.status=t;this.details=r;this.name="AifeaturesApiError"}};function Zs(e,a){if(typeof e=="string"&&e)return e;if(e&&typeof e=="object")try{return JSON.stringify(e)}catch{}return`Request failed with status ${a}`}function Ts(e,a="https://aifeatures.dev"){async function s(t,r={}){let c=`${a}${t}`,p=await fetch(c,{...r,headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json",...r.headers}});if(!p.ok){let g;try{g=await p.json()}catch{}throw new ma(Zs(g?.error,p.status),p.status,g)}if(p.status!==204)return p.json()}return{async getForms(){return(await s("/api/v1/forms")).forms},async getForm(t){return s(`/api/v1/forms/${t}`)},async createForm(t){return s("/api/v1/forms",{method:"POST",body:JSON.stringify(t)})},async updateForm(t,r){return s(`/api/v1/forms/${t}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteForm(t){await s(`/api/v1/forms/${t}`,{method:"DELETE"})},async getSubmissions(t,r={}){let c=new URLSearchParams;r.limit!==void 0&&c.set("limit",String(r.limit)),r.offset!==void 0&&c.set("offset",String(r.offset));let p=c.toString(),g=`/api/v1/forms/${t}/submissions${p?`?${p}`:""}`;return s(g)},async getSubmission(t){return s(`/api/v1/submissions/${t}`)},async deleteSubmission(t){await s(`/api/v1/submissions/${t}`,{method:"DELETE"})},async downloadAttachment(t,r){let c=typeof r=="string"?{name:r}:r,p=c.name,g=c.id??c.name,v=`${a}/api/v1/submissions/${t}/attachments/${encodeURIComponent(g)}`,y=await fetch(v,{headers:{Authorization:`Bearer ${e}`}});if(!y.ok){let x;try{x=await y.json()}catch{}throw new ma(Zs(x?.error,y.status),y.status,x)}let n=await y.blob(),b=URL.createObjectURL(n),S=document.createElement("a");S.href=b,S.download=p,document.body.appendChild(S),S.click(),document.body.removeChild(S),URL.revokeObjectURL(b)},async getPaymentsStatus(){return s("/api/v1/payments/status")},async getProducts(t={}){let r=new URLSearchParams;t.include&&r.set("include",t.include);let c=r.toString();return(await s(`/api/v1/products${c?`?${c}`:""}`)).products},async getProduct(t){return s(`/api/v1/products/${t}`)},async createProduct(t){return s("/api/v1/products",{method:"POST",body:JSON.stringify(t)})},async updateProduct(t,r){return s(`/api/v1/products/${t}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteProduct(t){await s(`/api/v1/products/${t}`,{method:"DELETE"})},async attachProductFile(t,r){let c=new FormData;c.append("file",r);let p=await fetch(`${a}/api/v1/products/${t}/file`,{method:"POST",headers:{Authorization:`Bearer ${e}`},body:c});if(!p.ok){let g;try{g=await p.json()}catch{}throw new ma(Zs(g?.error,p.status),p.status,g)}return p.json()},async attachProductFileFromUrl(t,r){return s(`/api/v1/products/${t}/file`,{method:"POST",body:JSON.stringify({source_url:r})})},async createOption(t,r){return s(`/api/v1/products/${t}/options`,{method:"POST",body:JSON.stringify(r)})},async updateOption(t,r){return s(`/api/v1/options/${t}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteOption(t){await s(`/api/v1/options/${t}`,{method:"DELETE"})},async createVariant(t,r){return s(`/api/v1/products/${t}/variants`,{method:"POST",body:JSON.stringify(r)})},async updateVariant(t,r){return s(`/api/v1/variants/${t}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteVariant(t){await s(`/api/v1/variants/${t}`,{method:"DELETE"})},async setVariantInventory(t,r){return s(`/api/v1/variants/${t}/inventory`,{method:"PUT",body:JSON.stringify(r)})},async adjustVariantInventory(t,r){return s(`/api/v1/variants/${t}/inventory/adjust`,{method:"POST",body:JSON.stringify(r)})},async getCustomers(t={}){let r=new URLSearchParams;t.email?r.set("email",t.email):t.q&&r.set("q",t.q),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/customers${c?`?${c}`:""}`)},async getCustomer(t){return s(`/api/v1/customers/${t}`)},async getOrders(t={}){let r=new URLSearchParams;t.status&&r.set("status",t.status),t.source&&r.set("source",t.source),t.excludeStatus?.length&&r.set("exclude_status",t.excludeStatus.join(",")),t.customerEmail&&r.set("customer_email",t.customerEmail),t.from&&r.set("from",t.from),t.to&&r.set("to",t.to),t.unfulfilledOnly&&r.set("unfulfilled_only","true"),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/orders${c?`?${c}`:""}`)},async getOrder(t){return s(`/api/v1/orders/${t}`)},async getInvoices(t={}){let r=new URLSearchParams;t.labels?.length&&r.set("label",t.labels.join(",")),t.customerEmail&&r.set("customer_email",t.customerEmail),t.customerId&&r.set("customer_id",t.customerId),t.q&&r.set("q",t.q),t.from&&r.set("from",t.from),t.to&&r.set("to",t.to),t.includeAggregates&&r.set("include","aggregates"),t.retainerId&&r.set("retainer_id",t.retainerId),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/invoices${c?`?${c}`:""}`)},async getInvoice(t){return s(`/api/v1/invoices/${t}`)},async getInvoicePreview(t){return s(`/api/v1/invoices/${t}/preview`)},async getInvoiceSettings(){return s("/api/v1/invoice-settings")},async getRetainers(t={}){let r=new URLSearchParams;t.statuses?.length&&r.set("status",t.statuses.join(",")),t.customerEmail&&r.set("customer_email",t.customerEmail),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/retainers${c?`?${c}`:""}`)},async getRetainer(t){return s(`/api/v1/retainers/${t}`)},async resendOrderReceipt(t){await s(`/api/v1/orders/${t}/resend-receipt`,{method:"POST"})},async resetOrderDownloads(t){return s(`/api/v1/orders/${t}/reset-downloads`,{method:"POST"})},async setOrderFulfillment(t,r){return s(`/api/v1/orders/${t}/fulfillment`,{method:"POST",body:JSON.stringify({fulfilled:r.fulfilled,...r.trackingNumber!==void 0?{tracking_number:r.trackingNumber}:{}})})},async getSubscriptions(t={}){let r=new URLSearchParams;t.status&&r.set("status",t.status),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/subscriptions${c?`?${c}`:""}`)},async getBusinessSummary(t){let r=new URLSearchParams;return r.set("tz_offset_minutes",String(t.tzOffsetMinutes)),s(`/api/v1/summary?${r.toString()}`)},async getPeople(t={}){let r=new URLSearchParams;t.since&&r.set("since",t.since),t.q&&r.set("q",t.q),t.limit!==void 0&&r.set("limit",String(t.limit)),t.offset!==void 0&&r.set("offset",String(t.offset));let c=r.toString();return s(`/api/v1/people${c?`?${c}`:""}`)},async getPerson(t){return s(`/api/v1/people/${encodeURIComponent(t)}`)},async getEventDestinations(){return(await s("/api/v1/event-destinations")).destinations},async createEventDestination(t){return s("/api/v1/event-destinations",{method:"POST",body:JSON.stringify(t)})},async updateEventDestination(t,r){return s(`/api/v1/event-destinations/${t}`,{method:"PATCH",body:JSON.stringify(r)})},async deleteEventDestination(t){await s(`/api/v1/event-destinations/${t}`,{method:"DELETE"})},async rotateEventDestinationSecret(t){return s(`/api/v1/event-destinations/${t}/rotate-secret`,{method:"POST"})},async getEventDestinationSecret(t){return(await s(`/api/v1/event-destinations/${t}/secret`)).secret},async getEventDeliveries(t,r){let c=r===void 0?"":`?limit=${r}`;return(await s(`/api/v1/event-destinations/${t}/deliveries${c}`)).deliveries},async retryEventDelivery(t,r){return s(`/api/v1/event-destinations/${t}/deliveries/${r}/retry`,{method:"POST"})}}}var Na=require("react/jsx-runtime"),_r=wa.createContext(null);function jn(e){return e?e.startsWith("sk_")?"Invalid token type: You passed an organization API key (sk_xxx) but AifeaturesProvider requires a site token (st_xxx). Site tokens are returned when you create a site via the API.":e.startsWith("st_")?null:`Invalid token format: Expected a site token starting with "st_" but got "${e.slice(0,10)}...". Site tokens are returned when you create a site via the API.`:"No siteToken provided to AifeaturesProvider"}function kr({siteToken:e,apiUrl:a="https://aifeatures.dev",dark:s=!1,className:t,children:r}){let c=wa.useMemo(()=>jn(e),[e]),p=wa.useMemo(()=>Ts(e,a),[e,a]),g=wa.useMemo(()=>({siteToken:e,apiUrl:a,api:p}),[e,a,p]);return c?(0,Na.jsx)("div",{className:`aifeatures-admin ${s?"dark":""} ${t||""}`,children:(0,Na.jsxs)("div",{className:"af-rounded-md af-border af-border-destructive af-bg-destructive/10 af-p-4",children:[(0,Na.jsx)("h3",{className:"af-text-sm af-font-semibold af-text-destructive af-mb-2",children:"AifeaturesProvider Configuration Error"}),(0,Na.jsx)("p",{className:"af-text-sm af-text-destructive",children:c})]})}):(0,Na.jsx)(_r.Provider,{value:g,children:(0,Na.jsx)("div",{className:`aifeatures-admin ${s?"dark":""} ${t||""}`,children:r})})}function te(){let e=wa.useContext(_r);if(!e)throw new Error("useAifeaturesContext must be used within an AifeaturesProvider");return e}var Ra=se(require("react"),1),ja=require("lucide-react");var _s=se(require("react"),1),jt=se(require("@radix-ui/react-tabs"),1);var Er=require("clsx"),Ir=require("tailwind-merge"),qn=(0,Ir.extendTailwindMerge)({prefix:"af-"});function M(...e){return qn((0,Er.clsx)(e))}var Es=require("react/jsx-runtime"),ks=jt.Root,Xa=_s.forwardRef(({className:e,...a},s)=>(0,Es.jsx)(jt.List,{ref:s,className:M("af-inline-flex af-h-9 af-items-center af-justify-center af-rounded-lg af-bg-muted af-p-1 af-text-muted-foreground",e),...a}));Xa.displayName=jt.List.displayName;var ea=_s.forwardRef(({className:e,...a},s)=>(0,Es.jsx)(jt.Trigger,{ref:s,className:M("af-inline-flex af-items-center af-justify-center af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-ring-offset-background af-transition-all focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring focus-visible:af-ring-offset-2 disabled:af-pointer-events-none disabled:af-opacity-50 data-[state=active]:af-bg-background data-[state=active]:af-text-foreground data-[state=active]:af-shadow",e),...a}));ea.displayName=jt.Trigger.displayName;var ta=_s.forwardRef(({className:e,...a},s)=>(0,Es.jsx)(jt.Content,{ref:s,className:M("af-mt-2 af-ring-offset-background focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring focus-visible:af-ring-offset-2",e),...a}));ta.displayName=jt.Content.displayName;var Dr=se(require("react"),1),Lr=require("@radix-ui/react-slot"),Or=require("class-variance-authority");var Br=require("react/jsx-runtime"),Gn=(0,Or.cva)("af-inline-flex af-items-center af-justify-center af-gap-2 af-whitespace-nowrap af-rounded-md af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring disabled:af-pointer-events-none disabled:af-opacity-50 [&_svg]:af-pointer-events-none [&_svg]:af-size-4 [&_svg]:af-shrink-0",{variants:{variant:{default:"af-bg-primary af-text-primary-foreground af-shadow hover:af-bg-primary/90",destructive:"af-bg-destructive af-text-destructive-foreground af-shadow-sm hover:af-bg-destructive/90",outline:"af-border af-border-input af-bg-background af-shadow-sm hover:af-bg-accent hover:af-text-accent-foreground",secondary:"af-bg-secondary af-text-secondary-foreground af-shadow-sm hover:af-bg-secondary/80",ghost:"hover:af-bg-accent hover:af-text-accent-foreground",link:"af-text-primary af-underline-offset-4 hover:af-underline"},size:{default:"af-h-9 af-px-4 af-py-2",sm:"af-h-8 af-rounded-md af-px-3 af-text-xs",lg:"af-h-10 af-rounded-md af-px-8",icon:"af-h-9 af-w-9"}},defaultVariants:{variant:"default",size:"default"}}),w=Dr.forwardRef(({className:e,variant:a,size:s,asChild:t=!1,...r},c)=>(0,Br.jsx)(t?Lr.Slot:"button",{className:M(Gn({variant:a,size:s,className:e})),ref:c,...r}));w.displayName="Button";var Ya=require("lucide-react");var pa=se(require("react"),1);var aa=require("react/jsx-runtime"),_e=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("div",{className:"af-relative af-w-full af-overflow-auto",children:(0,aa.jsx)("table",{ref:s,className:M("af-w-full af-caption-bottom af-text-sm",e),...a})}));_e.displayName="Table";var ke=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("thead",{ref:s,className:M("[&_tr]:af-border-b",e),...a}));ke.displayName="TableHeader";var Ee=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("tbody",{ref:s,className:M("[&_tr:last-child]:af-border-0",e),...a}));Ee.displayName="TableBody";var Wn=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("tfoot",{ref:s,className:M("af-border-t af-bg-muted/50 af-font-medium [&>tr]:last:af-border-b-0",e),...a}));Wn.displayName="TableFooter";var oe=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("tr",{ref:s,className:M("af-border-b af-transition-colors hover:af-bg-muted/50 data-[state=selected]:af-bg-muted",e),...a}));oe.displayName="TableRow";var I=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("th",{ref:s,className:M("af-h-10 af-px-2 af-text-left af-align-middle af-font-medium af-text-muted-foreground [&:has([role=checkbox])]:af-pr-0 [&>[role=checkbox]]:af-translate-y-[2px]",e),...a}));I.displayName="TableHead";var D=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("td",{ref:s,className:M("af-p-2 af-align-middle [&:has([role=checkbox])]:af-pr-0 [&>[role=checkbox]]:af-translate-y-[2px]",e),...a}));D.displayName="TableCell";var Kn=pa.forwardRef(({className:e,...a},s)=>(0,aa.jsx)("caption",{ref:s,className:M("af-mt-4 af-text-sm af-text-muted-foreground",e),...a}));Kn.displayName="TableCaption";var qt=se(require("react"),1);function Ia(){let{api:e}=te(),[a,s]=qt.useState([]),[t,r]=qt.useState(!0),[c,p]=qt.useState(null),g=qt.useCallback(async()=>{try{r(!0),p(null);let b=await e.getForms();s(b)}catch(b){p(b instanceof Error?b:new Error("Failed to fetch forms"))}finally{r(!1)}},[e]);qt.useEffect(()=>{g()},[g]);let v=qt.useCallback(async b=>{let S=await e.createForm(b);return s(x=>[...x,S]),S},[e]),y=qt.useCallback(async(b,S)=>{let x=await e.updateForm(b,S);return s(d=>d.map(u=>u.id===b?x:u)),x},[e]),n=qt.useCallback(async b=>{await e.deleteForm(b),s(S=>S.filter(x=>x.id!==b))},[e]);return{forms:a,isLoading:t,error:c,refetch:g,createForm:v,updateForm:y,deleteForm:n}}var Ie=require("react/jsx-runtime");function es({onSelectForm:e,className:a}){let{forms:s,isLoading:t,error:r}=Ia();return t?(0,Ie.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,Ie.jsx)("div",{className:"af-text-muted-foreground",children:"Loading forms..."})}):r?(0,Ie.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,Ie.jsxs)("div",{className:"af-text-destructive",children:["Error: ",r.message]})}):s.length===0?(0,Ie.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${a||""}`,children:[(0,Ie.jsx)(Ya.Inbox,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,Ie.jsxs)("p",{className:"af-text-muted-foreground af-text-center",children:["No forms yet.",(0,Ie.jsx)("br",{}),"Forms will appear here when the AI creates contact forms on your website."]})]}):(0,Ie.jsx)("div",{className:a,children:(0,Ie.jsxs)(_e,{children:[(0,Ie.jsx)(ke,{children:(0,Ie.jsxs)(oe,{children:[(0,Ie.jsx)(I,{children:"Name"}),(0,Ie.jsx)(I,{children:"Endpoint"}),(0,Ie.jsx)(I,{className:"af-text-right",children:"Actions"})]})}),(0,Ie.jsx)(Ee,{children:s.map(c=>(0,Ie.jsxs)(oe,{children:[(0,Ie.jsx)(D,{className:"af-font-medium",children:c.name}),(0,Ie.jsx)(D,{children:(0,Ie.jsx)("code",{className:"af-text-xs af-bg-muted af-px-1.5 af-py-0.5 af-rounded",children:c.endpoint_url.replace("https://aifeatures.dev","")})}),(0,Ie.jsx)(D,{className:"af-text-right",children:(0,Ie.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-2",children:[(0,Ie.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>e?.(c,"submissions"),children:[(0,Ie.jsx)(Ya.Inbox,{className:"af-h-4 af-w-4 af-mr-1"}),"Submissions"]}),(0,Ie.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>e?.(c,"settings"),children:[(0,Ie.jsx)(Ya.Settings,{className:"af-h-4 af-w-4 af-mr-1"}),"Settings"]})]})})]},c.id))})]})})}var ss=se(require("react"),1),sa=require("lucide-react");var Sa=se(require("react"),1),ye=se(require("@radix-ui/react-select"),1),Da=require("lucide-react");var qe=require("react/jsx-runtime"),Lt=ye.Root;var Ot=ye.Value,Nt=Sa.forwardRef(({className:e,children:a,...s},t)=>(0,qe.jsxs)(ye.Trigger,{ref:t,className:M("af-flex af-h-9 af-w-full af-items-center af-justify-between af-whitespace-nowrap af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-2 af-text-sm af-shadow-sm af-ring-offset-background placeholder:af-text-muted-foreground focus:af-outline-none focus:af-ring-1 focus:af-ring-ring disabled:af-cursor-not-allowed disabled:af-opacity-50 [&>span]:af-line-clamp-1",e),...s,children:[a,(0,qe.jsx)(ye.Icon,{asChild:!0,children:(0,qe.jsx)(Da.ChevronDown,{className:"af-h-4 af-w-4 af-opacity-50"})})]}));Nt.displayName=ye.Trigger.displayName;var Ar=Sa.forwardRef(({className:e,...a},s)=>(0,qe.jsx)(ye.ScrollUpButton,{ref:s,className:M("af-flex af-cursor-default af-items-center af-justify-center af-py-1",e),...a,children:(0,qe.jsx)(Da.ChevronUp,{className:"af-h-4 af-w-4"})}));Ar.displayName=ye.ScrollUpButton.displayName;var Fr=Sa.forwardRef(({className:e,...a},s)=>(0,qe.jsx)(ye.ScrollDownButton,{ref:s,className:M("af-flex af-cursor-default af-items-center af-justify-center af-py-1",e),...a,children:(0,qe.jsx)(Da.ChevronDown,{className:"af-h-4 af-w-4"})}));Fr.displayName=ye.ScrollDownButton.displayName;var wt=Sa.forwardRef(({className:e,children:a,position:s="popper",...t},r)=>(0,qe.jsx)(ye.Portal,{children:(0,qe.jsxs)(ye.Content,{ref:r,className:M("af-relative af-z-50 af-max-h-96 af-min-w-[8rem] af-overflow-hidden af-rounded-md af-border af-bg-popover af-text-popover-foreground af-shadow-md data-[state=open]:af-animate-in data-[state=closed]:af-animate-out data-[state=closed]:af-fade-out-0 data-[state=open]:af-fade-in-0 data-[state=closed]:af-zoom-out-95 data-[state=open]:af-zoom-in-95 data-[side=bottom]:af-slide-in-from-top-2 data-[side=left]:af-slide-in-from-right-2 data-[side=right]:af-slide-in-from-left-2 data-[side=top]:af-slide-in-from-bottom-2",s==="popper"&&"data-[side=bottom]:af-translate-y-1 data-[side=left]:af--translate-x-1 data-[side=right]:af-translate-x-1 data-[side=top]:af--translate-y-1",e),position:s,...t,children:[(0,qe.jsx)(Ar,{}),(0,qe.jsx)(ye.Viewport,{className:M("af-p-1",s==="popper"&&"af-h-[var(--radix-select-trigger-height)] af-w-full af-min-w-[var(--radix-select-trigger-width)]"),children:a}),(0,qe.jsx)(Fr,{})]})}));wt.displayName=ye.Content.displayName;var Qn=Sa.forwardRef(({className:e,...a},s)=>(0,qe.jsx)(ye.Label,{ref:s,className:M("af-px-2 af-py-1.5 af-text-sm af-font-semibold",e),...a}));Qn.displayName=ye.Label.displayName;var Fe=Sa.forwardRef(({className:e,children:a,...s},t)=>(0,qe.jsxs)(ye.Item,{ref:t,className:M("af-relative af-flex af-w-full af-cursor-default af-select-none af-items-center af-rounded-sm af-py-1.5 af-pl-2 af-pr-8 af-text-sm af-outline-none focus:af-bg-accent focus:af-text-accent-foreground data-[disabled]:af-pointer-events-none data-[disabled]:af-opacity-50",e),...s,children:[(0,qe.jsx)("span",{className:"af-absolute af-right-2 af-flex af-h-3.5 af-w-3.5 af-items-center af-justify-center",children:(0,qe.jsx)(ye.ItemIndicator,{children:(0,qe.jsx)(Da.Check,{className:"af-h-4 af-w-4"})})}),(0,qe.jsx)(ye.ItemText,{children:a})]}));Fe.displayName=ye.Item.displayName;var Zn=Sa.forwardRef(({className:e,...a},s)=>(0,qe.jsx)(ye.Separator,{ref:s,className:M("af--mx-1 af-my-1 af-h-px af-bg-muted",e),...a}));Zn.displayName=ye.Separator.displayName;var Bt=se(require("react"),1);function ts({formId:e,initialPageSize:a=25}){let{api:s}=te(),[t,r]=Bt.useState([]),[c,p]=Bt.useState(0),[g,v]=Bt.useState(!0),[y,n]=Bt.useState(null),[b,S]=Bt.useState(0),[x,d]=Bt.useState(a),u=Bt.useCallback(async()=>{if(e)try{v(!0),n(null);let C={limit:x,offset:b*x},H=await s.getSubmissions(e,C);r(H.submissions),p(H.total)}catch(C){n(C instanceof Error?C:new Error("Failed to fetch submissions"))}finally{v(!1)}},[s,e,b,x]);Bt.useEffect(()=>{u()},[u]);let h=Bt.useCallback(async C=>{await s.deleteSubmission(C),r(H=>H.filter(G=>G.id!==C)),p(H=>H-1)},[s]),R=Math.ceil(c/x);return{submissions:t,total:c,isLoading:g,error:y,page:b,pageSize:x,hasNextPage:b<R-1,hasPreviousPage:b>0,setPage:S,setPageSize:C=>{d(C),S(0)},refetch:u,deleteSubmission:h}}var $r=se(require("react"),1),ot=require("lucide-react");var le=require("react/jsx-runtime");function Ur(e){return new Date(e).toLocaleString(void 0,{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function Jn(e){if(e===0)return"0 B";let a=1024,s=["B","KB","MB","GB"],t=Math.floor(Math.log(e)/Math.log(a));return`${parseFloat((e/Math.pow(a,t)).toFixed(1))} ${s[t]}`}function as({submission:e,onBack:a}){let{api:s}=te(),[t,r]=$r.useState(null),c=v=>v.id??v.name,p=async v=>{r(c(v));try{await s.downloadAttachment(e.id,v)}catch(y){console.error("Download failed:",y)}finally{r(null)}},g=Object.entries(e.data).filter(([v])=>!v.startsWith("_"));return(0,le.jsxs)("div",{children:[(0,le.jsxs)("div",{className:"af-mb-6",children:[(0,le.jsxs)(w,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,le.jsx)(ot.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Submissions"]}),(0,le.jsx)("h3",{className:"af-text-lg af-font-semibold",children:"Submission Details"}),(0,le.jsxs)("p",{className:"af-text-sm af-text-muted-foreground",children:["Submitted ",Ur(e.metadata.submitted_at)]})]}),(0,le.jsxs)("div",{className:"af-space-y-6",children:[(0,le.jsxs)("div",{children:[(0,le.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Form Data"}),(0,le.jsxs)("div",{className:"af-space-y-3",children:[g.map(([v,y])=>(0,le.jsxs)("div",{className:"af-grid af-grid-cols-3 af-gap-2",children:[(0,le.jsx)("div",{className:"af-text-sm af-text-muted-foreground af-capitalize",children:v.replace(/_/g," ")}),(0,le.jsx)("div",{className:"af-col-span-2 af-text-sm af-break-words",children:typeof y=="string"&&y.includes(`
|
|
3
|
+
`)?(0,le.jsx)("pre",{className:"af-whitespace-pre-wrap af-font-sans af-bg-muted af-p-2 af-rounded af-text-xs",children:y}):String(y)})]},v)),g.length===0&&(0,le.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No data submitted"})]})]}),e.attachments.length>0&&(0,le.jsxs)("div",{children:[(0,le.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Attachments"}),(0,le.jsx)("div",{className:"af-space-y-2",children:e.attachments.map((v,y)=>(0,le.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-p-2 af-bg-muted af-rounded",children:[(0,le.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,le.jsx)(ot.FileText,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,le.jsx)("span",{className:"af-text-sm",children:v.name}),(0,le.jsxs)("span",{className:"af-text-xs af-text-muted-foreground",children:["(",Jn(v.size),")"]})]}),(0,le.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>p(v),disabled:t===c(v),children:[t===c(v)?(0,le.jsx)(ot.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,le.jsx)(ot.Download,{className:"af-h-4 af-w-4 af-mr-1"}),t===c(v)?"Downloading...":"Download"]})]},y))})]}),(0,le.jsxs)("div",{children:[(0,le.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Metadata"}),(0,le.jsxs)("div",{className:"af-space-y-2 af-text-sm",children:[(0,le.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,le.jsx)(ot.Clock,{className:"af-h-4 af-w-4"}),(0,le.jsx)("span",{children:Ur(e.metadata.submitted_at)})]}),e.metadata.ip_address&&(0,le.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,le.jsx)(ot.Globe,{className:"af-h-4 af-w-4"}),(0,le.jsxs)("span",{children:["IP: ",e.metadata.ip_address]})]}),e.metadata.user_agent&&(0,le.jsxs)("div",{className:"af-flex af-items-start af-gap-2 af-text-muted-foreground",children:[(0,le.jsx)(ot.Monitor,{className:"af-h-4 af-w-4 af-mt-0.5"}),(0,le.jsx)("span",{className:"af-text-xs af-break-all",children:e.metadata.user_agent})]}),e.resend_id&&(0,le.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-muted-foreground",children:[(0,le.jsx)(ot.Mail,{className:"af-h-4 af-w-4"}),(0,le.jsxs)("span",{children:["Email ID: ",e.resend_id]})]})]})]})]})]})}var ae=require("react/jsx-runtime");function Xn(e){return new Date(e).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function Yn(e){let a=["email","Email","EMAIL","e-mail","mail"];for(let s of a)if(typeof e[s]=="string")return e[s];return null}function ei(e){let a=Object.entries(e).filter(([r])=>!r.startsWith("_")&&!r.toLowerCase().includes("email"));if(a.length===0)return"";let[,s]=a[0],t=String(s);return t.length>50?t.slice(0,50)+"...":t}function rs({formId:e,className:a,defaultSubmissionId:s}){let{submissions:t,total:r,isLoading:c,error:p,page:g,pageSize:v,hasNextPage:y,hasPreviousPage:n,setPage:b,setPageSize:S}=ts({formId:e}),[x,d]=ss.useState("list"),[u,h]=ss.useState(null);ss.useEffect(()=>{if(s&&t.length>0){let f=t.find(L=>L.id===s);f&&(h(f),d("detail"))}},[s,t]);let R=f=>{h(f),d("detail")},C=()=>{d("list"),h(null)};if(x==="detail"&&u)return(0,ae.jsx)("div",{className:a,children:(0,ae.jsx)(as,{submission:u,onBack:C})});if(c)return(0,ae.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,ae.jsx)("div",{className:"af-text-muted-foreground",children:"Loading submissions..."})});if(p)return(0,ae.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${a||""}`,children:(0,ae.jsxs)("div",{className:"af-text-destructive",children:["Error: ",p.message]})});if(t.length===0&&g===0)return(0,ae.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${a||""}`,children:[(0,ae.jsx)(sa.Inbox,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,ae.jsxs)("p",{className:"af-text-muted-foreground af-text-center",children:["No submissions yet.",(0,ae.jsx)("br",{}),"Submissions will appear here when visitors use your form."]})]});let H=g*v+1,G=Math.min((g+1)*v,r);return(0,ae.jsxs)("div",{className:a,children:[(0,ae.jsxs)(_e,{children:[(0,ae.jsx)(ke,{children:(0,ae.jsxs)(oe,{children:[(0,ae.jsx)(I,{children:"Email"}),(0,ae.jsx)(I,{children:"Preview"}),(0,ae.jsx)(I,{children:"Date"}),(0,ae.jsx)(I,{className:"af-text-right",children:"Actions"})]})}),(0,ae.jsx)(Ee,{children:t.map(f=>{let L=Yn(f.data),$=ei(f.data);return(0,ae.jsxs)(oe,{children:[(0,ae.jsx)(D,{className:"af-font-medium",children:(0,ae.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[L||(0,ae.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"}),f.attachments.length>0&&(0,ae.jsxs)("span",{className:"af-flex af-items-center af-gap-1 af-text-muted-foreground",title:`${f.attachments.length} attachment(s)`,children:[(0,ae.jsx)(sa.Paperclip,{className:"af-h-3 af-w-3"}),(0,ae.jsx)("span",{className:"af-text-xs",children:f.attachments.length})]})]})}),(0,ae.jsx)(D,{className:"af-text-muted-foreground af-max-w-[200px] af-truncate",children:$||(0,ae.jsx)("span",{className:"af-italic",children:"No preview available"})}),(0,ae.jsx)(D,{className:"af-text-muted-foreground",children:Xn(f.metadata.submitted_at)}),(0,ae.jsx)(D,{className:"af-text-right",children:(0,ae.jsx)(w,{variant:"ghost",size:"icon",onClick:()=>R(f),title:"View details",children:(0,ae.jsx)(sa.Eye,{className:"af-h-4 af-w-4"})})})]},f.id)})})]}),(0,ae.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-px-2 af-py-4",children:[(0,ae.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,ae.jsxs)("span",{children:["Showing ",H,"-",G," of ",r]}),(0,ae.jsxs)(Lt,{value:String(v),onValueChange:f=>S(Number(f)),children:[(0,ae.jsx)(Nt,{className:"af-w-[70px] af-h-8",children:(0,ae.jsx)(Ot,{})}),(0,ae.jsxs)(wt,{children:[(0,ae.jsx)(Fe,{value:"10",children:"10"}),(0,ae.jsx)(Fe,{value:"25",children:"25"}),(0,ae.jsx)(Fe,{value:"50",children:"50"}),(0,ae.jsx)(Fe,{value:"100",children:"100"})]})]}),(0,ae.jsx)("span",{children:"per page"})]}),(0,ae.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,ae.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>b(g-1),disabled:!n,children:[(0,ae.jsx)(sa.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,ae.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>b(g+1),disabled:!y,children:["Next",(0,ae.jsx)(sa.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var Pt=se(require("react"),1),Pa=require("lucide-react");var Vr=se(require("react"),1);var Hr=require("react/jsx-runtime"),me=Vr.forwardRef(({className:e,type:a,...s},t)=>(0,Hr.jsx)("input",{type:a,className:M("af-flex af-h-9 af-w-full af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-1 af-text-sm af-shadow-sm af-transition-colors file:af-border-0 file:af-bg-transparent file:af-text-sm file:af-font-medium file:af-text-foreground placeholder:af-text-muted-foreground focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring disabled:af-cursor-not-allowed disabled:af-opacity-50",e),ref:t,...s}));me.displayName="Input";var Mr=se(require("react"),1),zr=require("class-variance-authority");var jr=require("react/jsx-runtime"),ti=(0,zr.cva)("af-text-sm af-font-medium af-leading-none peer-disabled:af-cursor-not-allowed peer-disabled:af-opacity-70"),pe=Mr.forwardRef(({className:e,...a},s)=>(0,jr.jsx)("label",{ref:s,className:M(ti(),e),...a}));pe.displayName="Label";var qr=require("class-variance-authority");var Gr=require("react/jsx-runtime"),ai=(0,qr.cva)("af-inline-flex af-items-center af-rounded-md af-border af-px-2.5 af-py-0.5 af-text-xs af-font-semibold af-transition-colors focus:af-outline-none focus:af-ring-2 focus:af-ring-ring focus:af-ring-offset-2",{variants:{variant:{default:"af-border-transparent af-bg-primary af-text-primary-foreground af-shadow hover:af-bg-primary/80",secondary:"af-border-transparent af-bg-secondary af-text-secondary-foreground hover:af-bg-secondary/80",destructive:"af-border-transparent af-bg-destructive af-text-destructive-foreground af-shadow hover:af-bg-destructive/80",outline:"af-text-foreground",success:"af-border-transparent af-bg-green-100 af-text-green-800",warning:"af-border-transparent af-bg-yellow-100 af-text-yellow-800"}},defaultVariants:{variant:"default"}});function W({className:e,variant:a,...s}){return(0,Gr.jsx)("div",{className:M(ai({variant:a}),e),...s})}var At=se(require("react"),1),lt=require("lucide-react");var ns=se(require("react"),1),et=se(require("@radix-ui/react-dialog"),1),Wr=require("lucide-react");var yt=require("react/jsx-runtime"),ra=et.Root;var si=et.Portal;var Kr=ns.forwardRef(({className:e,...a},s)=>(0,yt.jsx)(et.Overlay,{ref:s,className:M("aifeatures-dialog-overlay af-fixed af-inset-0 af-z-50 af-bg-black/80",e),...a}));Kr.displayName=et.Overlay.displayName;var Gt=ns.forwardRef(({className:e,children:a,...s},t)=>(0,yt.jsx)(si,{children:(0,yt.jsxs)("div",{className:"aifeatures-admin",children:[(0,yt.jsx)(Kr,{}),(0,yt.jsxs)(et.Content,{ref:t,className:M("aifeatures-dialog-content af-fixed af-left-[50%] af-top-[50%] af-z-50 af-grid af-w-full af-max-w-lg af-translate-x-[-50%] af-translate-y-[-50%] af-gap-4 af-border af-bg-background af-p-6 af-shadow-lg sm:af-rounded-lg",e),...s,children:[a,(0,yt.jsxs)(et.Close,{className:"af-absolute af-right-4 af-top-4 af-rounded-sm af-opacity-70 af-ring-offset-background af-transition-opacity hover:af-opacity-100 focus:af-outline-none focus:af-ring-2 focus:af-ring-ring focus:af-ring-offset-2 disabled:af-pointer-events-none data-[state=open]:af-bg-accent data-[state=open]:af-text-muted-foreground",children:[(0,yt.jsx)(Wr.X,{className:"af-h-4 af-w-4"}),(0,yt.jsx)("span",{className:"af-sr-only",children:"Close"})]})]})]})}));Gt.displayName=et.Content.displayName;var Wt=({className:e,...a})=>(0,yt.jsx)("div",{className:M("af-flex af-flex-col af-space-y-1.5 af-text-center sm:af-text-left",e),...a});Wt.displayName="DialogHeader";var Kt=({className:e,...a})=>(0,yt.jsx)("div",{className:M("af-flex af-flex-col-reverse sm:af-flex-row sm:af-justify-end sm:af-space-x-2",e),...a});Kt.displayName="DialogFooter";var Qt=ns.forwardRef(({className:e,...a},s)=>(0,yt.jsx)(et.Title,{ref:s,className:M("af-text-lg af-font-semibold af-leading-none af-tracking-tight",e),...a}));Qt.displayName=et.Title.displayName;var Zt=ns.forwardRef(({className:e,...a},s)=>(0,yt.jsx)(et.Description,{ref:s,className:M("af-text-sm af-text-muted-foreground",e),...a}));Zt.displayName=et.Description.displayName;var St=se(require("react"),1);function is(){let{api:e}=te(),[a,s]=St.useState([]),[t,r]=St.useState(!0),[c,p]=St.useState(null),g=St.useCallback(async()=>{try{r(!0),p(null),s(await e.getEventDestinations())}catch(x){p(x instanceof Error?x:new Error("Failed to fetch webhooks"))}finally{r(!1)}},[e]);St.useEffect(()=>{g()},[g]);let v=St.useCallback(async x=>{let d=await e.createEventDestination(x);return s(u=>[d,...u]),d},[e]),y=St.useCallback(async(x,d)=>{let u=await e.updateEventDestination(x,d);return s(h=>h.map(R=>R.id===x?u:R)),u},[e]),n=St.useCallback(async x=>{await e.deleteEventDestination(x),s(d=>d.filter(u=>u.id!==x))},[e]),b=St.useCallback(async x=>{let d=await e.rotateEventDestinationSecret(x);return s(u=>u.map(h=>h.id===x?d:h)),d},[e]),S=St.useCallback(x=>e.getEventDestinationSecret(x),[e]);return{destinations:a,isLoading:t,error:c,refetch:g,createDestination:v,updateDestination:y,deleteDestination:n,rotateSecret:b,getSecret:S}}var ga=se(require("react"),1),os=require("lucide-react");var tt=require("react/jsx-runtime");function ls({destinationId:e,limit:a=25,className:s}){let{api:t}=te(),[r,c]=ga.useState([]),[p,g]=ga.useState(!0),[v,y]=ga.useState(null),[n,b]=ga.useState(null),S=ga.useCallback(async()=>{try{g(!0),y(null),c(await t.getEventDeliveries(e,a))}catch(d){y(d instanceof Error?d.message:"Failed to load deliveries")}finally{g(!1)}},[t,e,a]);ga.useEffect(()=>{S()},[S]);let x=async d=>{try{b(d.id),y(null);let u=await t.retryEventDelivery(e,d.id);c(h=>h.map(R=>R.id===d.id?u:R))}catch(u){y(u instanceof Error?u.message:"Retry failed")}finally{b(null)}};return p?(0,tt.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground af-py-2",children:[(0,tt.jsx)(os.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}),"Loading deliveries..."]}):v?(0,tt.jsx)("div",{className:"af-text-sm af-text-destructive af-py-2",children:v}):r.length===0?(0,tt.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-py-2",children:"Nothing sent yet. Deliveries appear here as submissions come in."}):(0,tt.jsx)("div",{className:s,children:(0,tt.jsx)("div",{className:"af-divide-y af-border af-rounded",children:r.map(d=>{let u=d.delivered_at!==null;return(0,tt.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-3 af-px-3 af-py-2",children:[(0,tt.jsxs)("div",{className:"af-min-w-0",children:[(0,tt.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[u?(0,tt.jsx)(W,{variant:"secondary",children:d.last_status??200}):(0,tt.jsx)(W,{variant:"destructive",children:d.last_status??"failed"}),(0,tt.jsx)("span",{className:"af-text-sm af-truncate",children:d.type})]}),(0,tt.jsxs)("p",{className:"af-text-xs af-text-muted-foreground af-mt-0.5 af-font-mono af-truncate",children:[d.event_id," \xB7 ",d.created_at,d.attempts>0&&` \xB7 ${d.attempts} failed ${d.attempts===1?"attempt":"attempts"}`]}),!u&&d.last_error&&(0,tt.jsx)("p",{className:"af-text-xs af-text-destructive af-mt-0.5 af-truncate",children:d.last_error})]}),(0,tt.jsx)(w,{variant:"ghost",size:"sm",title:"Send this event again",disabled:n===d.id,onClick:()=>x(d),children:n===d.id?(0,tt.jsx)(os.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,tt.jsx)(os.RotateCw,{className:"af-h-4 af-w-4"})})]},d.id)})})})}var X=require("react/jsx-runtime");function cs({formId:e,topics:a,description:s,className:t}){let{destinations:r,isLoading:c,error:p,createDestination:g,updateDestination:v,deleteDestination:y,rotateSecret:n,getSecret:b}=is(),[S,x]=At.useState(""),[d,u]=At.useState(!1),[h,R]=At.useState(null),[C,H]=At.useState(null),[G,f]=At.useState(null),[L,$]=At.useState(!1),[Q,q]=At.useState(null),[k,ne]=At.useState(null),[m,Y]=At.useState(null),Pe=At.useMemo(()=>r.filter(j=>j.form_id===e),[r,e]),xe=async()=>{let j=S.trim();if(j){if(!j.startsWith("https://")){R("Webhook URLs must start with https://");return}try{u(!0),R(null),await g({url:j,form_id:e,...a&&a.length>0?{topics:a}:{}}),x("")}catch(Ne){R(Ne instanceof Error?Ne.message:"Failed to add webhook")}finally{u(!1)}}},fe=async j=>{try{ne(j.id),R(null),await n(j.id),$(!1),f(await b(j.id))}catch(Ne){R(Ne instanceof Error?Ne.message:"Failed to rotate secret")}finally{ne(null)}},Re=async j=>{try{ne(j.id),R(null),await v(j.id,{status:"active"})}catch(Ne){R(Ne instanceof Error?Ne.message:"Failed to re-enable")}finally{ne(null)}},ee=async()=>{if(Q)try{ne(Q.id),await y(Q.id),q(null)}catch(j){R(j instanceof Error?j.message:"Failed to delete")}finally{ne(null)}},ge=async j=>{if(C===j.id){H(null),f(null);return}try{ne(j.id),R(null),$(!1),H(j.id),f(await b(j.id))}catch(Ne){H(null),R(Ne instanceof Error?Ne.message:"Failed to read secret")}finally{ne(null)}},ve=async()=>{if(G)try{await navigator.clipboard.writeText(G),$(!0)}catch{}};return(0,X.jsxs)("div",{className:t,children:[(0,X.jsxs)("div",{className:"af-space-y-1",children:[(0,X.jsx)(pe,{children:"Webhooks"}),(0,X.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:s??"Send this form's submissions to your own system as they arrive \u2014 a Zapier or Make hook, a CRM, or your own endpoint."})]}),(0,X.jsxs)("div",{className:"af-flex af-gap-2 af-mt-4",children:[(0,X.jsx)(me,{value:S,placeholder:"https://hooks.zapier.com/...",onChange:j=>x(j.target.value),onKeyDown:j=>{j.key==="Enter"&&(j.preventDefault(),xe())},disabled:d}),(0,X.jsxs)(w,{onClick:xe,disabled:d||!S.trim(),children:[d?(0,X.jsx)(lt.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,X.jsx)(lt.Plus,{className:"af-h-4 af-w-4"}),(0,X.jsx)("span",{className:"af-ml-1",children:"Add"})]})]}),(h||p)&&(0,X.jsx)("div",{className:"af-mt-2 af-text-sm af-text-destructive",children:h||p?.message}),(0,X.jsxs)("div",{className:"af-mt-4 af-space-y-2",children:[c&&(0,X.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,X.jsx)(lt.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}),"Loading webhooks..."]}),!c&&Pe.length===0&&(0,X.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No webhooks yet. Submissions still arrive by email and in your dashboard."}),Pe.map(j=>(0,X.jsxs)("div",{className:"af-rounded af-border af-p-3 af-space-y-2",children:[(0,X.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-3",children:[(0,X.jsxs)("div",{className:"af-min-w-0",children:[(0,X.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,X.jsx)("span",{className:"af-font-mono af-text-sm af-truncate",children:j.url}),j.status==="disabled"?(0,X.jsx)(W,{variant:"destructive",children:"Disabled"}):(0,X.jsx)(W,{variant:"secondary",children:"Active"})]}),(0,X.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mt-1",children:j.topics.includes("*")?"All events":j.topics.join(", ")})]}),(0,X.jsxs)("div",{className:"af-flex af-items-center af-gap-1 af-shrink-0",children:[(0,X.jsx)(w,{variant:"ghost",size:"sm",onClick:()=>Y(m===j.id?null:j.id),children:m===j.id?"Hide log":"Deliveries"}),(0,X.jsx)(w,{variant:"ghost",size:"sm",onClick:()=>ge(j),disabled:k===j.id,children:C===j.id?"Hide secret":"Show secret"}),(0,X.jsx)(w,{variant:"ghost",size:"sm",title:"Delete webhook",disabled:k===j.id,onClick:()=>q(j),children:(0,X.jsx)(lt.Trash2,{className:"af-h-4 af-w-4"})})]})]}),j.status==="disabled"&&(0,X.jsxs)("div",{className:"af-flex af-items-start af-gap-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2",children:[(0,X.jsx)(lt.AlertTriangle,{className:"af-h-4 af-w-4 af-text-destructive af-mt-0.5 af-shrink-0"}),(0,X.jsxs)("div",{className:"af-text-sm",children:[(0,X.jsxs)("p",{children:["We stopped sending after repeated failures."," ",j.disabled_reason]}),(0,X.jsx)(w,{variant:"outline",size:"sm",className:"af-mt-2",disabled:k===j.id,onClick:()=>Re(j),children:"Re-enable"})]})]}),C===j.id&&G&&(0,X.jsxs)("div",{className:"af-rounded af-bg-muted af-px-3 af-py-2 af-space-y-2",children:[(0,X.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Optional. Use this if you want your endpoint to check that a delivery really came from us. Rotating replaces it."}),(0,X.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,X.jsx)(me,{readOnly:!0,value:G,className:"af-font-mono"}),(0,X.jsx)(w,{variant:"outline",title:"Copy signing secret",onClick:ve,children:L?(0,X.jsx)(lt.Check,{className:"af-h-4 af-w-4"}):(0,X.jsx)(lt.Copy,{className:"af-h-4 af-w-4"})}),(0,X.jsx)(w,{variant:"outline",title:"Rotate signing secret",disabled:k===j.id,onClick:()=>fe(j),children:k===j.id?(0,X.jsx)(lt.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,X.jsx)(lt.RefreshCw,{className:"af-h-4 af-w-4"})})]})]}),m===j.id&&(0,X.jsx)(ls,{destinationId:j.id})]},j.id))]}),(0,X.jsx)(ra,{open:!!Q,onOpenChange:j=>{!j&&!k&&q(null)},children:(0,X.jsxs)(Gt,{children:[(0,X.jsxs)(Wt,{children:[(0,X.jsx)(Qt,{children:"Delete this webhook?"}),(0,X.jsxs)(Zt,{children:["New submissions will stop being sent to ",Q?.url,". Its delivery history is removed too. Submissions themselves are not affected \u2014 they stay in your dashboard and still go out by email."]})]}),(0,X.jsxs)(Kt,{children:[(0,X.jsx)(w,{variant:"outline",onClick:()=>q(null),disabled:!!k,children:"Cancel"}),(0,X.jsx)(w,{variant:"destructive",onClick:ee,disabled:!!k,children:k?"Deleting...":"Delete"})]})]})})]})}var de=require("react/jsx-runtime");function fs({form:e,onSaved:a,className:s}){let{updateForm:t}=Ia(),[r,c]=Pt.useState(!1),[p,g]=Pt.useState(null),[v,y]=Pt.useState(null),[n,b]=Pt.useState(e.name),[S,x]=Pt.useState(e.redirect_url||""),[d,u]=Pt.useState(e.email_recipients),[h,R]=Pt.useState("");Pt.useEffect(()=>{b(e.name),x(e.redirect_url||""),u(e.email_recipients),y(null)},[e]);let C=Pt.useCallback(async $=>{try{c(!0),y(null);let Q=await t(e.id,$);g(new Date),a?.(Q)}catch(Q){y(Q instanceof Error?Q.message:"Failed to save")}finally{c(!1)}},[e.id,t,a]);Pt.useEffect(()=>{if(p){let $=setTimeout(()=>g(null),2e3);return()=>clearTimeout($)}},[p]);let H=async()=>{let $=h.trim().toLowerCase();if(!$)return;if(!$.includes("@")){y("Please enter a valid email address");return}if(d.includes($)){y("This email is already added");return}let Q=[...d,$];u(Q),R(""),y(null),await C({email_recipients:Q})},G=async $=>{let Q=d.filter(q=>q!==$);u(Q),await C({email_recipients:Q})},f=async()=>{n!==e.name&&await C({name:n})},L=async()=>{let $=S||null;$!==e.redirect_url&&await C({redirect_url:$})};return(0,de.jsxs)("div",{className:`af-space-y-6 af-relative ${s||""}`,children:[(r||p)&&(0,de.jsx)("div",{className:"aifeatures-admin af-fixed af-bottom-4 af-right-4 af-z-50",children:r?(0,de.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-bg-background af-border af-shadow-lg af-rounded-lg af-px-4 af-py-3",children:[(0,de.jsx)(Pa.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"}),(0,de.jsx)("span",{className:"af-text-sm",children:"Saving..."})]}):(0,de.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-bg-green-50 af-border af-border-green-200 af-shadow-lg af-rounded-lg af-px-4 af-py-3",children:[(0,de.jsx)(Pa.Check,{className:"af-h-4 af-w-4 af-text-green-600"}),(0,de.jsx)("span",{className:"af-text-sm af-text-green-800",children:"Changes saved"})]})}),(0,de.jsxs)("div",{className:"af-space-y-2",children:[(0,de.jsx)(pe,{htmlFor:"form-name",children:"Form Name"}),(0,de.jsx)(me,{id:"form-name",value:n,onChange:$=>b($.target.value),onBlur:f,placeholder:"Contact Form"}),(0,de.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"A name to identify this form in your dashboard."})]}),(0,de.jsxs)("div",{className:"af-space-y-2",children:[(0,de.jsx)(pe,{children:"Email Recipients"}),(0,de.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mb-2",children:"Form submissions will be sent to these email addresses."}),(0,de.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-2 af-mb-2",children:[d.map($=>(0,de.jsxs)(W,{variant:"secondary",className:"af-gap-1",children:[$,(0,de.jsx)("button",{type:"button",onClick:()=>G($),className:"af-ml-1 hover:af-text-destructive",disabled:r,children:(0,de.jsx)(Pa.X,{className:"af-h-3 af-w-3"})})]},$)),d.length===0&&(0,de.jsx)("span",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No recipients configured"})]}),(0,de.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,de.jsx)(me,{type:"email",value:h,onChange:$=>R($.target.value),placeholder:"Add email address",onKeyDown:$=>{$.key==="Enter"&&($.preventDefault(),H())},disabled:r}),(0,de.jsx)(w,{type:"button",variant:"outline",onClick:H,disabled:!h.trim()||r,children:(0,de.jsx)(Pa.Plus,{className:"af-h-4 af-w-4"})})]})]}),(0,de.jsxs)("div",{className:"af-space-y-2",children:[(0,de.jsx)(pe,{htmlFor:"redirect-url",children:"Redirect URL"}),(0,de.jsx)(me,{id:"redirect-url",value:S,onChange:$=>x($.target.value),onBlur:L,placeholder:"/thank-you"}),(0,de.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Where to send visitors after a successful submission. Use a site path (/thank-you) or a full URL (https://example.com/thank-you). Leave empty for a default success page."})]}),(0,de.jsxs)("div",{className:"af-space-y-2",children:[(0,de.jsx)(pe,{children:"Form Endpoint"}),(0,de.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,de.jsx)("code",{className:"af-flex-1 af-text-xs af-bg-muted af-px-3 af-py-2 af-rounded af-border",children:e.endpoint_url})}),(0,de.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Use this URL as the form action attribute."})]}),(0,de.jsxs)("div",{className:"af-space-y-2",children:[(0,de.jsx)(pe,{children:"Captcha Protection"}),(0,de.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,de.jsx)(W,{variant:e.turnstile_sitekey?"success":"secondary",children:e.turnstile_sitekey?"Protected":"Not Configured"})}),(0,de.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Turnstile CAPTCHA is automatically enabled to prevent spam."})]}),(0,de.jsx)(cs,{formId:e.id}),v&&(0,de.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:v})]})}var De=require("react/jsx-runtime");function Js({className:e,submissionId:a}){let{api:s}=te(),[t,r]=Ra.useState("list"),[c,p]=Ra.useState(null),[g,v]=Ra.useState("submissions"),[y,n]=Ra.useState(a),[b,S]=Ra.useState(!!a);Ra.useEffect(()=>{if(!a)return;async function h(){try{let R=await s.getSubmission(a),C=await s.getForm(R.form_id);p(C),r("detail"),v("submissions"),n(a)}catch(R){console.error("Failed to load submission for deep link:",R)}finally{S(!1)}}h()},[a,s]);let x=(h,R)=>{p(h),r("detail"),v(R),n(void 0)},d=()=>{r("list"),p(null),n(void 0)},u=h=>{p(h)};return b?(0,De.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${e||""}`,children:(0,De.jsx)("div",{className:"af-text-muted-foreground",children:"Loading..."})}):(0,De.jsxs)("div",{className:e,children:[t==="list"&&(0,De.jsxs)("div",{children:[(0,De.jsxs)("div",{className:"af-mb-6",children:[(0,De.jsx)("h2",{className:"af-text-lg af-font-semibold",children:"Forms"}),(0,De.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Manage your contact forms and view submissions."})]}),(0,De.jsx)(es,{onSelectForm:x})]}),t==="detail"&&c&&(0,De.jsxs)("div",{children:[(0,De.jsxs)("div",{className:"af-mb-6",children:[(0,De.jsxs)(w,{variant:"ghost",size:"sm",onClick:d,className:"af-mb-2 af--ml-2",children:[(0,De.jsx)(ja.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Forms"]}),(0,De.jsx)("h2",{className:"af-text-lg af-font-semibold",children:c.name}),(0,De.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:(0,De.jsx)("code",{className:"af-text-xs af-bg-muted af-px-1.5 af-py-0.5 af-rounded",children:c.endpoint_url})})]}),(0,De.jsxs)(ks,{value:g,onValueChange:h=>v(h),children:[(0,De.jsxs)(Xa,{className:"af-mb-4",children:[(0,De.jsxs)(ea,{value:"submissions",className:"af-gap-2",children:[(0,De.jsx)(ja.Inbox,{className:"af-h-4 af-w-4"}),"Submissions"]}),(0,De.jsxs)(ea,{value:"settings",className:"af-gap-2",children:[(0,De.jsx)(ja.Settings,{className:"af-h-4 af-w-4"}),"Settings"]})]}),(0,De.jsx)(ta,{value:"submissions",children:(0,De.jsx)(rs,{formId:c.id,defaultSubmissionId:y})}),(0,De.jsx)(ta,{value:"settings",children:(0,De.jsx)(fs,{form:c,onSaved:u})})]})]})]})}var Ka=se(require("react"),1),Mt=require("lucide-react");var Is=se(require("react"),1),va=require("lucide-react");var ri=new Set(["bif","clp","djf","gnf","jpy","kmf","krw","mga","pyg","rwf","vnd","vuv","xaf","xof","xpf"]);function Jt(e){return ri.has(e.toLowerCase())}function z(e,a,s){let t=Jt(a)?e:e/100;try{return new Intl.NumberFormat(s,{style:"currency",currency:a.toUpperCase()}).format(t)}catch{return`${t} ${a.toUpperCase()}`}}function Ft(e){let a=e.automatic_tax&&e.tax_behavior==="exclusive"?e.tax_total??0:0;return e.amount_total+(e.shipping_total??0)+a}function Ut(e,a){let s=typeof e=="string"?parseFloat(e):e;if(!Number.isFinite(s)||s<0)return null;let t=Jt(a)?s:s*100;return Math.round(t)}function rt(e,a){return Jt(a)?e:e/100}var ie=require("react/jsx-runtime");function ni(e){if(e.allow_custom_amount){let a=e.min_amount!==null?z(e.min_amount,e.currency):"?",s=e.max_amount!==null?z(e.max_amount,e.currency):"?";return`Buyer chooses (${a} - ${s})`}return e.unit_amount===null?"Varies by variant":z(e.unit_amount,e.currency)}function ii(e){switch(e.fulfillment.type){case"download":return e.fulfillment.filename||"Digital download";case"redirect":return"Redirect";default:return"-"}}function ds({productsState:e,onSelectProduct:a,onCreateProduct:s,className:t}){let{products:r,isLoading:c,error:p,deleteProduct:g}=e,[v,y]=Is.useState(null),[n,b]=Is.useState(!1),[S,x]=Is.useState(null),d=async()=>{if(v)try{b(!0),x(null),await g(v.id),y(null)}catch(u){x(u instanceof Error?u.message:"Failed to delete product")}finally{b(!1)}};return c?(0,ie.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,ie.jsx)("div",{className:"af-text-muted-foreground",children:"Loading products..."})}):p?(0,ie.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${t||""}`,children:(0,ie.jsxs)("div",{className:"af-text-destructive",children:["Error: ",p.message]})}):r.length===0?(0,ie.jsxs)("div",{className:`af-flex af-flex-col af-items-center af-justify-center af-py-12 ${t||""}`,children:[(0,ie.jsx)(va.Package,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,ie.jsxs)("p",{className:"af-text-muted-foreground af-text-center af-mb-4",children:["No products yet.",(0,ie.jsx)("br",{}),"Products will appear here when the AI adds them to your website, or you can create one yourself."]}),(0,ie.jsxs)(w,{variant:"outline",onClick:()=>s?.(),children:[(0,ie.jsx)(va.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"New Product"]})]}):(0,ie.jsxs)("div",{className:t,children:[(0,ie.jsx)("div",{className:"af-flex af-justify-end af-mb-4",children:(0,ie.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>s?.(),children:[(0,ie.jsx)(va.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"New Product"]})}),(0,ie.jsxs)(_e,{children:[(0,ie.jsx)(ke,{children:(0,ie.jsxs)(oe,{children:[(0,ie.jsx)(I,{children:"Name"}),(0,ie.jsx)(I,{children:"Price"}),(0,ie.jsx)(I,{children:"Fulfillment"}),(0,ie.jsx)(I,{children:"Status"}),(0,ie.jsx)(I,{className:"af-text-right",children:"Actions"})]})}),(0,ie.jsx)(Ee,{children:r.map(u=>(0,ie.jsxs)(oe,{children:[(0,ie.jsx)(D,{className:"af-font-medium",children:u.name}),(0,ie.jsx)(D,{children:ni(u)}),(0,ie.jsx)(D,{className:"af-text-muted-foreground",children:ii(u)}),(0,ie.jsx)(D,{children:(0,ie.jsx)(W,{variant:u.active?"success":"secondary",children:u.active?"Active":"Inactive"})}),(0,ie.jsx)(D,{className:"af-text-right",children:(0,ie.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-2",children:[(0,ie.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>a?.(u),children:[(0,ie.jsx)(va.Pencil,{className:"af-h-4 af-w-4 af-mr-1"}),"Edit"]}),(0,ie.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>{x(null),y(u)},children:[(0,ie.jsx)(va.Trash2,{className:"af-h-4 af-w-4 af-mr-1"}),"Delete"]})]})})]},u.id))})]}),(0,ie.jsx)(ra,{open:!!v,onOpenChange:u=>{!u&&!n&&y(null)},children:(0,ie.jsxs)(Gt,{children:[(0,ie.jsxs)(Wt,{children:[(0,ie.jsx)(Qt,{children:"Delete product?"}),(0,ie.jsxs)(Zt,{children:['"',v?.name,'" will be removed from your site and can no longer be purchased. Past orders and buyer downloads are not affected.']})]}),S&&(0,ie.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:S}),(0,ie.jsxs)(Kt,{children:[(0,ie.jsx)(w,{variant:"outline",onClick:()=>y(null),disabled:n,children:"Cancel"}),(0,ie.jsx)(w,{variant:"destructive",onClick:d,disabled:n,children:n?"Deleting...":"Delete"})]})]})})]})}var $e=se(require("react"),1),Rt=require("lucide-react");var Ze=se(require("react"),1),$t=require("lucide-react");var P=require("react/jsx-runtime");function Xs(e){return e.option_values.length>0?e.option_values.map(a=>a.value).join(" / "):e.title||"Default"}function us({productId:e,currency:a,className:s}){let{api:t}=te(),r=Jt(a)?"1":"0.01",[c,p]=Ze.useState(null),[g,v]=Ze.useState(!0),[y,n]=Ze.useState(null),[b,S]=Ze.useState(null),[x,d]=Ze.useState(!1),u=Ze.useRef(!0);Ze.useEffect(()=>(u.current=!0,()=>{u.current=!1}),[]);let h=Ze.useCallback(async()=>{try{let T=await t.getProduct(e);if(!u.current)return;p(T)}catch(T){if(!u.current)return;n(T instanceof Error?T.message:"Failed to load product")}finally{u.current&&v(!1)}},[t,e]);Ze.useEffect(()=>{h()},[h]);let R=async T=>{S(null),d(!0);try{await T(),await h()}catch(U){u.current&&S(U instanceof Error?U.message:"Something went wrong")}finally{u.current&&d(!1)}},[C,H]=Ze.useState(""),[G,f]=Ze.useState(""),[L,$]=Ze.useState({}),Q=()=>R(async()=>{let T=C.trim(),U=G.split(",").map(be=>be.trim()).filter(Boolean);if(!T)throw new Error("Option name is required");if(U.length===0)throw new Error("Add at least one value");await t.createOption(e,{title:T,values:U}),u.current&&(H(""),f(""))}),q=T=>R(async()=>{let U=(L[T.id]??"").trim();if(!U)throw new Error("Enter a value");await t.updateOption(T.id,{add_values:[U]}),u.current&&$(be=>({...be,[T.id]:""}))}),k=(T,U)=>R(()=>t.updateOption(T.id,{remove_values:[U]})),ne=T=>R(()=>t.deleteOption(T.id)),[m,Y]=Ze.useState(null),[Pe,xe]=Ze.useState(null),[fe,Re]=Ze.useState(!1),[ee,ge]=Ze.useState(null),ve=c?.options??[],j=c?.variants??[],Ne=()=>{xe(null),Y({variant:null,title:"",sku:"",unitAmount:"",compareAt:"",active:!0,optionValues:Object.fromEntries(ve.map(T=>[T.id,T.values[0]?.value??""])),tracked:!1,onHand:"0",initialOnHand:"0",allowBackorder:!1})},ct=T=>{xe(null);let U=T.inventory?.on_hand??0,be=U<0?"":String(U);Y({variant:T,title:T.title??"",sku:T.sku??"",unitAmount:T.unit_amount!=null?String(rt(T.unit_amount,a)):"",compareAt:T.compare_at_amount!=null?String(rt(T.compare_at_amount,a)):"",active:T.active,optionValues:{},tracked:T.inventory?.tracked??!1,onHand:be,initialOnHand:be,allowBackorder:T.inventory?.allow_backorder??!1})},It=async()=>{if(!m)return;xe(null);let T=m.variant?.allow_custom_amount??!1,U=null,be=null;if(!T){if(U=Ut(m.unitAmount,a),U===null||U<=0){xe("Enter a valid price");return}if(m.compareAt.trim()&&(be=Ut(m.compareAt,a),be===null||be<=U)){xe("Compare-at price must be greater than the price");return}}let st=m.onHand.trim()!==m.initialOnHand.trim(),je=parseInt(m.onHand,10);if(m.tracked&&st&&(!Number.isFinite(je)||je<0)){xe("On-hand quantity must be zero or more");return}try{if(Re(!0),m.variant)await t.updateVariant(m.variant.id,{title:m.title.trim()||null,sku:m.sku.trim()||null,...T?{}:{unit_amount:U,compare_at_amount:be},active:m.active}),await t.setVariantInventory(m.variant.id,{tracked:m.tracked,on_hand:m.tracked&&st?je:void 0,allow_backorder:m.allowBackorder});else{let Ye={};for(let Ke of ve){let pt=m.optionValues[Ke.id];if(!pt){xe(`Choose a ${Ke.title}`),Re(!1);return}Ye[Ke.title]=pt}await t.createVariant(e,{title:m.title.trim()||null,sku:m.sku.trim()||null,unit_amount:U,compare_at_amount:be,active:m.active,option_values:ve.length>0?Ye:void 0,inventory:m.tracked?{tracked:!0,on_hand:je,allow_backorder:m.allowBackorder}:{tracked:!1,allow_backorder:m.allowBackorder}})}await h(),u.current&&Y(null)}catch(Ye){if(u.current&&xe(Ye instanceof Error?Ye.message:"Failed to save variant"),m.variant)try{let Ke=await t.getProduct(e);u.current&&p(Ke)}catch{}}finally{u.current&&Re(!1)}},Ae=()=>R(async()=>{ee&&(await t.deleteVariant(ee.id),u.current&&ge(null))}),ut=m?.variant?.allow_custom_amount??!1,mt=m?.variant!=null&&m.variant.option_values.length===0&&ve.length>0&&!m.variant.active,Ce=m?.variant?.inventory!=null&&m.variant.inventory.on_hand<0?-m.variant.inventory.on_hand:null;function Dt(T){let U=T.inventory;if(!U||!U.tracked)return(0,P.jsx)("span",{className:"af-text-muted-foreground",children:"Unlimited"});let be=U.available??U.on_hand;return be<0?(0,P.jsxs)("span",{children:[-be," backordered"]}):(0,P.jsxs)("span",{children:[be," in stock",U.allow_backorder&&(0,P.jsx)("span",{className:"af-text-muted-foreground af-text-xs af-ml-1",children:"(backorder)"})]})}return g?(0,P.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-6 ${s||""}`,children:(0,P.jsx)($t.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})}):y?(0,P.jsx)("div",{className:s,children:(0,P.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:y})}):(0,P.jsxs)("div",{className:`af-space-y-6 ${s||""}`,children:[(0,P.jsxs)("div",{className:"af-space-y-3",children:[(0,P.jsxs)("div",{children:[(0,P.jsx)(pe,{children:"Options"}),(0,P.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Add options like Size or Color, then create a variant for each combination you sell. Simple products don't need options."})]}),ve.length>0&&(0,P.jsx)("div",{className:"af-space-y-3",children:ve.map(T=>(0,P.jsxs)("div",{className:"af-border af-rounded af-p-3 af-space-y-2",children:[(0,P.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,P.jsx)("span",{className:"af-text-sm af-font-medium",children:T.title}),(0,P.jsx)(w,{variant:"ghost",size:"sm",onClick:()=>ne(T),disabled:x,children:(0,P.jsx)($t.Trash2,{className:"af-h-4 af-w-4"})})]}),(0,P.jsx)("div",{className:"af-flex af-flex-wrap af-gap-2",children:T.values.map(U=>(0,P.jsxs)(W,{variant:"secondary",className:"af-gap-1",children:[U.value,(0,P.jsx)("button",{type:"button",onClick:()=>k(T,U.value),className:"af-ml-1 hover:af-text-destructive",disabled:x,title:"Remove value",children:(0,P.jsx)($t.X,{className:"af-h-3 af-w-3"})})]},U.id))}),(0,P.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,P.jsx)(me,{value:L[T.id]??"",onChange:U=>$(be=>({...be,[T.id]:U.target.value})),placeholder:"Add a value",className:"af-h-8",onKeyDown:U=>{U.key==="Enter"&&(U.preventDefault(),q(T))},disabled:x}),(0,P.jsx)(w,{type:"button",variant:"outline",size:"sm",onClick:()=>q(T),disabled:x||!(L[T.id]??"").trim(),children:(0,P.jsx)($t.Plus,{className:"af-h-4 af-w-4"})})]})]},T.id))}),(0,P.jsxs)("div",{className:"af-flex af-flex-wrap af-items-end af-gap-2",children:[(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{htmlFor:"new-option-title",className:"af-text-xs af-text-muted-foreground",children:"Option name"}),(0,P.jsx)(me,{id:"new-option-title",value:C,onChange:T=>H(T.target.value),placeholder:"Size",className:"af-w-[140px]",disabled:x})]}),(0,P.jsxs)("div",{className:"af-space-y-1 af-flex-1 af-min-w-[180px]",children:[(0,P.jsx)(pe,{htmlFor:"new-option-values",className:"af-text-xs af-text-muted-foreground",children:"Values (comma-separated)"}),(0,P.jsx)(me,{id:"new-option-values",value:G,onChange:T=>f(T.target.value),placeholder:"S, M, L",disabled:x})]}),(0,P.jsxs)(w,{type:"button",variant:"outline",onClick:Q,disabled:x||!C.trim()||!G.trim(),children:[(0,P.jsx)($t.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"Add option"]})]})]}),(0,P.jsxs)("div",{className:"af-space-y-3",children:[(0,P.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,P.jsx)(pe,{children:"Variants"}),(0,P.jsxs)(w,{type:"button",variant:"outline",size:"sm",onClick:Ne,disabled:x||ve.length===0,children:[(0,P.jsx)($t.Plus,{className:"af-h-4 af-w-4 af-mr-1"}),"Add variant"]})]}),ve.length===0&&(0,P.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Add an option (e.g. Size) above before adding variants."}),j.length===0?(0,P.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No variants yet."}):(0,P.jsxs)(_e,{children:[(0,P.jsx)(ke,{children:(0,P.jsxs)(oe,{children:[(0,P.jsx)(I,{children:"Variant"}),(0,P.jsx)(I,{children:"SKU"}),(0,P.jsx)(I,{children:"Price"}),(0,P.jsx)(I,{children:"Stock"}),(0,P.jsx)(I,{children:"Status"}),(0,P.jsx)(I,{className:"af-text-right",children:"Actions"})]})}),(0,P.jsx)(Ee,{children:j.map(T=>(0,P.jsxs)(oe,{children:[(0,P.jsx)(D,{className:"af-font-medium",children:Xs(T)}),(0,P.jsx)(D,{className:"af-text-muted-foreground",children:T.sku||"-"}),(0,P.jsx)(D,{children:T.unit_amount!=null?(0,P.jsxs)("span",{children:[T.compare_at_amount!=null&&(0,P.jsx)("span",{className:"af-text-muted-foreground af-line-through af-mr-1",children:z(T.compare_at_amount,T.currency)}),z(T.unit_amount,T.currency)]}):T.allow_custom_amount?(0,P.jsx)("span",{className:"af-text-muted-foreground",children:"Buyer chooses"}):"-"}),(0,P.jsx)(D,{children:Dt(T)}),(0,P.jsx)(D,{children:(0,P.jsx)(W,{variant:T.active?"success":"secondary",children:T.active?"Active":"Inactive"})}),(0,P.jsx)(D,{className:"af-text-right",children:(0,P.jsxs)("div",{className:"af-flex af-items-center af-justify-end af-gap-1",children:[(0,P.jsx)(w,{variant:"ghost",size:"sm",onClick:()=>ct(T),disabled:x,children:"Edit"}),(0,P.jsx)(w,{variant:"ghost",size:"icon",onClick:()=>ge(T),disabled:x,title:"Delete variant",children:(0,P.jsx)($t.Trash2,{className:"af-h-4 af-w-4"})})]})})]},T.id))})]})]}),b&&(0,P.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:b}),(0,P.jsx)(ra,{open:m!==null,onOpenChange:T=>{!T&&!fe&&Y(null)},children:(0,P.jsxs)(Gt,{children:[(0,P.jsxs)(Wt,{children:[(0,P.jsx)(Qt,{children:m?.variant?"Edit variant":"Add variant"}),(0,P.jsx)(Zt,{children:"Price, markdown, and stock live on the variant."})]}),m&&(0,P.jsxs)("div",{className:"af-space-y-3",children:[m.variant?m.variant.option_values.length>0&&(0,P.jsxs)("div",{className:"af-text-sm",children:[(0,P.jsx)("span",{className:"af-text-muted-foreground af-mr-2",children:"Options"}),Xs(m.variant)]}):ve.map(T=>(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{className:"af-text-xs af-text-muted-foreground",children:T.title}),(0,P.jsxs)(Lt,{value:m.optionValues[T.id]??"",onValueChange:U=>Y(be=>be&&{...be,optionValues:{...be.optionValues,[T.id]:U}}),children:[(0,P.jsx)(Nt,{children:(0,P.jsx)(Ot,{placeholder:`Choose ${T.title}`})}),(0,P.jsx)(wt,{children:T.values.map(U=>(0,P.jsx)(Fe,{value:U.value,children:U.value},U.id))})]})]},T.id)),ut?(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{className:"af-text-xs af-text-muted-foreground",children:"Price"}),(0,P.jsx)("p",{className:"af-text-sm",children:"Buyer chooses the amount \u2014 this variant has no fixed price."})]}):(0,P.jsxs)("div",{className:"af-flex af-gap-3",children:[(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsxs)(pe,{htmlFor:"variant-price",className:"af-text-xs af-text-muted-foreground",children:["Price (",a.toUpperCase(),")"]}),(0,P.jsx)(me,{id:"variant-price",type:"number",min:"0",step:r,value:m.unitAmount,onChange:T=>Y(U=>U&&{...U,unitAmount:T.target.value}),placeholder:"25.00",className:"af-w-[130px]"})]}),(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{htmlFor:"variant-compare",className:"af-text-xs af-text-muted-foreground",children:"Compare-at (optional)"}),(0,P.jsx)(me,{id:"variant-compare",type:"number",min:"0",step:r,value:m.compareAt,onChange:T=>Y(U=>U&&{...U,compareAt:T.target.value}),placeholder:"40.00",className:"af-w-[130px]"})]})]}),(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{htmlFor:"variant-sku",className:"af-text-xs af-text-muted-foreground",children:"SKU (optional)"}),(0,P.jsx)(me,{id:"variant-sku",value:m.sku,onChange:T=>Y(U=>U&&{...U,sku:T.target.value}),placeholder:"TEE-L-BLACK",className:"af-w-[200px]"})]}),(0,P.jsxs)("div",{className:"af-space-y-2 af-border-t af-pt-3",children:[(0,P.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,P.jsx)("input",{type:"checkbox",checked:m.tracked,onChange:T=>Y(U=>U&&{...U,tracked:T.target.checked}),className:"af-h-4 af-w-4"}),"Track inventory"]}),m.tracked&&(0,P.jsxs)("div",{className:"af-space-y-1 af-pl-6",children:[(0,P.jsxs)("div",{className:"af-flex af-items-end af-gap-3",children:[(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsx)(pe,{htmlFor:"variant-onhand",className:"af-text-xs af-text-muted-foreground",children:"On hand"}),(0,P.jsx)(me,{id:"variant-onhand",type:"number",min:"0",step:"1",value:m.onHand,onChange:T=>Y(U=>U&&{...U,onHand:T.target.value}),placeholder:Ce!=null?"0":void 0,className:"af-w-[100px]"})]}),(0,P.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm af-mb-2",children:[(0,P.jsx)("input",{type:"checkbox",checked:m.allowBackorder,onChange:T=>Y(U=>U&&{...U,allowBackorder:T.target.checked}),className:"af-h-4 af-w-4"}),"Allow backorder"]})]}),Ce!=null&&(0,P.jsxs)("p",{className:"af-text-xs af-text-muted-foreground af-max-w-[300px]",children:["Currently ",Ce," backordered. Leave the field empty to keep the backorder count, or enter a quantity (0 or more) to reset stock and clear it."]})]})]}),(0,P.jsxs)("div",{className:"af-space-y-1",children:[(0,P.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,P.jsx)("input",{type:"checkbox",checked:m.active,onChange:T=>Y(U=>U&&{...U,active:T.target.checked}),className:"af-h-4 af-w-4",disabled:mt}),"Active (buyers can purchase this variant)"]}),mt&&(0,P.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-pl-6",children:"This is the original pre-options variant \u2014 it has no option values, so buyers can't select it. It stays inactive while the product has options; delete all options to use it again."})]}),Pe&&(0,P.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:Pe})]}),(0,P.jsxs)(Kt,{children:[(0,P.jsx)(w,{variant:"outline",onClick:()=>Y(null),disabled:fe,children:"Cancel"}),(0,P.jsxs)(w,{onClick:It,disabled:fe,children:[fe?(0,P.jsx)($t.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):null,m?.variant?"Save":"Add variant"]})]})]})}),(0,P.jsx)(ra,{open:ee!==null,onOpenChange:T=>{!T&&!x&&ge(null)},children:(0,P.jsxs)(Gt,{children:[(0,P.jsxs)(Wt,{children:[(0,P.jsx)(Qt,{children:"Delete variant?"}),(0,P.jsx)(Zt,{children:ee&&`"${Xs(ee)}" will no longer be purchasable. Past orders are unaffected.`})]}),(0,P.jsxs)(Kt,{children:[(0,P.jsx)(w,{variant:"outline",onClick:()=>ge(null),disabled:x,children:"Cancel"}),(0,P.jsx)(w,{variant:"destructive",onClick:Ae,disabled:x,children:"Delete"})]})]})})]})}var A=require("react/jsx-runtime");function oi(e){if(e===0)return"0 B";let a=1024,s=["B","KB","MB","GB"],t=Math.floor(Math.log(e)/Math.log(a));return`${parseFloat((e/Math.pow(a,t)).toFixed(1))} ${s[t]}`}function ms({productsState:e,product:a,currency:s,onSaved:t,className:r}){let{createProduct:c,updateProduct:p,attachProductFile:g,attachProductFileFromUrl:v}=e,y=!!a,n=a?.currency??s,b=Jt(n)?"1":"0.01",S=a!=null&&a.unit_amount===null&&!a.allow_custom_amount,[x,d]=$e.useState(!1),[u,h]=$e.useState(null),R=$e.useRef(!0);$e.useEffect(()=>(R.current=!0,()=>{R.current=!1}),[]);let[C,H]=$e.useState(a?.name??""),[G,f]=$e.useState(a?.description??""),[L,$]=$e.useState(a?.allow_custom_amount?"custom":"fixed"),[Q,q]=$e.useState(a?.unit_amount!=null?String(rt(a.unit_amount,n)):""),[k,ne]=$e.useState(a?.min_amount!=null?String(rt(a.min_amount,n)):""),[m,Y]=$e.useState(a?.max_amount!=null?String(rt(a.max_amount,n)):""),[Pe,xe]=$e.useState(String(a?.max_quantity??10)),fe=$e.useRef({pricingMode:a?.allow_custom_amount?"custom":"fixed",unitAmount:a?.unit_amount!=null?String(rt(a.unit_amount,n)):"",minAmount:a?.min_amount!=null?String(rt(a.min_amount,n)):"",maxAmount:a?.max_amount!=null?String(rt(a.max_amount,n)):"",maxQuantity:String(a?.max_quantity??10)}).current,[Re,ee]=$e.useState(a?.collect_shipping_address??!1),[ge,ve]=$e.useState(a?.active??!0),[j,Ne]=$e.useState(a?.images??[]),[ct,It]=$e.useState(""),[Ae,ut]=$e.useState(a?.fulfillment??null),[mt,Ce]=$e.useState(!1),[Dt,T]=$e.useState(""),[U,be]=$e.useState(null),st=$e.useRef(null),je=x||mt,Ye=()=>{let J=ct.trim();if(J){if(j.includes(J)){h("This image URL is already added");return}Ne(Te=>[...Te,J]),It(""),h(null)}},Ke=J=>{Ne(Te=>Te.filter(Ue=>Ue!==J))},pt=async()=>{if(h(null),!C.trim()){h("Product name is required");return}let J=null,Te=null,Ue=null,zt=!y||L!==fe.pricingMode||(L==="fixed"?Q!==fe.unitAmount:k!==fe.minAmount||m!==fe.maxAmount),gt=!y||Pe!==fe.maxQuantity,ua=S||L==="fixed";if(!(S||!zt))if(L==="fixed"){if(J=Ut(Q,n),J===null||J<=0){h("Enter a valid price");return}}else{if(Te=Ut(k,n),Ue=Ut(m,n),Te===null||Ue===null){h("Enter valid minimum and maximum amounts");return}if(Ue<Te){h("Maximum amount must be at least the minimum amount");return}}let Qe=parseInt(Pe,10);if(ua&>&&(!Number.isFinite(Qe)||Qe<1)){h("Max quantity must be at least 1");return}try{d(!0);let He;if(y&&a){let _={name:C.trim(),description:G.trim()||null,images:j,collect_shipping_address:Re,active:ge};!S&&zt&&(_.allow_custom_amount=L==="custom",L==="fixed"?_.unit_amount=J:(_.min_amount=Te,_.max_amount=Ue)),ua&>&&(_.max_quantity=Qe),He=await p(a.id,_),fe.pricingMode=He.allow_custom_amount?"custom":"fixed",fe.unitAmount=He.unit_amount!=null?String(rt(He.unit_amount,n)):"",fe.minAmount=He.min_amount!=null?String(rt(He.min_amount,n)):"",fe.maxAmount=He.max_amount!=null?String(rt(He.max_amount,n)):"",fe.maxQuantity=String(He.max_quantity)}else{let _={name:C.trim(),allow_custom_amount:L==="custom",collect_shipping_address:Re,active:ge};G.trim()&&(_.description=G.trim()),j.length>0&&(_.images=j),L==="fixed"?(_.unit_amount=J,_.max_quantity=Qe):(_.min_amount=Te,_.max_amount=Ue),He=await c(_)}if(!R.current)return;ut(He.fulfillment),t?.(He)}catch(He){if(!R.current)return;h(He instanceof Error?He.message:"Failed to save product")}finally{R.current&&d(!1)}},xa=async J=>{let Te=J.target.files?.[0];if(!(!Te||!a))try{Ce(!0),be(null);let Ue=await g(a.id,Te);if(!R.current)return;ut(Ue.fulfillment),t?.(Ue)}catch(Ue){if(!R.current)return;be(Ue instanceof Error?Ue.message:"Failed to upload file")}finally{R.current&&(Ce(!1),st.current&&(st.current.value=""))}},Yt=async()=>{let J=Dt.trim();if(!(!J||!a))try{Ce(!0),be(null);let Te=await v(a.id,J);if(!R.current)return;ut(Te.fulfillment),T(""),t?.(Te)}catch(Te){if(!R.current)return;be(Te instanceof Error?Te.message:"Failed to fetch file")}finally{R.current&&Ce(!1)}};return(0,A.jsxs)("div",{className:`af-space-y-6 ${r||""}`,children:[(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{htmlFor:"product-name",children:"Name"}),(0,A.jsx)(me,{id:"product-name",value:C,onChange:J=>H(J.target.value),placeholder:"My product"})]}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{htmlFor:"product-description",children:"Description"}),(0,A.jsx)("textarea",{id:"product-description",value:G,onChange:J=>f(J.target.value),rows:3,placeholder:"Shown on the Stripe checkout page.",className:"af-flex af-w-full af-rounded-md af-border af-border-input af-bg-transparent af-px-3 af-py-2 af-text-sm af-shadow-sm placeholder:af-text-muted-foreground focus-visible:af-outline-none focus-visible:af-ring-1 focus-visible:af-ring-ring"})]}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{children:"Pricing"}),S?(0,A.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"This product is priced per variant. Set prices in the Options & Variants editor below."}):(0,A.jsxs)(A.Fragment,{children:[(0,A.jsxs)(Lt,{value:L,onValueChange:J=>$(J),children:[(0,A.jsx)(Nt,{className:"af-w-[260px]",children:(0,A.jsx)(Ot,{})}),(0,A.jsxs)(wt,{children:[(0,A.jsx)(Fe,{value:"fixed",children:"Fixed price"}),(0,A.jsx)(Fe,{value:"custom",children:"Buyer chooses the amount"})]})]}),L==="fixed"?(0,A.jsx)("div",{className:"af-flex af-items-end af-gap-3",children:(0,A.jsxs)("div",{className:"af-space-y-1",children:[(0,A.jsxs)(pe,{htmlFor:"product-price",className:"af-text-xs af-text-muted-foreground",children:["Price (",n.toUpperCase(),")"]}),(0,A.jsx)(me,{id:"product-price",type:"number",min:"0",step:b,value:Q,onChange:J=>q(J.target.value),placeholder:"29.00",className:"af-w-[140px]"})]})}):(0,A.jsxs)("div",{className:"af-space-y-1",children:[(0,A.jsxs)("div",{className:"af-flex af-items-end af-gap-3",children:[(0,A.jsxs)("div",{className:"af-space-y-1",children:[(0,A.jsxs)(pe,{htmlFor:"product-min-amount",className:"af-text-xs af-text-muted-foreground",children:["Minimum (",n.toUpperCase(),")"]}),(0,A.jsx)(me,{id:"product-min-amount",type:"number",min:"0",step:b,value:k,onChange:J=>ne(J.target.value),placeholder:"1.00",className:"af-w-[140px]"})]}),(0,A.jsxs)("div",{className:"af-space-y-1",children:[(0,A.jsxs)(pe,{htmlFor:"product-max-amount",className:"af-text-xs af-text-muted-foreground",children:["Maximum (",n.toUpperCase(),")"]}),(0,A.jsx)(me,{id:"product-max-amount",type:"number",min:"0",step:b,value:m,onChange:J=>Y(J.target.value),placeholder:"500.00",className:"af-w-[140px]"})]})]}),(0,A.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Buyers pick their own amount within these bounds (tip jar, pay-what-you-want). Quantity is always 1."})]}),(0,A.jsxs)("p",{className:"af-text-xs af-text-muted-foreground",children:["Prices are in your site currency (",n.toUpperCase(),"), which follows the country of your payment account."]})]}),(S||L==="fixed")&&(0,A.jsxs)("div",{className:"af-space-y-1",children:[(0,A.jsx)(pe,{htmlFor:"product-max-quantity",className:"af-text-xs af-text-muted-foreground",children:"Max quantity per order"}),(0,A.jsx)(me,{id:"product-max-quantity",type:"number",min:"1",step:"1",value:Pe,onChange:J=>xe(J.target.value),className:"af-w-[140px]"})]})]}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{children:"Images"}),(0,A.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mb-2",children:"Image URLs shown on the Stripe checkout page."}),(0,A.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-2 af-mb-2",children:[j.map(J=>(0,A.jsxs)(W,{variant:"secondary",className:"af-gap-1 af-max-w-[320px]",children:[(0,A.jsx)("span",{className:"af-truncate",children:J}),(0,A.jsx)("button",{type:"button",onClick:()=>Ke(J),className:"af-ml-1 hover:af-text-destructive",disabled:x,children:(0,A.jsx)(Rt.X,{className:"af-h-3 af-w-3"})})]},J)),j.length===0&&(0,A.jsx)("span",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No images"})]}),(0,A.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,A.jsx)(me,{type:"url",value:ct,onChange:J=>It(J.target.value),placeholder:"https://example.com/image.jpg",onKeyDown:J=>{J.key==="Enter"&&(J.preventDefault(),Ye())},disabled:x}),(0,A.jsx)(w,{type:"button",variant:"outline",onClick:Ye,disabled:!ct.trim()||x,children:(0,A.jsx)(Rt.Plus,{className:"af-h-4 af-w-4"})})]})]}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{children:"Digital File"}),(0,A.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Attach a file (PDF, ZIP, EPUB, image, audio, or video; max 50 MB) and buyers get a download link after paying."}),y?(0,A.jsxs)("div",{className:"af-space-y-3",children:[Ae?.type==="download"&&(0,A.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-p-2 af-bg-muted af-rounded",children:[(0,A.jsx)(Rt.FileText,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,A.jsx)("span",{className:"af-text-sm",children:Ae.filename}),Ae.size!=null&&(0,A.jsxs)("span",{className:"af-text-xs af-text-muted-foreground",children:["(",oi(Ae.size),")"]})]}),Ae?.type==="redirect"&&Ae.url&&(0,A.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-p-2 af-bg-muted af-rounded",children:[(0,A.jsx)(Rt.Link2,{className:"af-h-4 af-w-4 af-text-muted-foreground"}),(0,A.jsxs)("span",{className:"af-text-sm af-truncate",children:["Buyers are sent to ",Ae.url]})]}),(0,A.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,A.jsx)("input",{ref:st,type:"file",onChange:xa,className:"af-hidden",disabled:je}),(0,A.jsxs)(w,{type:"button",variant:"outline",size:"sm",onClick:()=>st.current?.click(),disabled:je,children:[mt?(0,A.jsx)(Rt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,A.jsx)(Rt.Upload,{className:"af-h-4 af-w-4 af-mr-1"}),Ae?.type==="download"?"Replace file":"Upload file"]}),(0,A.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:"or"}),(0,A.jsx)(me,{type:"url",value:Dt,onChange:J=>T(J.target.value),placeholder:"https://example.com/file.pdf",className:"af-flex-1",disabled:mt}),(0,A.jsx)(w,{type:"button",variant:"outline",size:"sm",onClick:Yt,disabled:!Dt.trim()||je,children:"Fetch from URL"})]}),U&&(0,A.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:U})]}):(0,A.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"Save the product first, then attach a file."})]}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsx)(pe,{children:"Settings"}),(0,A.jsxs)("div",{className:"af-space-y-2",children:[(0,A.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,A.jsx)("input",{type:"checkbox",checked:Re,onChange:J=>ee(J.target.checked),className:"af-h-4 af-w-4"}),"Collect a shipping address at checkout"]}),(0,A.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,A.jsx)("input",{type:"checkbox",checked:ge,onChange:J=>ve(J.target.checked),className:"af-h-4 af-w-4"}),"Active (buyers can purchase this product)"]})]})]}),y&&a&&(0,A.jsxs)("div",{className:"af-space-y-2 af-border-t af-pt-6",children:[(0,A.jsx)(pe,{children:"Options & Variants"}),(0,A.jsx)(us,{productId:a.id,currency:n})]}),u&&(0,A.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:u}),(0,A.jsx)("div",{className:"af-flex af-items-center af-gap-2",children:(0,A.jsxs)(w,{type:"button",onClick:pt,disabled:je,children:[x?(0,A.jsx)(Rt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):null,y?"Save changes":"Create product"]})})]})}var nt=se(require("react"),1),_t=require("lucide-react");var Me=se(require("react"),1);var li=["pending","paid","failed","expired","refunded","partially_refunded","disputed","voided"];function ps({initialPageSize:e=25,refreshKey:a}={}){let{api:s}=te(),[t,r]=Me.useState([]),[c,p]=Me.useState(0),[g,v]=Me.useState(!0),[y,n]=Me.useState(null),[b,S]=Me.useState(0),[x,d]=Me.useState(e),[u,h]=Me.useState(void 0),[R,C]=Me.useState(""),[H,G]=Me.useState(""),[f,L]=Me.useState(""),[$,Q]=Me.useState(!1),[q,k]=Me.useState(!1),[ne,m]=Me.useState(void 0),Y=ne?ne.join(","):"",Pe=Me.useMemo(()=>{if(!Y)return;let ee=new Set(Y.split(","));return li.filter(ge=>!ee.has(ge))},[Y]),xe=Me.useRef(0),fe=Me.useCallback(async()=>{let ee=++xe.current;try{v(!0),n(null);let ge={limit:x,offset:b*x};u?ge.status=u:Pe?ge.excludeStatus=Pe:$||(ge.excludeStatus=["pending","expired"]),R.trim()&&(ge.customerEmail=R.trim()),H.trim()&&(ge.from=H.trim()),f.trim()&&(ge.to=f.trim()),q&&(ge.unfulfilledOnly=!0);let ve=await s.getOrders(ge);if(ee!==xe.current)return;r(ve.orders),p(ve.total)}catch(ge){if(ee!==xe.current)return;n(ge instanceof Error?ge:new Error("Failed to fetch orders"))}finally{ee===xe.current&&v(!1)}},[s,b,x,u,Pe,$,q,R,H,f,a]);Me.useEffect(()=>{fe()},[fe]);let Re=Math.ceil(c/x);return{orders:t,total:c,isLoading:g,error:y,page:b,pageSize:x,hasNextPage:b<Re-1,hasPreviousPage:b>0,setPage:S,setPageSize:ee=>{d(ee),S(0)},status:u,setStatus:ee=>{h(ee),S(0)},statusIn:ne,setStatusIn:ee=>{m(ee&&ee.length>0?ee:void 0),S(0)},customerEmail:R,setCustomerEmail:ee=>{C(ee),S(0)},from:H,setFrom:ee=>{G(ee),S(0)},to:f,setTo:ee=>{L(ee),S(0)},showAbandoned:$,setShowAbandoned:ee=>{Q(ee),S(0)},unfulfilledOnly:q,setUnfulfilledOnly:ee=>{k(ee),S(0)},clearFilters:()=>{h(void 0),m(void 0),Q(!1),k(!1),C(""),G(""),L(""),S(0)},refetch:fe}}var Ge=se(require("react"),1),we=require("lucide-react");var Qr=se(require("react"),1),na=se(require("@radix-ui/react-popover"),1);var Ds=require("react/jsx-runtime"),Zr=na.Root,Jr=na.Trigger;var Ys=Qr.forwardRef(({className:e,align:a="end",sideOffset:s=4,...t},r)=>(0,Ds.jsx)(na.Portal,{children:(0,Ds.jsx)("div",{className:"aifeatures-admin",children:(0,Ds.jsx)(na.Content,{ref:r,align:a,sideOffset:s,className:M("af-z-50 af-w-64 af-rounded-md af-border af-bg-popover af-p-1 af-text-popover-foreground af-shadow-md af-outline-none",e),...t})})}));Ys.displayName=na.Content.displayName;var gs=require("react/jsx-runtime"),ci={pending:"Pending",paid:"Paid",failed:"Failed",expired:"Expired",refunded:"Refunded",partially_refunded:"Partially refunded",disputed:"Disputed",voided:"Voided"};function fi(e){return ci[e]??e}function Xr(e){switch(e){case"paid":return"success";case"pending":return"secondary";case"refunded":case"partially_refunded":return"warning";case"disputed":return"destructive";default:return"outline"}}function La({status:e,className:a}){return(0,gs.jsx)(W,{variant:Xr(e),className:a,children:fi(e)})}function Ca({status:e}){return(0,gs.jsx)(La,{status:e})}function qa({order:e,className:a}){return e.line_items.some(s=>s.collectShippingAddress===!0)?typeof e.merchant_fulfilled_at=="string"?(0,gs.jsx)(W,{variant:"outline",className:a,children:"Shipped"}):e.merchant_fulfilled_at===null&&(e.status==="paid"||e.status==="partially_refunded"||e.status==="disputed")?(0,gs.jsx)(W,{variant:"warning",className:a,children:"Needs shipping"}):null:null}function ba(e){return typeof e.order_number=="number"?`#${e.order_number}`:`#${e.id.slice(0,8)}`}var l=require("react/jsx-runtime"),an="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",Oa="af-text-xs af-text-muted-foreground";function er(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{year:"numeric",month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function di(e){return typeof e=="object"&&e!==null&&!Array.isArray(e)}function sn(e){if(e==null)return"";if(typeof e=="string")return e;if(typeof e=="number"||typeof e=="boolean")return String(e);try{return JSON.stringify(e)}catch{return String(e)}}var Ls=24,ui=["paid","partially_refunded"],mi=["paid","partially_refunded","disputed"],pi=/^[A-Za-z0-9 _-]*$/,Yr=100;function sr(e,a,s,t,r){let c=ui.includes(t);return a==="sent"?{label:"Sent",variant:"success",detail:null}:a==="sending"?{label:"Sending",variant:"secondary",detail:"Going out right now."}:a==="failed"?c?{label:"Retrying",variant:"warning",detail:`This did not go out yet. We have used ${s} of ${Ls} tries and keep retrying automatically.`}:{label:"Retries paused",variant:"warning",detail:`This did not go out (${s} of ${Ls} tries used). Nothing is being retried while the order is ${t.replace(/_/g," ")} \u2014 retries resume by themselves if the order goes back to paid.`}:a==="failed_final"?s<Ls&&e==="merchant"?{label:"No recipient set",variant:"destructive",detail:"Nobody was told about this sale: this site has no notification email address set. Add one in your site settings and future sales will notify you."}:s<Ls&&e==="buyer"&&!r?{label:"No buyer email",variant:"destructive",detail:"This order has no buyer email address, so no receipt could be sent. Send the buyer their details another way."}:{label:"Not delivered",variant:"destructive",detail:`We stopped after ${s} attempt${s===1?"":"s"}. This will not be retried automatically \u2014 it needs you to act.`}:a==="suppressed"?{label:"Held back",variant:"outline",detail:`We deliberately did not send this ${t==="refunded"?"because the order was refunded":t==="disputed"?"because the order is disputed":"because the order was not in a sendable state"}. It sends automatically if the order becomes sellable again.`}:a==="none"?{label:"Not sent yet",variant:"secondary",detail:null}:{label:String(a),variant:"secondary",detail:null}}function en({title:e,recipient:a,status:s,attempts:t,side:r,orderStatus:c,action:p}){let g=sr(r,s,t,c,a);return(0,l.jsxs)("div",{children:[(0,l.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,l.jsxs)("div",{className:"af-min-w-0",children:[(0,l.jsx)("div",{className:"af-text-sm",children:e}),a&&(0,l.jsx)("div",{className:"af-text-xs af-text-muted-foreground af-break-all",children:a})]}),(0,l.jsx)(W,{variant:g.variant,className:"af-shrink-0",children:g.label})]}),g.detail&&(0,l.jsx)("p",{className:"af-text-xs af-text-muted-foreground af-mt-1",children:g.detail}),p&&(0,l.jsx)("div",{className:"af-mt-2",children:p})]})}function tn(e){let a=[];typeof e.name=="string"&&e.name&&a.push(e.name);let s=di(e.address)?e.address:null;if(s){for(let r of["line1","line2"]){let c=s[r];typeof c=="string"&&c&&a.push(c)}let t=["city","state","postal_code"].map(r=>s[r]).filter(r=>typeof r=="string"&&!!r).join(", ");t&&a.push(t),typeof s.country=="string"&&s.country&&a.push(s.country)}return a.length>0?a:Object.entries(e).map(([t,r])=>`${t}: ${sn(r)}`)}function gi(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function rr({value:e,label:a,children:s,className:t,size:r="icon"}){let[c,p]=Ge.useState(!1);if(!gi())return null;let g=async()=>{try{await navigator.clipboard.writeText(e),p(!0),setTimeout(()=>p(!1),1500)}catch{}},v=c?(0,l.jsx)(we.Check,{className:"af-h-3 af-w-3"}):(0,l.jsx)(we.Copy,{className:"af-h-3 af-w-3"});return r==="icon"?(0,l.jsx)(w,{variant:"ghost",size:"icon",className:M("af-h-6 af-w-6 af-shrink-0",t),onClick:g,title:a,"aria-label":a,children:v}):(0,l.jsxs)(w,{variant:"outline",size:"sm",className:t,onClick:g,"aria-label":a,children:[c?(0,l.jsx)(we.Check,{className:"af-h-3.5 af-w-3.5 af-mr-1"}):(0,l.jsx)(we.ClipboardCopy,{className:"af-h-3.5 af-w-3.5 af-mr-1"}),c?"Copied":s]})}function tr({label:e,value:a}){return(0,l.jsxs)("div",{children:[(0,l.jsx)("div",{className:Oa,children:e}),(0,l.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,l.jsx)("code",{className:"af-text-xs af-font-mono af-break-all",children:a}),(0,l.jsx)(rr,{value:a,label:`Copy ${e}`})]})]})}function vi({src:e}){let[a,s]=Ge.useState(!1);return!e||a?(0,l.jsx)("div",{"aria-hidden":"true",className:"af-flex af-h-10 af-w-10 af-shrink-0 af-items-center af-justify-center af-rounded-md af-border af-bg-muted",children:(0,l.jsx)(we.Package,{className:"af-h-4 af-w-4 af-text-muted-foreground"})}):(0,l.jsx)("img",{src:e,alt:"","aria-hidden":"true",onError:()=>s(!0),className:"af-h-10 af-w-10 af-shrink-0 af-rounded-md af-border af-object-cover"})}function Ta({title:e,action:a,children:s,className:t}){return(0,l.jsxs)("section",{className:M("af-rounded-lg af-border af-p-4",t),children:[(0,l.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-gap-2",children:[(0,l.jsx)("h4",{className:an,children:e}),a]}),(0,l.jsx)("div",{className:"af-mt-3",children:s})]})}function _a({label:e,emphasis:a,divider:s,children:t}){return(0,l.jsxs)("div",{className:M("af-flex af-items-baseline af-justify-between af-gap-6",s&&"af-mt-2 af-border-t af-pt-2"),children:[(0,l.jsx)("span",{className:a?"af-text-base af-font-semibold":Oa,children:e}),(0,l.jsx)("span",{className:M("af-text-right af-tabular-nums",a?"af-text-base af-font-semibold":"af-text-sm"),children:t})]})}function ar({onSelect:e,disabled:a,icon:s,children:t}){return(0,l.jsxs)(w,{variant:"ghost",size:"sm",className:"af-w-full af-justify-start af-font-normal",onClick:e,disabled:a,children:[(0,l.jsx)("span",{className:"af-mr-2 af-flex af-h-4 af-w-4 af-items-center af-justify-center",children:s}),t]})}function bi(e){let a=[];if(a.push({key:"placed",text:"Order placed",at:e.created_at}),e.paid_at&&a.push({key:"paid",text:`Payment received \u2014 ${z(Ft(e),e.currency)}`,at:e.paid_at}),e.buyer_email_status!=="none"){let s=sr("buyer",e.buyer_email_status,e.buyer_email_attempts,e.status,e.customer_email);a.push({key:"buyer-email",text:e.buyer_email_status==="sent"?`Receipt emailed${e.customer_email?` to ${e.customer_email}`:""}`:`Buyer receipt \u2014 ${s.label.toLowerCase()}`,at:null,tone:s.variant==="destructive"?"destructive":s.variant==="warning"?"warning":void 0})}if(e.merchant_email_status!=="none"){let s=sr("merchant",e.merchant_email_status,e.merchant_email_attempts,e.status,null);a.push({key:"merchant-email",text:e.merchant_email_status==="sent"?"You were notified of this sale":`Sale alert to you \u2014 ${s.label.toLowerCase()}`,at:null,tone:s.variant==="destructive"?"destructive":s.variant==="warning"?"warning":void 0})}return typeof e.merchant_fulfilled_at=="string"&&a.push({key:"shipped",text:e.tracking_number?`Marked shipped \u2014 tracking ${e.tracking_number}`:"Marked shipped",at:e.merchant_fulfilled_at}),e.amount_refunded>0&&a.push({key:"refunded",text:`Refunded ${z(e.amount_refunded,e.currency)}`,at:e.refunded_at,tone:"warning"}),e.status==="disputed"&&a.push({key:"disputed",text:"Payment disputed by the buyer",at:null,tone:"destructive"}),a}function vs({order:e,onBack:a,onRefund:s,onOrderUpdated:t,allowFulfillment:r=!1,onPrevOrder:c,onNextOrder:p,hasPrevOrder:g=!1,hasNextOrder:v=!1}){let{api:y}=te(),[n,b]=Ge.useState(e),[S,x]=Ge.useState(null),[d,u]=Ge.useState(null),[h,R]=Ge.useState(!1),[C,H]=Ge.useState(!1),[G,f]=Ge.useState(!1),[L,$]=Ge.useState(!1),[Q,q]=Ge.useState(""),[k,ne]=Ge.useState(!1),[m,Y]=Ge.useState(null),[Pe,xe]=Ge.useState(!1),[fe,Re]=Ge.useState(""),[ee,ge]=Ge.useState([]),[ve,j]=Ge.useState(!1),[Ne,ct]=Ge.useState(!1),[It,Ae]=Ge.useState(null);Ge.useEffect(()=>{b(e)},[e]);let ut=O=>{b(O),t?.(O)},mt=Object.entries(n.metadata??{}),Ce=Ft(n)-n.amount_refunded,Dt=(n.status==="paid"||n.status==="partially_refunded")&&Ce>0,T=O=>(typeof O.unitAmount=="number"?O.unitAmount:0)*(typeof O.quantity=="number"&&O.quantity>0?O.quantity:1),U=Math.max(0,(n.shipping_total??0)-(n.shipping_refunded??0)),be=ee.reduce((O,Be)=>O+T(n.line_items[Be]??{}),0)+(ve?U:0),st=n.tax_total??0,je=n.automatic_tax&&n.tax_behavior==="exclusive"&&st>0&&n.amount_total+(n.shipping_total??0)>0?Math.min(Math.max(0,st-(n.tax_refunded??0)),Math.round(st*be/(n.amount_total+(n.shipping_total??0)))):0,Ye=be+je,Ke=ee.length>0||ve,pt=Ke?Ye:Ut(fe,n.currency),xa=Jt(n.currency)?"1":"0.01",Yt=async()=>{try{R(!0),x(null),u(null),await y.resendOrderReceipt(n.id);let O=await y.getOrder(n.id);ut(O),u("Receipt email queued.")}catch(O){x(O instanceof Error?O.message:"Failed to resend receipt")}finally{R(!1)}},J=async()=>{try{H(!0),x(null),u(null);let O=await y.resetOrderDownloads(n.id);ut(O),f(!1),u("Download count reset.")}catch(O){x(O instanceof Error?O.message:"Failed to reset downloads")}finally{H(!1)}},Te=n.line_items.some(O=>O.collectShippingAddress===!0),Ue=typeof n.merchant_fulfilled_at=="string",zt=mi.includes(n.status),gt=Q.trim(),ua=pi.test(gt)&>.length<=Yr,Qe=async O=>{try{ne(!0),Y(null),u(null);let Be=await y.setOrderFulfillment(n.id,{fulfilled:O,...O&>?{trackingNumber:gt}:{}});ut(Be),q(""),u(O?"Marked as shipped.":"Marked as not shipped.")}catch(Be){Be instanceof ma&&Be.status===409?Y(`This order is ${n.status.replace(/_/g," ")}, so it can't be marked here. Refresh to see its current state.`):Y(Be instanceof Error?Be.message:"Failed to update the order")}finally{ne(!1)}},He=()=>{Ae(null),Re(String(rt(Ce,n.currency))),ge([]),j(!1),xe(!0)},_=O=>ge(Be=>Be.includes(O)?Be.filter(bt=>bt!==O):[...Be,O]),Oe=async(O,Be)=>{if(s)try{ct(!0),Ae(null),await s(n,O,Be);let bt=await y.getOrder(n.id);ut(bt),xe(!1),u("Refund issued.")}catch(bt){Ae(bt instanceof Error?bt.message:"Refund failed")}finally{ct(!1)}},F=async()=>{if(Ke){if(Ye<=0){Ae("The lines you picked were charged nothing");return}if(Ye>Ce){Ae(`That selection exceeds the refundable remainder (${z(Ce,n.currency)})`);return}await Oe(Ye,{...ee.length>0?{line_items:[...ee].sort((Be,bt)=>Be-bt)}:{},...ve?{shipping:!0}:{}});return}let O=Ut(fe,n.currency);if(O===null||O<=0){Ae("Enter a valid refund amount");return}if(O>Ce){Ae(`Amount exceeds the refundable remainder (${z(Ce,n.currency)})`);return}await Oe(O,{amount:O})},vt=()=>Oe(Ce,{full:!0}),xr=n.fulfillment_type==="download",Nr=r&&Te,On=bi(n),Bn=n.shipping_address?tn(n.shipping_address).join(`
|
|
4
|
+
`):"",wr=n.shipping_total!=null,Sr=!!n.automatic_tax&&n.tax_behavior==="exclusive"&&(n.tax_total??0)>0,An=!(n.automatic_tax&&n.tax_behavior==="exclusive")&&(n.tax_total??0)>0,Pr=wr||Sr,Ja=[];return Ja.push((0,l.jsx)(ar,{icon:h?(0,l.jsx)(we.Loader2,{className:"af-h-4 af-w-4 af-animate-spin"}):(0,l.jsx)(we.Mail,{className:"af-h-4 af-w-4"}),disabled:h||n.fulfillment_status!=="fulfilled",onSelect:()=>{$(!1),Yt()},children:"Resend receipt"},"resend")),xr&&Ja.push((0,l.jsx)(ar,{icon:(0,l.jsx)(we.RotateCcw,{className:"af-h-4 af-w-4"}),disabled:C,onSelect:()=>{$(!1),f(!0)},children:"Reset download count"},"reset")),Dt&&Ja.push((0,l.jsxs)("div",{children:[(0,l.jsx)(ar,{icon:(0,l.jsx)(we.Undo2,{className:"af-h-4 af-w-4"}),disabled:!s,onSelect:()=>{$(!1),He()},children:"Refund\u2026"}),!s&&(0,l.jsx)("p",{className:"af-px-3 af-pb-1 af-text-xs af-text-muted-foreground",children:"Refunds are issued from your site dashboard."})]},"refund")),(0,l.jsxs)("div",{children:[(0,l.jsxs)("div",{className:"af-mb-6",children:[(0,l.jsxs)(w,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,l.jsx)(we.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Orders"]}),(0,l.jsxs)("div",{className:"af-flex af-flex-wrap af-items-start af-justify-between af-gap-3",children:[(0,l.jsxs)("div",{className:"af-min-w-0",children:[(0,l.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,l.jsx)("h3",{className:"af-text-xl af-font-semibold",children:ba(n)}),(0,l.jsx)(La,{status:n.status}),r&&(0,l.jsx)(qa,{order:n}),n.is_test&&(0,l.jsx)(W,{variant:"outline",children:"Test"})]}),(0,l.jsxs)("p",{className:"af-mt-1 af-text-sm af-text-muted-foreground",children:["Placed ",er(n.created_at)]}),n.is_test&&(0,l.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Simulated test purchase \u2014 no payment was collected. Refunding it is simulated too."})]}),(0,l.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(c||p)&&(0,l.jsxs)("div",{className:"af-flex af-items-center",children:[(0,l.jsx)(w,{variant:"outline",size:"icon",className:"af-h-9 af-w-9 af-rounded-r-none",onClick:c,disabled:!g,"aria-label":"Previous order",children:(0,l.jsx)(we.ChevronLeft,{className:"af-h-4 af-w-4"})}),(0,l.jsx)(w,{variant:"outline",size:"icon",className:"af-h-9 af-w-9 af-rounded-l-none af--ml-px",onClick:p,disabled:!v,"aria-label":"Next order",children:(0,l.jsx)(we.ChevronRight,{className:"af-h-4 af-w-4"})})]}),Nr&&(Ue?(0,l.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,l.jsxs)("span",{className:"af-text-sm af-text-muted-foreground",children:["Shipped \u2713",n.merchant_fulfilled_at?` \xB7 ${er(n.merchant_fulfilled_at)}`:""]}),zt&&(0,l.jsxs)(w,{variant:"ghost",size:"sm",onClick:()=>Qe(!1),disabled:k,children:[k?(0,l.jsx)(we.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,l.jsx)(we.Undo2,{className:"af-h-4 af-w-4 af-mr-1"}),"Undo"]})]}):zt?(0,l.jsxs)("div",{className:"af-flex af-flex-nowrap af-items-center af-gap-2",children:[(0,l.jsx)(me,{value:Q,onChange:O=>q(O.target.value),placeholder:"Tracking number (optional)",maxLength:Yr,className:"af-w-[190px] af-shrink-0","aria-label":"Tracking number (optional)",disabled:k}),(0,l.jsxs)(w,{variant:"default",className:"af-shrink-0",onClick:()=>Qe(!0),disabled:k||!ua,children:[k?(0,l.jsx)(we.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,l.jsx)(we.Truck,{className:"af-h-4 af-w-4 af-mr-1"}),"Mark as shipped"]})]}):null),Ja.length>0&&(0,l.jsxs)(Zr,{open:L,onOpenChange:$,children:[(0,l.jsx)(Jr,{asChild:!0,children:(0,l.jsx)(w,{variant:"outline",size:"icon","aria-label":"More actions",children:(0,l.jsx)(we.MoreHorizontal,{className:"af-h-4 af-w-4"})})}),(0,l.jsx)(Ys,{children:(0,l.jsx)("div",{className:"af-space-y-0.5",children:Ja})})]})]})]}),Nr&&!Ue&&!ua&&(0,l.jsx)("p",{className:"af-mt-2 af-text-xs af-text-destructive",children:"Tracking numbers can use letters, numbers, spaces, dashes and underscores \u2014 up to 100 characters."}),m&&(0,l.jsx)("div",{className:"af-mt-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2 af-text-sm af-text-destructive",children:m}),d&&(0,l.jsx)("div",{className:"af-mt-2 af-rounded af-border af-border-green-200 af-bg-green-50 af-px-3 af-py-2 af-text-sm af-text-green-800",children:d}),S&&(0,l.jsx)("div",{className:"af-mt-2 af-rounded af-bg-destructive/10 af-px-3 af-py-2 af-text-sm af-text-destructive",children:S})]}),(0,l.jsxs)("div",{className:"af-grid af-gap-6 lg:af-grid-cols-[1fr_320px]",children:[(0,l.jsxs)("div",{className:"af-space-y-6",children:[n.line_items.length>0&&(0,l.jsx)(Ta,{title:"Items",children:(0,l.jsx)("ul",{className:"af-space-y-3",children:n.line_items.map((O,Be)=>{let bt=typeof O.name=="string"&&O.name?O.name:"Item",Rr=typeof O.quantity=="number"&&O.quantity>0?O.quantity:1,Cr=typeof O.unitAmount=="number";return(0,l.jsxs)("li",{className:"af-flex af-items-start af-gap-3",children:[(0,l.jsx)(vi,{src:typeof O.imageUrl=="string"&&O.imageUrl?O.imageUrl:void 0}),(0,l.jsxs)("div",{className:"af-min-w-0 af-flex-1",children:[(0,l.jsxs)("div",{className:"af-text-sm af-font-medium",children:[bt,typeof O.variantTitle=="string"&&O.variantTitle&&(0,l.jsxs)("span",{className:"af-font-normal af-text-muted-foreground",children:[" \u2014 ",O.variantTitle]})]}),(0,l.jsxs)("div",{className:Oa,children:[Rr," \xD7"," ",Cr?z(O.unitAmount,n.currency):"\u2014"]}),O.refunded===!0&&(0,l.jsx)(W,{variant:"secondary",className:"af-mt-1",children:O.refundReason==="oversold"?"Sold out \u2014 refunded":"Refunded"})]}),(0,l.jsx)("div",{className:"af-shrink-0 af-text-sm af-tabular-nums",children:Cr?z(O.unitAmount*Rr,n.currency):"\u2014"})]},Be)})})}),mt.length>0&&(0,l.jsx)(Ta,{title:"Order details",children:(0,l.jsx)("dl",{className:"af-space-y-2",children:mt.map(([O,Be])=>(0,l.jsxs)("div",{children:[(0,l.jsx)("dt",{className:M(Oa,"af-capitalize"),children:O.replace(/_/g," ")}),(0,l.jsx)("dd",{className:"af-text-sm af-break-words",children:sn(Be)})]},O))})}),(0,l.jsx)(Ta,{title:"Totals",children:(0,l.jsxs)("div",{className:"af-w-full",children:[Pr&&(0,l.jsx)(_a,{label:"Subtotal",children:z(n.amount_total,n.currency)}),wr&&(0,l.jsx)(_a,{label:"Shipping",children:n.shipping_total===0?"Free":z(n.shipping_total??0,n.currency)}),Sr&&(0,l.jsx)(_a,{label:"Tax",children:z(n.tax_total??0,n.currency)}),(0,l.jsx)(_a,{label:"Total",emphasis:!0,divider:Pr,children:z(Ft(n),n.currency)}),An&&(0,l.jsx)(_a,{label:"incl. tax",children:z(n.tax_total??0,n.currency)}),n.base_currency!=null&&n.base_amount_total!=null&&(0,l.jsxs)(_a,{label:"Your price",children:[z(n.base_amount_total,n.base_currency),(0,l.jsxs)("span",{className:"af-block af-text-xs af-text-muted-foreground",children:["buyer paid in ",n.currency.toUpperCase()]})]}),n.amount_refunded>0&&(0,l.jsxs)(l.Fragment,{children:[(0,l.jsxs)(_a,{label:"Refunded",children:["\u2212",z(n.amount_refunded,n.currency),(n.shipping_refunded??0)>0&&(0,l.jsxs)("span",{className:"af-block af-text-xs af-text-muted-foreground",children:["incl."," ",z(n.shipping_refunded??0,n.currency)," ","shipping"]})]}),(0,l.jsx)(_a,{label:"Net",children:(0,l.jsx)("span",{className:"af-font-medium",children:z(Math.max(0,Ft(n)-n.amount_refunded),n.currency)})})]})]})}),(0,l.jsx)(Ta,{title:"Timeline",children:(0,l.jsx)("ol",{className:"af-space-y-3",children:On.map(O=>(0,l.jsxs)("li",{className:"af-flex af-gap-3",children:[(0,l.jsx)("span",{"aria-hidden":"true",className:M("af-mt-1.5 af-h-2 af-w-2 af-shrink-0 af-rounded-full",O.tone==="destructive"?"af-bg-destructive":O.tone==="warning"?"af-bg-yellow-500":"af-bg-muted-foreground/40")}),(0,l.jsxs)("div",{className:"af-min-w-0",children:[(0,l.jsx)("div",{className:"af-text-sm af-break-words",children:O.text}),O.at&&(0,l.jsx)("div",{className:"af-text-xs af-text-muted-foreground",children:er(O.at)})]})]},O.key))})})]}),(0,l.jsxs)("div",{className:"af-space-y-6",children:[(0,l.jsx)(Ta,{title:"Customer",children:(0,l.jsxs)("div",{className:"af-space-y-2",children:[(0,l.jsxs)("div",{children:[(0,l.jsx)("div",{className:Oa,children:"Email"}),(0,l.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,l.jsx)("span",{className:"af-min-w-0 af-break-all af-text-sm",children:n.customer_email||(0,l.jsx)("span",{className:"af-italic af-text-muted-foreground",children:"Unknown"})}),n.customer_email&&(0,l.jsx)(rr,{value:n.customer_email,label:"Copy email"})]})]}),n.customer_name&&(0,l.jsxs)("div",{children:[(0,l.jsx)("div",{className:Oa,children:"Name"}),(0,l.jsx)("div",{className:"af-text-sm",children:n.customer_name})]}),n.customer&&(0,l.jsxs)("div",{children:[(0,l.jsx)("div",{className:Oa,children:"Customer record"}),(0,l.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,l.jsx)("span",{className:"af-break-all af-text-sm",children:n.customer.name||n.customer.email}),(0,l.jsx)(W,{variant:"secondary",children:"Linked"})]})]})]})}),n.shipping_address&&(0,l.jsx)(Ta,{title:"Ship to",action:(0,l.jsx)(rr,{value:Bn,label:"Copy address",size:"sm",children:"Copy address"}),children:(0,l.jsx)("address",{className:"af-space-y-0.5 af-text-sm af-not-italic",children:tn(n.shipping_address).map((O,Be)=>(0,l.jsx)("div",{children:O},Be))})}),(0,l.jsx)(Ta,{title:"Emails",children:(0,l.jsxs)("div",{className:"af-space-y-4",children:[(0,l.jsx)(en,{side:"buyer",title:"Receipt to buyer",recipient:n.customer_email,status:n.buyer_email_status,attempts:n.buyer_email_attempts,orderStatus:n.status,action:(0,l.jsxs)(w,{variant:"outline",size:"sm",onClick:Yt,disabled:h||n.fulfillment_status!=="fulfilled",children:[h?(0,l.jsx)(we.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,l.jsx)(we.Mail,{className:"af-h-4 af-w-4 af-mr-1"}),"Resend"]})}),(0,l.jsx)(en,{side:"merchant",title:"Sale alert to you",recipient:null,status:n.merchant_email_status,attempts:n.merchant_email_attempts,orderStatus:n.status})]})}),xr&&(0,l.jsx)(Ta,{title:"Download",children:(0,l.jsxs)("div",{className:"af-space-y-2",children:[(0,l.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,l.jsx)(we.Download,{className:"af-h-4 af-w-4"}),(0,l.jsxs)("span",{children:["Downloaded ",n.download_count," time",n.download_count===1?"":"s"]})]}),n.download_url&&(0,l.jsx)("a",{href:n.download_url,target:"_blank",rel:"noreferrer",className:"af-inline-block af-text-sm af-underline af-underline-offset-2",children:"Buyer download link"})]})}),(0,l.jsxs)("details",{className:"af-rounded-lg af-border af-p-4",children:[(0,l.jsx)("summary",{className:M(an,"af-cursor-pointer af-select-none"),children:"Technical details"}),(0,l.jsxs)("div",{className:"af-mt-3 af-space-y-2",children:[(0,l.jsx)(tr,{label:"Order ID",value:n.id}),n.stripe_checkout_session_id&&(0,l.jsx)(tr,{label:"Checkout session",value:n.stripe_checkout_session_id}),n.stripe_payment_intent_id&&(0,l.jsx)(tr,{label:"Payment",value:n.stripe_payment_intent_id})]})]})]})]}),(0,l.jsx)(ra,{open:G,onOpenChange:O=>{!O&&!C&&f(!1)},children:(0,l.jsxs)(Gt,{children:[(0,l.jsxs)(Wt,{children:[(0,l.jsx)(Qt,{children:"Reset download count?"}),(0,l.jsx)(Zt,{children:"The buyer's download link stops working after it reaches the download limit. Resetting the count lets them download again."})]}),(0,l.jsxs)(Kt,{children:[(0,l.jsx)(w,{variant:"outline",onClick:()=>f(!1),disabled:C,children:"Cancel"}),(0,l.jsx)(w,{onClick:J,disabled:C,children:C?"Resetting...":"Reset"})]})]})}),(0,l.jsx)(ra,{open:Pe,onOpenChange:O=>{!O&&!Ne&&xe(!1)},children:(0,l.jsxs)(Gt,{children:[(0,l.jsxs)(Wt,{children:[(0,l.jsx)(Qt,{children:"Refund order"}),(0,l.jsxs)(Zt,{children:["Refund up to ",z(Ce,n.currency)," to the buyer's original payment method. Stripe's processing fee is not returned."]})]}),(0,l.jsxs)("div",{className:"af-space-y-4",children:[(n.line_items.length>0||U>0)&&(0,l.jsxs)("div",{className:"af-space-y-2",children:[(0,l.jsx)(pe,{children:"Items"}),(0,l.jsxs)("div",{className:"af-space-y-2",children:[n.line_items.map((O,Be)=>{let bt=O.refunded===!0;return(0,l.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,l.jsx)("input",{type:"checkbox",checked:ee.includes(Be),onChange:()=>_(Be),disabled:Ne||bt,className:"af-h-4 af-w-4"}),(0,l.jsxs)("span",{className:bt?"af-text-muted-foreground af-line-through":void 0,children:[typeof O.name=="string"&&O.name?O.name:"Item",typeof O.variantTitle=="string"&&O.variantTitle?` \u2014 ${O.variantTitle}`:"",typeof O.quantity=="number"&&O.quantity>1?` \xD7 ${O.quantity}`:""]}),(0,l.jsx)("span",{className:"af-ml-auto af-text-muted-foreground",children:bt?"Refunded":z(T(O),n.currency)})]},Be)}),U>0&&(0,l.jsxs)("label",{className:"af-flex af-items-center af-gap-2 af-text-sm",children:[(0,l.jsx)("input",{type:"checkbox",checked:ve,onChange:O=>j(O.target.checked),disabled:Ne,className:"af-h-4 af-w-4"}),(0,l.jsx)("span",{children:"Shipping"}),(0,l.jsx)("span",{className:"af-ml-auto af-text-muted-foreground",children:z(U,n.currency)})]})]})]}),(0,l.jsxs)("div",{className:"af-space-y-2",children:[(0,l.jsxs)(pe,{htmlFor:"refund-amount",children:["Amount (",n.currency.toUpperCase(),")"]}),(0,l.jsx)(me,{id:"refund-amount",type:"number",min:"0",step:xa,value:Ke?String(rt(Ye,n.currency)):fe,onChange:O=>Re(O.target.value),disabled:Ne||Ke}),Ke&&(0,l.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Priced from the order. Clear the ticks above to refund a different amount."})]})]}),It&&(0,l.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:It}),(0,l.jsxs)(Kt,{children:[(0,l.jsx)(w,{variant:"outline",onClick:()=>xe(!1),disabled:Ne,children:"Cancel"}),(0,l.jsx)(w,{variant:"outline",onClick:vt,disabled:Ne,children:"Refund everything"}),(0,l.jsx)(w,{variant:"destructive",onClick:F,disabled:Ne,children:Ne?"Refunding...":pt!==null&&pt>0?`Refund ${z(pt,n.currency)}`:"Refund"})]})]})})]})}var Os=se(require("react"),1),Bs=require("lucide-react");var at=require("react/jsx-runtime");function Ct({title:e,onBackToWebsite:a,hideBackButton:s=!1,children:t,className:r}){let c=Os.useRef(null);return Os.useEffect(()=>{c.current?.focus()},[]),(0,at.jsxs)("div",{className:M("af-bg-background",r),children:[(0,at.jsxs)("div",{className:"af-sticky af-top-0 af-z-10 lg:af-static af-flex af-items-center af-justify-between af-gap-2 af-border-b af-bg-background af-px-4 af-py-2",children:[(0,at.jsx)("h2",{ref:c,tabIndex:-1,"aria-live":"polite",className:"af-text-lg af-font-semibold focus:af-outline-none",children:e}),a&&!s&&(0,at.jsxs)(w,{type:"button",variant:"outline",size:"sm",onClick:a,children:[(0,at.jsx)(Bs.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to website"]})]}),(0,at.jsx)("div",{className:"af-p-4",children:t})]})}function K({className:e}){return(0,at.jsx)("div",{"aria-hidden":"true",className:M("af-h-4 af-w-16 af-animate-pulse af-rounded af-bg-muted",e)})}function Ve({label:e,children:a,detail:s,valueClassName:t}){return(0,at.jsxs)("div",{className:"af-rounded-md af-border af-px-3 af-py-2",children:[(0,at.jsx)("div",{className:"af-text-xs af-text-muted-foreground",children:e}),(0,at.jsx)("div",{className:M("af-mt-1 af-text-lg af-font-semibold",t),children:a}),s&&(0,at.jsx)("div",{className:"af-mt-0.5 af-text-xs af-text-muted-foreground",children:s})]})}function Tt({message:e,onRetry:a}){return(0,at.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,at.jsx)("span",{children:e}),(0,at.jsxs)(w,{type:"button",variant:"ghost",size:"sm",onClick:a,children:[(0,at.jsx)(Bs.RefreshCw,{className:"af-h-4 af-w-4 af-mr-1"}),"Try again"]})]})}function ia({icon:e,children:a}){return(0,at.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[e,(0,at.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:a})]})}var N=require("react/jsx-runtime"),yi={needs_shipping:"Needs shipping",all:"All",paid:"Paid",refunds:"Refunds & disputes"},rn={needs_shipping:{unfulfilledOnly:!0},all:{unfulfilledOnly:!1},paid:{statusIn:["paid","partially_refunded"],unfulfilledOnly:!1},refunds:{statusIn:["refunded","partially_refunded","disputed"],unfulfilledOnly:!1}};function nn(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function on(e){let a=Array.isArray(e.line_items)?e.line_items:[],s=a[0];if(!s)return"\u2014";let t=typeof s.name=="string"&&s.name?s.name:"Item",r=typeof s.quantity=="number"&&s.quantity>0?s.quantity:1,c=r>1?`${t} \xD7${r}`:t;return a.length>1?`${c} +${a.length-1} more`:c}function As(e){return e.customer?.name||e.customer_name||null}function Ba({onRefund:e,className:a,refreshKey:s,initialOrderId:t,showFulfillment:r=!1,emptyMessage:c,onDetailOpenChange:p}){let{api:g}=te(),v=ps({refreshKey:s}),{orders:y,total:n,isLoading:b,error:S,page:x,pageSize:d,hasNextPage:u,hasPreviousPage:h,setPage:R,setPageSize:C,customerEmail:H,setCustomerEmail:G,setStatusIn:f,from:L,setFrom:$,to:Q,setTo:q,showAbandoned:k,setShowAbandoned:ne,setUnfulfilledOnly:m,refetch:Y}=v,[Pe,xe]=nt.useState("list"),[fe,Re]=nt.useState(null),[ee,ge]=nt.useState(""),[ve,j]=nt.useState(""),[Ne,ct]=nt.useState(r?"needs_shipping":"all"),[It,Ae]=nt.useState(!1),[ut,mt]=nt.useState(!!t),[Ce,Dt]=nt.useState(null);nt.useEffect(()=>{if(!t)return;let F=!1;return mt(!0),Dt(null),g.getOrder(t).then(vt=>{F||(Re(vt),xe("detail"))}).catch(()=>{F||Dt("Couldn't open that order \u2014 showing all orders.")}).finally(()=>{F||mt(!1)}),()=>{F=!0}},[g,t]);let T=nt.useCallback(F=>{ct(F);let vt=rn[F];f(vt.statusIn),m(vt.unfulfilledOnly)},[f,m]);nt.useEffect(()=>{r&&m(!0)},[]);let U=nt.useRef(!r);nt.useEffect(()=>{U.current||b||(U.current=!0,y.length===0&&T("all"))},[b,y.length,T]);let be=Pe==="detail"&&!!fe;nt.useEffect(()=>{p?.(be)},[be,p]);let st=(L?1:0)+(Q?1:0)+(k?1:0),je=st>0||!!H||!!ve,Ye=!!L&&!!Q&&L>Q,Ke=F=>{Re(F),xe("detail")},pt=()=>{xe("list"),Re(null),Y()},xa=F=>{let vt=F.trim();if(!vt){j(""),H&&G("");return}if(vt.startsWith("#")||/^\d+$/.test(vt)){j(vt),H&&G("");return}j(""),G(vt)},Yt=()=>{ge(""),j(""),G(""),$(""),q(""),ne(!1),Ae(!1)},J=ve.replace(/^#/,"").toLowerCase(),Te=J?y.filter(F=>String(F.order_number??"")===J||F.id.toLowerCase().startsWith(J)):y,Ue=fe?y.findIndex(F=>F.id===fe.id):-1,zt=Ue>0,gt=Ue>=0&&Ue<y.length-1;if(ut)return(0,N.jsx)("div",{className:a,children:(0,N.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,N.jsx)(_t.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})})});if(Pe==="detail"&&fe)return(0,N.jsx)("div",{className:a,children:(0,N.jsx)(vs,{order:fe,onBack:pt,onRefund:e,onOrderUpdated:F=>Re(F),allowFulfillment:r,onPrevOrder:zt?()=>Re(y[Ue-1]):void 0,onNextOrder:gt?()=>Re(y[Ue+1]):void 0,hasPrevOrder:zt,hasNextOrder:gt},fe.id)});let Qe=(0,N.jsxs)("div",{className:"af-mb-4 af-space-y-3",children:[Ce&&(0,N.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:Ce}),(0,N.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,N.jsx)("div",{role:"group","aria-label":"Order views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:(r?["needs_shipping","all","paid","refunds"]:["all","paid","refunds"]).map(F=>(0,N.jsx)("button",{type:"button","aria-pressed":Ne===F,onClick:()=>T(F),className:M("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",Ne===F?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:yi[F]},F))}),(0,N.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,N.jsx)(_t.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,N.jsx)(me,{type:"text",value:ee,onChange:F=>ge(F.target.value),onBlur:()=>xa(ee),onKeyDown:F=>{F.key==="Enter"&&(F.preventDefault(),xa(ee))},placeholder:"Search email, name, or order #","aria-label":"Search orders by email, name, or order number",className:"af-pl-8"})]}),(0,N.jsxs)(w,{type:"button",variant:st>0?"secondary":"ghost",size:"sm",onClick:()=>Ae(F=>!F),"aria-expanded":It,children:[(0,N.jsx)(_t.SlidersHorizontal,{className:"af-h-4 af-w-4 af-mr-1"}),"Filters",st>0&&(0,N.jsx)(W,{variant:"secondary",className:"af-ml-2",children:st})]}),je&&(0,N.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:Yt,children:"Clear filters"})]}),(It||!!L||!!Q)&&(0,N.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2 af-rounded-md af-border af-px-3 af-py-2",children:[(0,N.jsx)(pe,{htmlFor:"orders-from",className:"af-text-xs af-text-muted-foreground",children:"From"}),(0,N.jsx)(me,{id:"orders-from",type:"date",value:L,max:Q||void 0,onChange:F=>$(F.target.value),className:"af-w-[170px]"}),(0,N.jsx)(pe,{htmlFor:"orders-to",className:"af-text-xs af-text-muted-foreground",children:"To"}),(0,N.jsx)(me,{id:"orders-to",type:"date",value:Q,min:L||void 0,onChange:F=>q(F.target.value),className:"af-w-[170px]"}),!rn[Ne].statusIn&&(0,N.jsx)(w,{type:"button",variant:k?"secondary":"ghost",size:"sm",onClick:()=>ne(!k),"aria-pressed":k,children:k?"Hide abandoned":"Show abandoned"})]}),Ye&&(0,N.jsx)("p",{className:"af-text-xs af-text-destructive",children:"The end date is before the start date, so nothing can match."})]});if(b)return(0,N.jsxs)("div",{className:a,children:[Qe,(0,N.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(F=>(0,N.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,N.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,N.jsx)(K,{className:"af-h-4 af-w-16"}),(0,N.jsx)(K,{className:"af-h-4 af-w-14"})]}),(0,N.jsx)(K,{className:"af-mt-2 af-h-3 af-w-40"}),(0,N.jsx)(K,{className:"af-mt-2 af-h-3 af-w-24"})]},F))}),(0,N.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,N.jsxs)(_e,{children:[(0,N.jsx)(ke,{children:(0,N.jsxs)(oe,{children:[(0,N.jsx)(I,{children:"Order"}),(0,N.jsx)(I,{children:"Date"}),(0,N.jsx)(I,{children:"Customer"}),(0,N.jsx)(I,{children:"Items"}),(0,N.jsx)(I,{className:"af-text-right",children:"Total"}),(0,N.jsx)(I,{children:"Status"}),(0,N.jsx)(I,{className:"af-w-8"})]})}),(0,N.jsx)(Ee,{"aria-hidden":"true",children:[0,1,2,3,4].map(F=>(0,N.jsxs)(oe,{children:[(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-h-4 af-w-14"})}),(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-h-4 af-w-40"})}),(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-h-4 af-w-32"})}),(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-ml-auto af-h-4 af-w-16"})}),(0,N.jsx)(D,{children:(0,N.jsx)(K,{className:"af-h-5 af-w-16"})}),(0,N.jsx)(D,{})]},F))})]})}),(0,N.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading orders"})]});if(S)return(0,N.jsxs)("div",{className:a,children:[Qe,(0,N.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-gap-2 af-py-8",children:[(0,N.jsx)("div",{className:"af-text-sm af-text-muted-foreground",children:"Couldn't load orders."}),(0,N.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>Y(),children:"Try again"})]})]});let He=Ne!=="all";if(Te.length===0&&x===0)return(0,N.jsxs)("div",{className:a,children:[(je||He)&&Qe,(0,N.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[(0,N.jsx)(_t.Receipt,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,N.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:je||He?"No orders match these filters.":c??(0,N.jsxs)(N.Fragment,{children:["No orders yet.",(0,N.jsx)("br",{}),"Orders will appear here when someone buys from your website."]})}),!je&&!He&&(0,N.jsx)(w,{type:"button",variant:"ghost",size:"sm",className:"af-mt-2",onClick:()=>ne(!0),children:"Show abandoned checkouts"})]})]});let _=x*d+1,Oe=Math.min((x+1)*d,n);return(0,N.jsxs)("div",{className:a,children:[Qe,(0,N.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:Te.map(F=>(0,N.jsxs)("button",{type:"button",onClick:()=>Ke(F),className:"af-block af-w-full af-rounded-md af-border af-p-3 af-text-left hover:af-bg-muted/50",children:[(0,N.jsxs)("div",{className:"af-flex af-items-baseline af-justify-between af-gap-2",children:[(0,N.jsx)("span",{className:"af-font-medium",children:ba(F)}),(0,N.jsx)("span",{className:"af-shrink-0 af-font-medium af-tabular-nums",children:z(Ft(F),F.currency)})]}),(0,N.jsxs)("div",{className:"af-mt-1 af-min-w-0",children:[(0,N.jsxs)("div",{className:"af-truncate af-text-sm",children:[F.customer_email||(0,N.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"}),As(F)&&(0,N.jsxs)("span",{className:"af-text-muted-foreground",children:[" \xB7 ",As(F)]})]}),(0,N.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:on(F)}),F.amount_refunded>0&&(0,N.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["\u2212",z(F.amount_refunded,F.currency)," refunded"]})]}),(0,N.jsxs)("div",{className:"af-mt-2 af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,N.jsx)(La,{status:F.status}),F.is_test&&(0,N.jsx)(W,{variant:"outline",children:"Test"}),r&&(0,N.jsx)(qa,{order:F}),(0,N.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:nn(F.created_at)})]})]},F.id))}),(0,N.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,N.jsxs)(_e,{children:[(0,N.jsx)(ke,{children:(0,N.jsxs)(oe,{children:[(0,N.jsx)(I,{children:"Order"}),(0,N.jsx)(I,{children:"Date"}),(0,N.jsx)(I,{children:"Customer"}),(0,N.jsx)(I,{children:"Items"}),(0,N.jsx)(I,{className:"af-text-right",children:"Total"}),(0,N.jsx)(I,{children:"Status"}),(0,N.jsx)(I,{className:"af-w-8"})]})}),(0,N.jsx)(Ee,{children:Te.map(F=>(0,N.jsxs)(oe,{onClick:()=>Ke(F),className:"af-cursor-pointer focus-within:af-bg-muted/50",children:[(0,N.jsxs)(D,{className:"af-whitespace-nowrap af-font-medium",children:[(0,N.jsx)("button",{type:"button",onClick:vt=>{vt.stopPropagation(),Ke(F)},"aria-label":`Open order ${ba(F)}`,className:"af-rounded-sm af-text-left hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:ba(F)}),F.is_test&&(0,N.jsx)(W,{variant:"outline",className:"af-ml-2",children:"Test"})]}),(0,N.jsx)(D,{className:"af-whitespace-nowrap af-text-muted-foreground",children:nn(F.created_at)}),(0,N.jsxs)(D,{className:"af-max-w-[220px]",children:[(0,N.jsx)("div",{className:"af-truncate",children:F.customer_email||(0,N.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"})}),As(F)&&(0,N.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:As(F)})]}),(0,N.jsx)(D,{className:"af-max-w-[220px]",children:(0,N.jsx)("div",{className:"af-truncate af-text-muted-foreground",children:on(F)})}),(0,N.jsxs)(D,{className:"af-whitespace-nowrap af-text-right",children:[(0,N.jsx)("div",{className:"af-font-medium af-tabular-nums",children:z(Ft(F),F.currency)}),F.amount_refunded>0&&(0,N.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["\u2212",z(F.amount_refunded,F.currency)," ","refunded"]})]}),(0,N.jsx)(D,{children:(0,N.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,N.jsx)(La,{status:F.status}),r&&(0,N.jsx)(qa,{order:F})]})}),(0,N.jsx)(D,{className:"af-w-8 af-text-right",children:(0,N.jsx)(_t.ChevronRight,{"aria-hidden":"true",className:"af-h-4 af-w-4 af-text-muted-foreground/50"})})]},F.id))})]})}),(0,N.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,N.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,N.jsx)("span",{children:J?`${Te.length} matching this page`:`Showing ${_}-${Oe} of ${n}`}),(0,N.jsxs)(Lt,{value:String(d),onValueChange:F=>C(Number(F)),children:[(0,N.jsx)(Nt,{className:"af-w-[70px] af-h-8",children:(0,N.jsx)(Ot,{})}),(0,N.jsxs)(wt,{children:[(0,N.jsx)(Fe,{value:"10",children:"10"}),(0,N.jsx)(Fe,{value:"25",children:"25"}),(0,N.jsx)(Fe,{value:"50",children:"50"}),(0,N.jsx)(Fe,{value:"100",children:"100"})]})]}),(0,N.jsx)("span",{children:"per page"})]}),(0,N.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,N.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>R(x-1),disabled:!h,children:[(0,N.jsx)(_t.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,N.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>R(x+1),disabled:!u,children:["Next",(0,N.jsx)(_t.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var Us=se(require("react"),1),oa=require("lucide-react");var ht=se(require("react"),1);function bs({initialPageSize:e=25}={}){let{api:a}=te(),[s,t]=ht.useState([]),[r,c]=ht.useState(0),[p,g]=ht.useState(!0),[v,y]=ht.useState(null),[n,b]=ht.useState(0),[S,x]=ht.useState(e),[d,u]=ht.useState(""),h=ht.useRef(0),R=ht.useCallback(async()=>{let H=++h.current;try{g(!0),y(null);let G={limit:S,offset:n*S};d.trim()&&(G.email=d.trim());let f=await a.getCustomers(G);if(H!==h.current)return;t(f.customers),c(f.total)}catch(G){if(H!==h.current)return;y(G instanceof Error?G:new Error("Failed to fetch customers"))}finally{H===h.current&&g(!1)}},[a,n,S,d]);ht.useEffect(()=>{R()},[R]);let C=Math.ceil(r/S);return{customers:s,total:r,isLoading:p,error:v,page:n,pageSize:S,hasNextPage:n<C-1,hasPreviousPage:n>0,setPage:b,setPageSize:H=>{x(H),b(0)},email:d,setEmail:H=>{u(H),b(0)},refetch:R}}var Aa=se(require("react"),1),Fs=require("lucide-react");var re=require("react/jsx-runtime");function ln(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function hi(e){let a=[];e.name&&a.push(e.name),a.push(e.line1),e.line2&&a.push(e.line2);let s=[e.city,e.state,e.postal_code].filter(Boolean).join(", ");return s&&a.push(s),e.country&&a.push(e.country),a}function ys({customer:e,onBack:a,className:s}){let{api:t}=te(),[r,c]=Aa.useState(null),[p,g]=Aa.useState([]),[v,y]=Aa.useState(!0),[n,b]=Aa.useState(null);Aa.useEffect(()=>{let d=!1;return y(!0),b(null),Promise.all([t.getCustomer(e.id),t.getOrders({customerEmail:e.email,excludeStatus:["pending","expired"],limit:100})]).then(([u,h])=>{d||(c(u),g(h.orders))}).catch(u=>{d||b(u instanceof Error?u.message:"Failed to load customer")}).finally(()=>{d||y(!1)}),()=>{d=!0}},[t,e.id,e.email]);let S=r?.addresses??[],x=r?.order_count??p.length;return(0,re.jsxs)("div",{className:s,children:[(0,re.jsxs)("div",{className:"af-mb-6",children:[(0,re.jsxs)(w,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,re.jsx)(Fs.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Customers"]}),(0,re.jsx)("h3",{className:"af-text-lg af-font-semibold",children:e.name||e.email}),(0,re.jsxs)("p",{className:"af-text-sm af-text-muted-foreground",children:["Customer since ",ln(e.created_at)]})]}),v?(0,re.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,re.jsx)(Fs.Loader2,{className:"af-h-4 af-w-4 af-animate-spin af-text-muted-foreground"})}):n?(0,re.jsx)("div",{className:"af-text-sm af-text-destructive af-bg-destructive/10 af-px-3 af-py-2 af-rounded",children:n}):(0,re.jsxs)("div",{className:"af-space-y-6",children:[(0,re.jsxs)("div",{children:[(0,re.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Contact"}),(0,re.jsxs)("div",{className:"af-space-y-1 af-text-sm",children:[(0,re.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,re.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Email"}),(0,re.jsx)("span",{children:e.email})]}),e.name&&(0,re.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,re.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Name"}),(0,re.jsx)("span",{children:e.name})]}),e.phone&&(0,re.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,re.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Phone"}),(0,re.jsx)("span",{children:e.phone})]}),(0,re.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,re.jsx)("span",{className:"af-text-muted-foreground af-w-32",children:"Orders"}),(0,re.jsx)("span",{children:x})]})]})]}),S.length>0&&(0,re.jsxs)("div",{children:[(0,re.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Addresses"}),(0,re.jsx)("div",{className:"af-flex af-flex-wrap af-gap-3",children:S.map(d=>(0,re.jsxs)("div",{className:"af-text-sm af-bg-muted af-rounded af-p-3 af-space-y-0.5",children:[d.is_default&&(0,re.jsx)(W,{variant:"secondary",className:"af-mb-1",children:"Default"}),hi(d).map((u,h)=>(0,re.jsx)("div",{children:u},h))]},d.id))})]}),(0,re.jsxs)("div",{children:[(0,re.jsx)("h4",{className:"af-text-sm af-font-medium af-mb-3",children:"Orders"}),p.length===0?(0,re.jsx)("p",{className:"af-text-sm af-text-muted-foreground af-italic",children:"No orders."}):(0,re.jsxs)(_e,{children:[(0,re.jsx)(ke,{children:(0,re.jsxs)(oe,{children:[(0,re.jsx)(I,{children:"Date"}),(0,re.jsx)(I,{children:"Amount"}),(0,re.jsx)(I,{children:"Status"})]})}),(0,re.jsx)(Ee,{children:p.map(d=>(0,re.jsxs)(oe,{children:[(0,re.jsx)(D,{className:"af-text-muted-foreground",children:ln(d.created_at)}),(0,re.jsx)(D,{children:z(Ft(d),d.currency)}),(0,re.jsx)(D,{children:(0,re.jsx)(Ca,{status:d.status})})]},d.id))})]})]})]})]})}var Z=require("react/jsx-runtime");function xi(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function hs({className:e}){let{customers:a,total:s,isLoading:t,error:r,page:c,pageSize:p,hasNextPage:g,hasPreviousPage:v,setPage:y,setPageSize:n,email:b,setEmail:S,refetch:x}=bs(),[d,u]=Us.useState("list"),[h,R]=Us.useState(null),[C,H]=Us.useState(""),G=q=>{R(q),u("detail")},f=()=>{u("list"),R(null),x()};if(d==="detail"&&h)return(0,Z.jsx)("div",{className:e,children:(0,Z.jsx)(ys,{customer:h,onBack:f})});let L=(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-mb-4",children:[(0,Z.jsx)(me,{type:"email",value:C,onChange:q=>H(q.target.value),placeholder:"Search by email",className:"af-w-[260px]",onKeyDown:q=>{q.key==="Enter"&&(q.preventDefault(),S(C.trim()))}}),(0,Z.jsxs)(w,{type:"button",variant:"outline",size:"sm",onClick:()=>S(C.trim()),children:[(0,Z.jsx)(oa.Search,{className:"af-h-4 af-w-4 af-mr-1"}),"Search"]}),b&&(0,Z.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>{H(""),S("")},children:"Clear"})]});if(t)return(0,Z.jsxs)("div",{className:e,children:[L,(0,Z.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,Z.jsx)("div",{className:"af-text-muted-foreground",children:"Loading customers..."})})]});if(r)return(0,Z.jsxs)("div",{className:e,children:[L,(0,Z.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:(0,Z.jsxs)("div",{className:"af-text-destructive",children:["Error: ",r.message]})})]});if(a.length===0&&c===0)return(0,Z.jsxs)("div",{className:e,children:[b&&L,(0,Z.jsxs)("div",{className:"af-flex af-flex-col af-items-center af-justify-center af-py-12",children:[(0,Z.jsx)(oa.Users,{className:"af-h-12 af-w-12 af-text-muted-foreground af-mb-4"}),(0,Z.jsx)("p",{className:"af-text-muted-foreground af-text-center",children:b?"No customers match that email.":(0,Z.jsxs)(Z.Fragment,{children:["No customers yet.",(0,Z.jsx)("br",{}),"Customers appear here after their first purchase."]})})]})]});let $=c*p+1,Q=Math.min((c+1)*p,s);return(0,Z.jsxs)("div",{className:e,children:[L,(0,Z.jsxs)(_e,{children:[(0,Z.jsx)(ke,{children:(0,Z.jsxs)(oe,{children:[(0,Z.jsx)(I,{children:"Email"}),(0,Z.jsx)(I,{children:"Name"}),(0,Z.jsx)(I,{children:"Customer since"}),(0,Z.jsx)(I,{className:"af-text-right",children:"Actions"})]})}),(0,Z.jsx)(Ee,{children:a.map(q=>(0,Z.jsxs)(oe,{children:[(0,Z.jsx)(D,{className:"af-font-medium",children:q.email}),(0,Z.jsx)(D,{children:q.name||(0,Z.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"-"})}),(0,Z.jsx)(D,{className:"af-text-muted-foreground",children:xi(q.created_at)}),(0,Z.jsx)(D,{className:"af-text-right",children:(0,Z.jsx)(w,{variant:"ghost",size:"icon",onClick:()=>G(q),title:"View customer",children:(0,Z.jsx)(oa.Eye,{className:"af-h-4 af-w-4"})})})]},q.id))})]}),(0,Z.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-px-2 af-py-4",children:[(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2 af-text-sm af-text-muted-foreground",children:[(0,Z.jsxs)("span",{children:["Showing ",$,"-",Q," of ",s]}),(0,Z.jsxs)(Lt,{value:String(p),onValueChange:q=>n(Number(q)),children:[(0,Z.jsx)(Nt,{className:"af-w-[70px] af-h-8",children:(0,Z.jsx)(Ot,{})}),(0,Z.jsxs)(wt,{children:[(0,Z.jsx)(Fe,{value:"10",children:"10"}),(0,Z.jsx)(Fe,{value:"25",children:"25"}),(0,Z.jsx)(Fe,{value:"50",children:"50"}),(0,Z.jsx)(Fe,{value:"100",children:"100"})]})]}),(0,Z.jsx)("span",{children:"per page"})]}),(0,Z.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,Z.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>y(c-1),disabled:!v,children:[(0,Z.jsx)(oa.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,Z.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>y(c+1),disabled:!g,children:["Next",(0,Z.jsx)(oa.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}var he=se(require("react"),1),Xe=require("lucide-react");var Je=se(require("react"),1);function Fa({initialPageSize:e=25,refreshKey:a,includeAggregates:s=!1}={}){let{api:t}=te(),[r,c]=Je.useState([]),[p,g]=Je.useState(0),[v,y]=Je.useState(null),[n,b]=Je.useState(!0),[S,x]=Je.useState(null),[d,u]=Je.useState(0),[h,R]=Je.useState(e),[C,H]=Je.useState(void 0),[G,f]=Je.useState(""),[L,$]=Je.useState(""),[Q,q]=Je.useState(""),[k,ne]=Je.useState(""),[m,Y]=Je.useState(""),Pe=C?C.join(","):"",xe=Je.useRef(0),fe=Je.useCallback(async()=>{let ee=++xe.current;b(!0),x(null);try{let ge={labels:Pe?Pe.split(","):void 0,customerEmail:G||void 0,q:L||void 0,from:Q||void 0,to:k||void 0,retainerId:m||void 0,includeAggregates:s,limit:h,offset:d*h},ve=await t.getInvoices(ge);if(ee!==xe.current)return;if(d>0&&d*h>=ve.total){u(Math.max(0,Math.ceil(ve.total/h)-1));return}c(ve.invoices),g(ve.total),s&&y(ve.aggregates??null)}catch(ge){if(ee!==xe.current)return;x(ge instanceof Error?ge:new Error("Failed to fetch invoices"))}finally{ee===xe.current&&b(!1)}},[t,Pe,G,L,Q,k,m,d,h,s]);Je.useEffect(()=>{fe()},[fe,a]);let Re=ee=>ge=>{u(0),ee(ge)};return{invoices:r,total:p,aggregates:v,isLoading:n,error:S,page:d,pageSize:h,hasNextPage:(d+1)*h<p,hasPreviousPage:d>0,setPage:u,setPageSize:Re(R),labels:C,setLabels:Re(H),customerEmail:G,setCustomerEmail:Re(f),q:L,setQ:Re($),from:Q,setFrom:Re(q),to:k,setTo:Re(ne),retainerId:m,setRetainerId:Re(Y),clearFilters:()=>{u(0),H(void 0),f(""),$(""),q(""),ne(""),Y("")},refetch:fe}}var i=require("react/jsx-runtime"),Ni={creating:"Creating",failed:"Failed",awaiting_payment:"Awaiting payment",overdue:"Overdue",payment_processing:"Payment processing",awaiting_authentication:"Awaiting authentication",paid:"Paid",paid_outside_stripe:"Paid outside Stripe",paid_by_credit:"Paid by credit",partially_refunded:"Partially refunded",refunded:"Refunded",disputed:"Disputed",voided:"Voided",uncollectible:"Marked uncollectible"};function cr(e){return e.label==="overdue"?`${e.days_overdue??0} days overdue`:Ni[e.label]??e.label}function wi(e){switch(e){case"paid":case"paid_outside_stripe":case"paid_by_credit":return"success";case"overdue":case"disputed":case"failed":return"destructive";case"partially_refunded":case"refunded":case"payment_processing":case"awaiting_authentication":return"warning";case"awaiting_payment":return"secondary";default:return"outline"}}var lr={queued:"Sending",sending:"Sending",sent:"Sent",delivered:"Delivered",bounced:"Bounced \u2014 check the address",complained:"Marked as spam",failed:"Not sent",unknown:"Delivery unknown"},Si={invoice_sent:"Invoice",reminder:"Reminder",void:"Cancellation notice",receipt:"Receipt"};function Wa({invoice:e,className:a}){return(0,i.jsxs)("span",{className:"af-inline-flex af-flex-wrap af-gap-1",children:[(0,i.jsx)(W,{variant:wi(e.label),className:a,children:cr(e)}),e.collection_method==="charge_automatically"&&(0,i.jsx)(W,{variant:"secondary",children:"Managed in Stripe"})]})}function Vs({delivery:e,className:a}){if(!e)return null;let s=e.state==="bounced"||e.state==="complained"||e.state==="failed"?"destructive":e.state==="unknown"?"warning":"outline";return(0,i.jsx)(W,{variant:s,className:a,children:lr[e.state]??e.state})}function Ga(e){return e.invoice_number??`#${e.id.slice(0,8)}`}function Ht(e){if(!e)return"\u2014";let a=e.includes("T")?e:`${e.replace(" ","T")}Z`,s=new Date(a);return Number.isNaN(s.getTime())?e:s.toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})}var cn={open:"Open",overdue:"Overdue",paid:"Paid",all:"All"},nr={open:["awaiting_payment","overdue","payment_processing","awaiting_authentication"],overdue:["overdue"],paid:["paid","paid_outside_stripe","paid_by_credit","partially_refunded","refunded"],all:void 0};function Pi(e){let a=e==null?"":String(e),s=/^[\s\u0000-\u001f]*[=+\-@=+-@]|^[\t\r\n]/u.test(a),t=s?`'${a}`:a;return s||/[",;\r\n]/.test(t)?`"${t.replace(/"/g,'""')}"`:t}var ir=100,or=5e3;async function Ri(e,a){let s=[],t=0;for(let r=0;r<or;r+=ir){let c=await e.getInvoices({...a,limit:ir,offset:r});if(t=c.total,s.push(...c.invoices),c.invoices.length<ir||s.length>=t)break}return{invoices:s.slice(0,or),total:t,truncated:t>s.length||t>or}}function dn(e){let a=["id","invoice","status","customer","email","created","due","paid","currency","subtotal","tax","total","amount_due","amount_paid","refunded","payment_method","pay_link","retainer_id","replaces_order_id","replaced_by_order_id","tax_behavior","discount_total","collection_method"],s=e.map(t=>[t.id,Ga(t),cr(t),t.customer_name??"",t.customer_email??"",t.created_at,t.due_at??"",t.paid_at??"",t.currency.toUpperCase(),z(t.amount_subtotal,t.currency),z(t.tax_total,t.currency),z(t.amount_total,t.currency),t.amount_due===null?"":z(t.amount_due,t.currency),t.amount_paid===null?"":z(t.amount_paid,t.currency),z(t.amount_refunded,t.currency),t.payment_method??"",t.pay_url??"",t.retainer_id??"",t.replaces_order_id??"",t.replaced_by_order_id??"",t.tax_behavior??"",t.discount_total===void 0?"":z(t.discount_total,t.currency),t.collection_method??""]);return[a,...s].map(t=>t.map(Pi).join(",")).join(`
|
|
5
|
+
`)}function Ci(e){if(typeof document>"u")return;let a=new Blob([dn(e)],{type:"text/csv;charset=utf-8"}),s=URL.createObjectURL(a),t=document.createElement("a");t.href=s,t.download=`invoices-${new Date().toISOString().slice(0,10)}.csv`,document.body.appendChild(t),t.click(),document.body.removeChild(t),URL.revokeObjectURL(s)}function Ua({className:e,refreshKey:a,initialInvoiceId:s,onDetailOpenChange:t,onConnectStripe:r,onUpgradePlan:c,needsPaidPlan:p=!1,retainerFilter:g=null,onClearRetainerFilter:v}){let{api:y}=te(),{invoices:n,total:b,isLoading:S,error:x,page:d,pageSize:u,hasNextPage:h,hasPreviousPage:R,setPage:C,labels:H,setLabels:G,customerEmail:f,setCustomerEmail:L,q:$,setQ:Q,from:q,setFrom:k,to:ne,setTo:m,retainerId:Y,setRetainerId:Pe,refetch:xe}=Fa({refreshKey:a}),[fe,Re]=he.useState("open"),[ee,ge]=he.useState(""),[ve,j]=he.useState(null),Ne=he.useRef(0),ct=he.useCallback(_=>{Ne.current++,Ae(!1),j(_)},[]),[It,Ae]=he.useState(!1),[ut,mt]=he.useState(0),[Ce,Dt]=he.useState(null),[T,U]=he.useState(!1),[be,st]=he.useState(0);he.useEffect(()=>{j(_=>{if(!_)return _;let Oe=n.find(F=>F.id===_.id);return Oe&&Oe!==_?Oe:_})},[n]);let[je,Ye]=he.useState(!1),[Ke,pt]=he.useState(null),xa=async()=>{if(!je){Ye(!0),pt(null);try{let{invoices:_,total:Oe,truncated:F}=await Ri(y,{labels:H,customerEmail:f,q:$,from:q,to:ne,retainerId:Y});Ci(_),pt(F?`Exported the first ${_.length.toLocaleString()} of ${Oe.toLocaleString()} matching invoices \u2014 narrow the filters for the rest.`:null)}catch{pt("Couldn't export \u2014 try again.")}finally{Ye(!1)}}},Yt=!f&&!$&&!q&&!ne&&!Y,[J,Te]=he.useState(null);he.useEffect(()=>{if(S||b>0||!Yt||fe!=="open")return;let _=!1;return y.getInvoices({limit:1}).then(Oe=>{_||Te(Oe.total>0)}).catch(()=>{_||Te(!0)}),()=>{_=!0}},[y,S,b,Yt,fe,a]),he.useEffect(()=>{G(nr.open)},[]),he.useEffect(()=>{let _=g?.id??"";_!==Y&&(Pe(_),_&&(Re("all"),G(nr.all),L(""),Q(""),ge(""),k(""),m("")))},[g?.id]),he.useEffect(()=>{let _=!1;return U(!1),y.getInvoiceSettings().then(Oe=>{_||Dt({payments_active:Oe.payments_active,blocked:Oe.blocked})}).catch(()=>{_||U(!0)}),()=>{_=!0}},[y,a,be]),he.useEffect(()=>{if(!s)return;let _=!1,Oe=Ne.current;return Ae(!1),y.getInvoice(s).then(F=>{!_&&Oe===Ne.current&&j(F)}).catch(()=>{!_&&Oe===Ne.current&&Ae(!0)}),()=>{_=!0}},[y,s,ut]),he.useEffect(()=>{t?.(ve!==null)},[ve,t]);let Ue=_=>{Re(_),G(nr[_])},zt=_=>{let Oe=_.trim();if(!Oe){$&&Q(""),f&&L("");return}if(Oe.includes("@")){$&&Q(""),L(Oe);return}f&&L(""),Q(Oe.replace(/^#/,""))},gt=n,ua=It&&(0,i.jsxs)("div",{role:"alert",className:"af-mb-3 af-rounded-md af-border af-p-3 af-text-sm",children:["Couldn't open the requested invoice. ",(0,i.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:()=>mt(_=>_+1),children:"Retry opening invoice"})]});if(ve)return(0,i.jsxs)("div",{className:e,children:[ua,(0,i.jsx)(Hs,{invoice:ve,refreshKey:a,onBack:()=>{ct(null),xe()}},ve.id)]});let Qe=(0,i.jsxs)("div",{className:"af-mb-4 af-space-y-3",children:[T&&(0,i.jsxs)("div",{role:"alert",className:"af-rounded-md af-border af-p-3 af-text-sm",children:["Couldn't check invoice setup. ",(0,i.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:()=>st(_=>_+1),children:"Retry setup check"})]}),ua,Ce?.blocked&&(0,i.jsxs)("div",{role:"alert",className:"af-rounded-md af-border af-border-destructive/40 af-bg-destructive/5 af-px-3 af-py-2 af-text-sm",children:["Invoicing is paused",Ce.blocked.reason?`: ${Ce.blocked.reason}`:"",". Reply to the email we sent you, or contact support."]}),g&&(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2 af-text-sm",children:[(0,i.jsxs)(W,{variant:"secondary",className:"af-max-w-full af-whitespace-normal af-break-words",children:["Invoices for ",g.label]}),v&&(0,i.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:v,children:"Clear schedule filter"})]}),(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,i.jsx)("div",{role:"group","aria-label":"Invoice views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:Object.keys(cn).map(_=>(0,i.jsx)("button",{type:"button","aria-pressed":fe===_,onClick:()=>Ue(_),className:M("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",fe===_?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:cn[_]},_))}),(0,i.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,i.jsx)(Xe.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,i.jsx)(me,{type:"text",value:ee,onChange:_=>ge(_.target.value),onBlur:()=>zt(ee),onKeyDown:_=>{_.key==="Enter"&&(_.preventDefault(),zt(ee))},placeholder:"Search email, name, or invoice #","aria-label":"Search invoices by email, name, or number",className:"af-pl-8"})]}),(0,i.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,i.jsx)(pe,{htmlFor:"invoices-from",className:"af-text-xs af-text-muted-foreground",children:"From"}),(0,i.jsx)(me,{id:"invoices-from",type:"date",value:q,max:ne||void 0,onChange:_=>k(_.target.value),className:"af-invoice-date af-w-[150px]"})]}),(0,i.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,i.jsx)(pe,{htmlFor:"invoices-to",className:"af-text-xs af-text-muted-foreground",children:"To"}),(0,i.jsx)(me,{id:"invoices-to",type:"date",value:ne,min:q||void 0,onChange:_=>m(_.target.value),className:"af-invoice-date af-w-[150px]"})]}),(0,i.jsxs)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>void xa(),disabled:je||S||!!x||b===0,"aria-busy":je,children:[(0,i.jsx)(Xe.Download,{className:"af-h-4 af-w-4 af-mr-1"}),je?"Exporting\u2026":"Export CSV (all matching)"]})]}),Ke&&(0,i.jsx)("div",{role:"status",className:"af-text-xs af-text-muted-foreground",children:Ke})]});return S?(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsx)("div",{className:"af-space-y-3","aria-hidden":"true",children:[0,1,2,3].map(_=>(0,i.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,i.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,i.jsx)(K,{className:"af-h-4 af-w-20"}),(0,i.jsx)(K,{className:"af-h-4 af-w-16"})]}),(0,i.jsx)(K,{className:"af-mt-2 af-h-3 af-w-40"})]},_))})]}):x?(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsxs)("div",{className:"af-rounded-md af-border af-px-3 af-py-2 af-text-sm",children:["Couldn't load your invoices."," ",(0,i.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:()=>void xe(),children:"Retry"})]})]}):b===0&&Yt&&J===!1&&fe==="open"?Ce?.blocked?(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsx)($s,{heading:"Invoicing is paused",body:"Contact support for help with invoicing."})]}):p?(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsx)($s,{heading:"Invoicing is on the Pro plan.",body:"Upgrade to send invoices from the chat.",action:c?{label:"Upgrade",onClick:c}:void 0})]}):Ce&&!Ce.payments_active?(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsx)($s,{heading:"Check your payment setup to send invoices.",body:"Stripe payments need to be active before you can send an invoice.",action:r?{label:"Payment setup",onClick:r}:void 0})]}):(0,i.jsxs)("div",{className:e,children:[Qe,(0,i.jsx)($s,{heading:"No invoices yet",body:!Ce||T?"Once invoice setup is ready, you can send invoices from the chat.":"Ask in the chat to send an invoice to a client for your services."})]}):(0,i.jsxs)("div",{className:e,children:[Qe,gt.length===0?(0,i.jsx)("div",{className:"af-rounded-md af-border af-px-3 af-py-6 af-text-center af-text-sm af-text-muted-foreground",children:"No invoices match."}):(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:gt.map(_=>(0,i.jsxs)("button",{type:"button",onClick:()=>ct(_),className:"af-block af-w-full af-rounded-md af-border af-p-3 af-text-left hover:af-bg-muted/50",children:[(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2",children:[(0,i.jsx)("span",{className:"af-font-medium",children:Ga(_)}),(0,i.jsx)(Wa,{invoice:_})]}),(0,i.jsx)("div",{className:"af-mt-1 af-break-words af-text-sm af-text-muted-foreground",children:_.customer_name??_.customer_email??"\u2014"}),_.delivery&&(0,i.jsx)("div",{className:"af-mt-1",children:(0,i.jsx)(Vs,{delivery:_.delivery})}),(0,i.jsxs)("div",{className:"af-mt-1 af-flex af-flex-wrap af-gap-2 af-items-center af-justify-between af-text-sm",children:[(0,i.jsx)("span",{children:_.collection_method==="charge_automatically"?"Automatic collection":_.due_at?`Due ${Ht(_.due_at)}`:"Due on receipt"}),(0,i.jsx)("span",{className:"af-font-medium af-tabular-nums",children:z(_.amount_due??_.amount_total,_.currency)})]})]},_.id))}),(0,i.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,i.jsxs)(_e,{children:[(0,i.jsx)(ke,{children:(0,i.jsxs)(oe,{children:[(0,i.jsx)(I,{children:"Invoice"}),(0,i.jsx)(I,{children:"Created"}),(0,i.jsx)(I,{children:"Customer"}),(0,i.jsx)(I,{children:"Due"}),(0,i.jsx)(I,{className:"af-text-right",children:"Amount"}),(0,i.jsx)(I,{children:"Status"}),(0,i.jsx)(I,{children:"Delivery"}),(0,i.jsx)(I,{className:"af-w-8"})]})}),(0,i.jsx)(Ee,{children:gt.map(_=>(0,i.jsxs)(oe,{className:"af-cursor-pointer",onClick:()=>ct(_),children:[(0,i.jsx)(D,{className:"af-font-medium",children:(0,i.jsx)("button",{type:"button",className:"af-text-left hover:af-underline focus-visible:af-ring-2 focus-visible:af-ring-ring",onClick:Oe=>{Oe.stopPropagation(),ct(_)},"aria-label":`Open invoice ${Ga(_)}`,children:Ga(_)})}),(0,i.jsx)(D,{children:Ht(_.created_at)}),(0,i.jsxs)(D,{children:[(0,i.jsx)("div",{children:_.customer_name??"\u2014"}),(0,i.jsx)("div",{className:"af-text-xs af-text-muted-foreground",children:_.customer_email})]}),(0,i.jsx)(D,{children:_.collection_method==="charge_automatically"?"Automatic":_.due_at?Ht(_.due_at):"On receipt"}),(0,i.jsx)(D,{className:"af-text-right af-tabular-nums",children:z(_.amount_due??_.amount_total,_.currency)}),(0,i.jsx)(D,{children:(0,i.jsx)(Wa,{invoice:_})}),(0,i.jsx)(D,{children:(0,i.jsx)(Vs,{delivery:_.delivery})}),(0,i.jsx)(D,{children:(0,i.jsx)(Xe.ChevronRight,{className:"af-h-4 af-w-4 af-text-muted-foreground"})})]},_.id))})]})}),(0,i.jsxs)("div",{className:"af-mt-4 af-flex af-items-center af-justify-between af-text-sm af-text-muted-foreground",children:[(0,i.jsxs)("span",{children:[b," invoice",b===1?"":"s"]}),(0,i.jsxs)("div",{className:"af-flex af-items-center af-gap-1",children:[(0,i.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>C(d-1),disabled:!R,"aria-label":"Previous page",children:(0,i.jsx)(Xe.ChevronLeft,{className:"af-h-4 af-w-4"})}),(0,i.jsxs)("span",{className:"af-tabular-nums",children:[d*u+1,"\u2013",Math.min((d+1)*u,b)]}),(0,i.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>C(d+1),disabled:!h,"aria-label":"Next page",children:(0,i.jsx)(Xe.ChevronRight,{className:"af-h-4 af-w-4"})})]})]})]})]})}function $s({heading:e,body:a,action:s}){return(0,i.jsxs)("div",{className:"af-rounded-md af-border af-px-6 af-py-10 af-text-center",children:[(0,i.jsx)(Xe.FileText,{className:"af-mx-auto af-h-8 af-w-8 af-text-muted-foreground","aria-hidden":"true"}),(0,i.jsx)("h3",{className:"af-mt-3 af-text-base af-font-semibold",children:e}),(0,i.jsx)("p",{className:"af-mt-1 af-text-sm af-text-muted-foreground",children:a}),s&&(0,i.jsx)(w,{type:"button",className:"af-mt-4",onClick:s.onClick,children:s.label})]})}var ya="af-text-xs af-text-muted-foreground";function fn(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function Hs({invoice:e,onBack:a,refreshKey:s,className:t}){let{api:r}=te(),c=he.useRef(null);he.useEffect(()=>{c.current?.focus()},[e.id]);let[p,g]=he.useState(!1),[v,y]=he.useState(null),[n,b]=he.useState(!1),[S,x]=he.useState(!1),d=he.useRef(0),[u,h]=he.useState(null),[R,C]=he.useState(!1),[H,G]=he.useState(0);he.useEffect(()=>{let m=!1;return C(!1),r.getInvoice(e.id).then(Y=>{m||h({source:e,value:Y})}).catch(()=>{m||C(!0)}),()=>{m=!0}},[r,e,s,H]);let f=u?.source===e?u.value:e,L=f.collection_method==="charge_automatically";he.useEffect(()=>(d.current++,y(null),b(!1),x(!1),g(!1),()=>{d.current++}),[r,f,s]);let $=m=>m==null?"\u2014":z(m,f.currency),Q=async()=>{if(!(!f.pay_url||!fn()))try{await navigator.clipboard.writeText(f.pay_url),g(!0),setTimeout(()=>g(!1),1500)}catch{}},q=async()=>{if(v){y(null);return}let m=++d.current;x(!1),b(!0);try{let Y=await r.getInvoicePreview(f.id);m===d.current&&y(Y)}catch{m===d.current&&x(!0)}finally{m===d.current&&b(!1)}},k=f.bill_to,ne=k?[k.company,k.name,k.address?.line1,k.address?.line2,[k.address?.city,k.address?.state,k.address?.postal_code].filter(Boolean).join(", "),k.address?.country,k.tax_id?`${k.tax_id.value}`:null,k.po_number?`PO ${k.po_number}`:null,k.service_period?`Service period: ${k.service_period}`:null].filter(m=>!!m&&m.length>0):[];return(0,i.jsxs)("div",{className:t,children:[(0,i.jsxs)("div",{className:"af-mb-4",children:[(0,i.jsxs)(w,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,i.jsx)(Xe.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to invoices"]}),(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,i.jsxs)("h3",{ref:c,tabIndex:-1,className:"af-text-lg af-font-semibold focus:af-outline-none",children:["Invoice ",Ga(f)]}),(0,i.jsx)(Wa,{invoice:f}),(0,i.jsx)(Vs,{delivery:f.delivery})]}),(0,i.jsxs)("p",{className:"af-mt-1 af-break-words af-text-sm af-text-muted-foreground",children:[f.customer_name??f.customer_email,f.customer_name&&f.customer_email?` \xB7 ${f.customer_email}`:""]})]}),R&&(0,i.jsxs)("div",{role:"alert",className:"af-mb-4 af-rounded-md af-border af-p-3 af-text-sm",children:["Couldn't refresh this invoice. These details may be out of date. ",(0,i.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:()=>G(m=>m+1),children:"Retry details"})]}),f.label==="failed"&&(0,i.jsx)("div",{role:"alert",className:"af-mb-4 af-rounded-md af-border af-p-3 af-text-sm",children:"Couldn't finish creating this invoice. Ask in the chat to retry."}),f.label==="creating"&&(0,i.jsx)("p",{role:"status",className:"af-mb-4 af-text-sm af-text-muted-foreground",children:"This invoice is still being created. Its final details will appear when it is ready."}),(0,i.jsxs)("div",{className:"af-grid af-grid-cols-1 af-gap-4 lg:af-grid-cols-3",children:[(0,i.jsxs)("div",{className:"af-min-w-0 lg:af-col-span-2 af-space-y-4",children:[(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4",children:[(0,i.jsxs)(_e,{children:[(0,i.jsx)(ke,{children:(0,i.jsxs)(oe,{children:[(0,i.jsx)(I,{children:"Description"}),(0,i.jsx)(I,{className:"af-text-right",children:"Qty"}),(0,i.jsx)(I,{className:"af-text-right",children:"Amount"})]})}),(0,i.jsx)(Ee,{children:f.line_items.map((m,Y)=>(0,i.jsxs)(oe,{children:[(0,i.jsx)(D,{style:{overflowWrap:"anywhere"},children:m.description}),(0,i.jsx)(D,{className:"af-whitespace-nowrap af-text-right af-tabular-nums",children:m.quantity}),(0,i.jsx)(D,{className:"af-whitespace-nowrap af-text-right af-tabular-nums",children:$(m.amount)})]},Y))})]}),(0,i.jsxs)("dl",{className:"af-mt-3 af-space-y-1 af-text-sm",children:[(0,i.jsx)(ka,{label:"Subtotal",value:$(f.amount_subtotal)}),(f.discount_total??0)>0&&(0,i.jsx)(ka,{label:"Discount",value:`-${$(f.discount_total)}`}),f.tax_total>0&&(0,i.jsx)(ka,{label:f.tax_behavior==="inclusive"?"Tax (included)":"Tax",value:$(f.tax_total)}),(0,i.jsx)(ka,{label:"Total",value:$(f.amount_total),strong:!0}),f.credit_applied>0&&(0,i.jsx)(ka,{label:"Credit applied",value:`-${$(f.credit_applied)}`}),f.amount_due!==null&&f.amount_due!==f.amount_total&&(0,i.jsx)(ka,{label:"Amount due",value:$(f.amount_due),strong:!0}),f.amount_paid!==null&&f.amount_paid>0&&(0,i.jsx)(ka,{label:"Amount paid",value:$(f.amount_paid)}),f.amount_refunded>0&&(0,i.jsx)(ka,{label:"Refunded",value:$(f.amount_refunded)})]}),f.credit_notes?.length>0&&(0,i.jsxs)("div",{className:"af-mt-3 af-space-y-1 af-text-sm",children:[(0,i.jsx)("div",{className:ya,children:"Credit notes"}),f.credit_notes.map(m=>(0,i.jsxs)("div",{className:"af-flex af-items-baseline af-justify-between af-gap-3",children:[(0,i.jsxs)("span",{className:"af-text-muted-foreground",children:[Ht(m.created_at)," \xB7 ",m.origin==="dashboard"?"issued in Stripe":"refund",m.stripe_credit_note_id?"":" \xB7 being created"]}),(0,i.jsxs)("span",{className:"af-tabular-nums",children:["-",z(m.amount,m.currency)]})]},m.id))]})]}),(f.note||f.footer)&&(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-space-y-2 af-text-sm",children:[f.note&&(0,i.jsxs)("div",{children:[(0,i.jsx)("div",{className:ya,children:"Note"}),(0,i.jsx)("div",{className:"af-whitespace-pre-wrap af-break-words",children:f.note})]}),f.footer&&(0,i.jsxs)("div",{children:[(0,i.jsx)("div",{className:ya,children:"Footer"}),(0,i.jsx)("div",{className:"af-whitespace-pre-wrap af-break-words af-text-muted-foreground",children:f.footer})]})]}),v&&!L&&(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4",children:[(0,i.jsxs)("div",{className:M(ya,"af-break-words"),children:["Email preview \u2014 ",v.subject]}),(0,i.jsx)("p",{className:"af-mt-1 af-text-xs af-text-muted-foreground",children:"Uses current invoice details. Delivery history is shown separately."}),(0,i.jsx)("iframe",{title:"Invoice email preview",srcDoc:v.html,sandbox:"",className:"af-mt-2 af-h-[560px] af-w-full af-rounded af-border af-bg-white"})]})]}),(0,i.jsxs)("div",{className:"af-min-w-0 af-space-y-4",children:[(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-space-y-2 af-text-sm",children:[(0,i.jsx)(Vt,{label:"Created",value:Ht(f.created_at)}),(0,i.jsx)(Vt,{label:"Due",value:f.collection_method==="charge_automatically"?"Managed in Stripe":f.due_at?Ht(f.due_at):"On receipt"}),f.paid_at&&(0,i.jsx)(Vt,{label:"Paid",value:Ht(f.paid_at)}),f.payment_method&&(0,i.jsx)(Vt,{label:"Paid by",value:f.paid_out_of_band?`${f.payment_method} (recorded outside Stripe)`:f.payment_method}),f.payment_state!=="none"&&(0,i.jsx)(Vt,{label:"Payment",value:f.payment_state==="processing"?"Processing":f.payment_state==="requires_action"?"Awaiting authentication":"Last attempt failed"}),f.delivery&&(0,i.jsx)(Vt,{label:"Delivery",value:`${lr[f.delivery.state]} \xB7 ${f.delivery.recipient}`}),f.marked_uncollectible_at&&(0,i.jsx)(Vt,{label:"Written off",value:Ht(f.marked_uncollectible_at)}),f.correction_reason&&(0,i.jsx)(Vt,{label:"Correction",value:f.correction_reason}),f.replaced_by_order_id&&(0,i.jsx)(Vt,{label:"Replaced by",value:f.replaced_by_order_id}),f.collection_method==="charge_automatically"?(0,i.jsx)(Vt,{label:"Managed in Stripe",value:"Stripe manages automatic collection for this invoice. View or change its payment settings in Stripe."}):f.retainer_id&&(0,i.jsx)(Vt,{label:"Recurring invoice",value:"Sent on a recurring schedule \u2014 change the schedule, skip the next invoice or pause future invoices from the chat"}),f.replaces_order_id&&(0,i.jsx)(Vt,{label:"Replaces",value:f.replaces_order_id})]}),(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-space-y-2 af-text-sm",children:[(0,i.jsx)("div",{className:ya,children:"Reminders"}),L?(0,i.jsx)("div",{children:"View automatic payment reminders in Stripe."}):f.reminders.disabled?(0,i.jsx)("div",{children:"Off for this invoice."}):f.reminders.paused_until?(0,i.jsxs)("div",{children:["Paused until ",Ht(f.reminders.paused_until),"."]}):f.reminders.next_at?(0,i.jsxs)("div",{children:["Next on ",Ht(f.reminders.next_at),"."]}):f.reminders.final_sent_at?(0,i.jsxs)("div",{children:["Final reminder sent ",Ht(f.reminders.final_sent_at),"."]}):(0,i.jsx)("div",{children:"None scheduled."}),!L&&(0,i.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["Schedule: ",f.reminders.schedule.length===0?"none":f.reminders.schedule.map(m=>m<0?`${-m}d before`:m===0?"due day":`${m}d after`).join(", ")]})]}),f.communications&&f.communications.length>0&&(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-space-y-2 af-text-sm",children:[(0,i.jsx)("div",{className:ya,children:"Email delivery history"}),f.communications.map(m=>(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-items-baseline af-justify-between af-gap-x-3 af-gap-y-1",children:[(0,i.jsxs)("span",{className:"af-min-w-0 af-break-words",children:[Si[m.kind]??m.kind,m.kind==="reminder"&&m.slot!==null?` #${m.slot}`:"",(0,i.jsxs)("span",{className:"af-text-muted-foreground",children:[" \xB7 ",m.recipient]})]}),(0,i.jsxs)("span",{className:"af-shrink-0 af-text-xs af-text-muted-foreground",children:[lr[m.state]??m.state," \xB7 ",Ht(m.state_at??m.created_at)]})]},m.id))]}),ne.length>0&&(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-text-sm",children:[(0,i.jsx)("div",{className:ya,children:"Bill to"}),ne.map((m,Y)=>(0,i.jsx)("div",{className:"af-break-words",children:m},Y))]}),(0,i.jsxs)("section",{className:"af-rounded-md af-border af-p-4 af-space-y-2",children:[f.pay_url&&(0,i.jsxs)(w,{type:"button",variant:"outline",size:"sm",className:"af-w-full",onClick:()=>void Q(),disabled:!fn(),children:[(0,i.jsx)(Xe.Copy,{className:"af-h-4 af-w-4 af-mr-1"}),p?"Copied":L?"Copy invoice link":"Copy pay link"]}),f.pdf_url&&(0,i.jsx)(w,{type:"button",variant:"outline",size:"sm",className:"af-w-full",asChild:!0,children:(0,i.jsxs)("a",{href:f.pdf_url,target:"_blank",rel:"noreferrer",children:[(0,i.jsx)(Xe.ExternalLink,{className:"af-h-4 af-w-4 af-mr-1"}),"Open PDF"]})}),!L&&(0,i.jsxs)(w,{type:"button",variant:"outline",size:"sm",className:"af-w-full",onClick:()=>void q(),disabled:n,children:[n?(0,i.jsx)(Xe.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,i.jsx)(Xe.FileText,{className:"af-h-4 af-w-4 af-mr-1"}),v?"Hide email preview":S?"Retry email preview":"Show email preview"]}),S&&!L&&(0,i.jsx)("p",{role:"alert",className:"af-text-xs af-text-destructive",children:"Couldn't load the email preview. Try again."}),f.pay_url&&(0,i.jsx)("div",{className:"af-break-all af-text-xs af-text-muted-foreground",children:f.pay_url}),(0,i.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:L?"Manage automatic charging and payment reminders for this invoice in Stripe.":"To resend, remind, void, or mark this invoice paid, ask in the chat."})]})]})]})]})}function Vt({label:e,value:a}){return(0,i.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-3",children:[(0,i.jsx)("span",{className:M(ya,"af-shrink-0"),children:e}),(0,i.jsx)("span",{className:"af-min-w-0 af-break-words af-text-right",children:a})]})}function ka({label:e,value:a,strong:s}){return(0,i.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-x-3 af-items-center af-justify-between",children:[(0,i.jsx)("dt",{className:M(ya,s&&"af-font-medium af-text-foreground"),children:e}),(0,i.jsx)("dd",{className:M("af-tabular-nums",s&&"af-font-semibold"),children:a})]})}var mn=require("lucide-react");var Se=require("react/jsx-runtime"),Ti={creating:"Setting up",active:"Active",paused:"Paused",canceling:"Ends this period",canceled:"Ended",failed:"Failed"},un={merchant:"paused by you",plan:"paused \u2014 invoicing unavailable",suspension:"paused \u2014 payments disabled"};function _i(e){switch(e){case"active":return"success";case"paused":case"canceling":return"warning";case"failed":return"destructive";case"canceled":return"outline";default:return"secondary"}}function pn(e){return e.status==="paused"&&e.paused_by.length>0?e.paused_by.map(a=>un[a]).join("; "):e.status==="paused"?"Paused in Stripe":e.status==="canceling"&&e.paused_by.length>0?`Ends this period; ${e.paused_by.map(a=>un[a]).join("; ")}`:e.status==="active"&&e.skip_next_cycle?"Active \u2014 next invoice skipped":Ti[e.status]}function gn(e){let a=e.interval==="year"?"every year":"every month";return`${z(e.amount,e.currency)} ${a}`}function ki(e){return e.status==="canceled"?`Ended ${Ms(e.canceled_at)}`:e.status==="canceling"?e.next_cycle_at?`Ends ${Ms(e.next_cycle_at)}`:"Ends this period":e.collection_method==="charge_automatically"?"Billing schedule managed in Stripe":e.status==="paused"?"Future invoices paused":e.status==="failed"||e.status==="creating"?"Next invoice not scheduled yet":e.skip_next_cycle?e.next_cycle_at?`Skipping ${Ms(e.next_cycle_at)}`:"Next invoice will be skipped":e.next_cycle_at?`Next invoice ${Ms(e.next_cycle_at)}`:"Next invoice not scheduled yet"}function Ms(e){if(!e)return"\u2014";let a=e.includes("T")?e:e.replace(" ","T")+(e.length===19?"Z":""),s=new Date(a);return Number.isNaN(s.getTime())?e.slice(0,10):s.toLocaleDateString(void 0,{year:"numeric",month:"short",day:"numeric"})}function zs({retainer:e,className:a}){return(0,Se.jsx)(W,{variant:_i(e.status),className:M("af-max-w-full af-whitespace-normal af-break-words",a),children:pn(e)})}function $a({retainers:e,isLoading:a,error:s,onRetry:t,onShowCycles:r,activeRetainerId:c,total:p,endedTotal:g=0,isLoadingMore:v=!1,onLoadMore:y,includeEnded:n=!1,onIncludeEndedChange:b,className:S}){let x=!!b;if(!a&&!s&&e.length===0&&!(x&&(n||g>0)))return null;let d=p!==void 0?Math.max(0,p-e.length):0;return(0,Se.jsxs)("section",{className:M("af-mb-6",S),"aria-label":"Recurring invoices",children:[(0,Se.jsxs)("div",{className:"af-mb-2 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,Se.jsx)(mn.Repeat,{className:"af-h-4 af-w-4 af-text-muted-foreground","aria-hidden":"true"}),(0,Se.jsx)("h3",{className:"af-text-sm af-font-semibold",children:"Recurring invoices"}),(0,Se.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:"Send invoices on a schedule \u2014 change, skip, pause or end them from the chat."}),x&&(0,Se.jsx)(w,{type:"button",variant:"ghost",size:"sm",className:"af-ml-auto","aria-pressed":n,onClick:()=>b?.(!n),children:n?"Hide ended recurring invoices":g>0&&e.length===0?`Show ended recurring invoices (${g})`:"Show ended recurring invoices"})]}),s?(0,Se.jsxs)("div",{className:"af-rounded-md af-border af-px-3 af-py-2 af-text-sm",children:["Couldn't load your recurring invoices."," ",(0,Se.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:t,children:"Retry"})]}):a?(0,Se.jsx)("div",{className:"af-space-y-2","aria-hidden":"true",children:[0,1].map(u=>(0,Se.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,Se.jsx)(K,{className:"af-h-4 af-w-48"}),(0,Se.jsx)(K,{className:"af-mt-2 af-h-3 af-w-32"})]},u))}):e.length===0?(0,Se.jsx)("div",{className:"af-rounded-md af-border af-px-3 af-py-2 af-text-sm af-text-muted-foreground",children:n?"No recurring invoices yet.":"No current recurring invoices. Select Show ended recurring invoices to view past schedules."}):(0,Se.jsx)("ul",{className:"af-grid af-grid-cols-1 af-gap-2 md:af-grid-cols-2",children:e.map(u=>{let h=c===u.id;return(0,Se.jsxs)("li",{className:M("af-rounded-md af-border af-p-3",h&&"af-border-primary af-bg-muted/40"),children:[(0,Se.jsxs)("div",{className:"af-flex af-flex-wrap af-items-start af-justify-between af-gap-2",children:[(0,Se.jsxs)("div",{className:"af-min-w-0",children:[(0,Se.jsx)("div",{className:"af-truncate af-font-medium",children:u.customer_name??u.customer_email??"\u2014"}),(0,Se.jsx)("div",{className:"af-truncate af-text-sm af-text-muted-foreground",children:u.description})]}),(0,Se.jsx)(zs,{retainer:u})]}),u.collection_method==="charge_automatically"?(0,Se.jsxs)("div",{className:"af-mt-2 af-space-y-1",children:[(0,Se.jsx)(W,{variant:"secondary",children:"Managed in Stripe"}),(0,Se.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Automatic charging is enabled in Stripe. View or change the billing terms there. To edit this schedule from the chat, switch it back to sending invoices in Stripe."})]}):(0,Se.jsx)("div",{className:"af-mt-2 af-text-sm",children:gn(u)}),(0,Se.jsxs)("div",{className:"af-mt-1 af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-text-xs af-text-muted-foreground",children:[(0,Se.jsxs)("span",{children:[ki(u)," \xB7 ",u.cycles.count===0?"no invoices yet":`${u.cycles.paid} paid${u.cycles.open>0?`, ${u.cycles.open} open`:""}`]}),u.cycles.count>0&&(0,Se.jsx)(w,{type:"button",variant:h?"secondary":"ghost",size:"sm",onClick:()=>r(u),"aria-pressed":h,children:h?"Showing invoices":"View invoices"})]}),u.status==="failed"&&u.operation?.last_error&&(0,Se.jsxs)("div",{className:"af-mt-2 af-text-xs af-text-destructive",children:["Couldn't be set up: ",u.operation.last_error,". Ask in the chat to try again."]})]},u.id)})}),!a&&!s&&d>0&&y&&(0,Se.jsx)("div",{className:"af-mt-2",children:(0,Se.jsx)(w,{type:"button",variant:"outline",size:"sm",onClick:y,disabled:v,children:v?"Loading\u2026":`Load more (${d} more)`})})]})}var vn=se(require("react"),1),kt=require("lucide-react");var la=se(require("react"),1);function Va(){let{api:e}=te(),[a,s]=la.useState(null),[t,r]=la.useState(!0),[c,p]=la.useState(null),g=la.useRef(0),v=la.useCallback(async()=>{let y=++g.current;try{r(!0),p(null);let n=await e.getPaymentsStatus();if(y!==g.current)return;s(n)}catch(n){if(y!==g.current)return;p(n instanceof Error?n:new Error("Failed to fetch payments status"))}finally{y===g.current&&r(!1)}},[e]);return la.useEffect(()=>{v()},[v]),{status:a,isLoading:t,error:c,refetch:v}}var ce=require("react/jsx-runtime");function xs({onConnectStripe:e,onUpgradePlan:a,className:s}){let{status:t,isLoading:r,error:c,refetch:p}=Va(),[g,v]=vn.useState(!1),y=async()=>{v(!0);try{await p()}finally{v(!1)}};return r&&!t?(0,ce.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${s||""}`,children:(0,ce.jsx)("div",{className:"af-text-muted-foreground",children:"Loading payment status..."})}):c||!t?(0,ce.jsx)("div",{className:`af-flex af-items-center af-justify-center af-py-8 ${s||""}`,children:(0,ce.jsxs)("div",{className:"af-text-destructive",children:["Error: ",c?.message||"Failed to load payment status"]})}):t.status==="active"?(0,ce.jsx)("div",{className:s,children:(0,ce.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-green-50 af-border af-border-green-200 af-rounded-lg af-p-4",children:[(0,ce.jsx)(kt.CheckCircle2,{className:"af-h-5 af-w-5 af-text-green-600 af-mt-0.5"}),(0,ce.jsxs)("div",{children:[(0,ce.jsx)("p",{className:"af-text-sm af-font-medium af-text-green-800",children:"Payments are active"}),(0,ce.jsxs)("p",{className:"af-text-sm af-text-green-800",children:["Your Stripe account is connected and your site can accept payments in ",t.currency.toUpperCase(),". Payouts go directly to your Stripe account."]})]})]})}):(0,ce.jsxs)("div",{className:`af-space-y-4 ${s||""}`,children:[t.reason==="needs_stripe"&&(0,ce.jsxs)("div",{className:"af-space-y-3",children:[(0,ce.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-muted af-rounded-lg af-p-4",children:[(0,ce.jsx)(kt.CreditCard,{className:"af-h-5 af-w-5 af-text-muted-foreground af-mt-0.5"}),(0,ce.jsxs)("div",{children:[(0,ce.jsx)("p",{className:"af-text-sm af-font-medium",children:"Set up payments to start selling"}),(0,ce.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"It only takes a few minutes. You'll enter your details with Stripe, who handle the payments, and money from sales goes straight to your own bank."})]})]}),e?(0,ce.jsxs)(w,{onClick:e,children:[(0,ce.jsx)(kt.CreditCard,{className:"af-h-4 af-w-4 af-mr-1"}),t.stripe_account_connected?"Finish setting up":"Set up payments"]}):(0,ce.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Set up payments from your site dashboard to start selling."})]}),t.reason==="needs_paid_plan"&&(0,ce.jsxs)("div",{className:"af-space-y-3",children:[(0,ce.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-muted af-rounded-lg af-p-4",children:[(0,ce.jsx)(kt.AlertTriangle,{className:"af-h-5 af-w-5 af-text-muted-foreground af-mt-0.5"}),(0,ce.jsxs)("div",{children:[(0,ce.jsx)("p",{className:"af-text-sm af-font-medium",children:"Selling requires a paid plan"}),(0,ce.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Your products are saved as drafts. Upgrade to a paid plan to start accepting payments on your site."})]})]}),a?(0,ce.jsx)(w,{onClick:a,children:"Upgrade plan"}):(0,ce.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Upgrade your plan from your site dashboard to start selling."})]}),t.reason==="account_disabled"&&(0,ce.jsxs)("div",{className:"af-space-y-3",children:[(0,ce.jsxs)("div",{className:"af-flex af-items-start af-gap-3 af-bg-yellow-50 af-border af-border-yellow-200 af-rounded-lg af-p-4",children:[(0,ce.jsx)(kt.AlertTriangle,{className:"af-h-5 af-w-5 af-text-yellow-600 af-mt-0.5"}),(0,ce.jsxs)("div",{children:[(0,ce.jsx)("p",{className:"af-text-sm af-font-medium af-text-yellow-800",children:"Payments are paused"}),t.stripe_account_connected?(0,ce.jsx)("p",{className:"af-text-sm af-text-yellow-800",children:"Your Stripe account can't currently accept charges. Check your Stripe account for outstanding requirements or notifications \u2014 once Stripe re-enables charges, payments resume here automatically."}):(0,ce.jsx)("p",{className:"af-text-sm af-text-yellow-800",children:"Payments are currently paused for this site. If you think this is a mistake, get in touch with support and we'll look into it with you."})]})]}),(0,ce.jsxs)("div",{className:"af-flex af-gap-2",children:[e&&t.stripe_account_connected&&(0,ce.jsxs)(w,{onClick:e,children:[(0,ce.jsx)(kt.CreditCard,{className:"af-h-4 af-w-4 af-mr-1"}),"Finish setting up"]}),(0,ce.jsxs)(w,{variant:"outline",onClick:y,disabled:g,children:[g?(0,ce.jsx)(kt.Loader2,{className:"af-h-4 af-w-4 af-mr-1 af-animate-spin"}):(0,ce.jsx)(kt.RefreshCw,{className:"af-h-4 af-w-4 af-mr-1"}),"Refresh status"]})]})]})]})}var ft=se(require("react"),1);function fr(e,a,s){return e.map(t=>t.id===a?{...t,...s}:t)}function Ha({includeStats:e=!1,refreshKey:a}={}){let{api:s}=te(),[t,r]=ft.useState([]),[c,p]=ft.useState(!0),[g,v]=ft.useState(null),y=ft.useRef(0),n=ft.useCallback(async()=>{let h=++y.current;try{p(!0),v(null);let R=await s.getProducts(e?{include:"stats"}:{});if(h!==y.current)return;r(R)}catch(R){if(h!==y.current)return;v(R instanceof Error?R:new Error("Failed to fetch products"))}finally{h===y.current&&p(!1)}},[s,e,a]);ft.useEffect(()=>{n()},[n]);let b=ft.useCallback(async h=>{let R=await s.createProduct(h);return r(C=>[R,...C]),R},[s]),S=ft.useCallback(async(h,R)=>{let C=await s.updateProduct(h,R);return r(H=>fr(H,h,C)),C},[s]),x=ft.useCallback(async h=>{await s.deleteProduct(h),r(R=>R.filter(C=>C.id!==h))},[s]),d=ft.useCallback(async(h,R)=>{let C=await s.attachProductFile(h,R);return r(H=>fr(H,h,C)),C},[s]),u=ft.useCallback(async(h,R)=>{let C=await s.attachProductFileFromUrl(h,R);return r(H=>fr(H,h,C)),C},[s]);return{products:t,isLoading:c,error:g,refetch:n,createProduct:b,updateProduct:S,deleteProduct:x,attachProductFile:d,attachProductFileFromUrl:u}}var dt=se(require("react"),1);var bn=["creating","active","paused","canceling"],Ei=["canceled","failed"];function Ma({refreshKey:e,includeEnded:a=!1,limit:s=50}={}){let{api:t}=te(),[r,c]=dt.useState([]),[p,g]=dt.useState(0),[v,y]=dt.useState(0),[n,b]=dt.useState(!0),[S,x]=dt.useState(!1),[d,u]=dt.useState(null),[h,R]=dt.useState(a),C=dt.useRef(0),H=dt.useCallback(async()=>{let f=++C.current;b(!0),x(!1),u(null);try{let L=await t.getRetainers({statuses:h?void 0:bn,limit:s,offset:0});if(f!==C.current)return;if(c(L.retainers),g(L.total),!h&&L.total===0){let $=await t.getRetainers({statuses:Ei,limit:1,offset:0});if(f!==C.current)return;y($.total)}else y(0)}catch(L){if(f!==C.current)return;u(L instanceof Error?L:new Error("Failed to fetch retainers"))}finally{f===C.current&&b(!1)}},[t,h,s]);dt.useEffect(()=>{H()},[H,e]);let G=dt.useCallback(async()=>{if(n||S||r.length>=p)return;let f=C.current;x(!0);try{let L=await t.getRetainers({statuses:h?void 0:bn,limit:s,offset:r.length});if(f!==C.current)return;c($=>{let Q=new Set($.map(q=>q.id));return[...$,...L.retainers.filter(q=>!Q.has(q.id))]}),g(L.total)}catch(L){if(f!==C.current)return;u(L instanceof Error?L:new Error("Failed to fetch retainers"))}finally{f===C.current&&x(!1)}},[t,h,s,n,S,r.length,p]);return{retainers:r,total:p,endedTotal:v,hasMore:r.length<p,isLoading:n,isLoadingMore:S,error:d,includeEnded:h,setIncludeEnded:R,loadMore:G,refetch:H}}var ue=require("react/jsx-runtime");function dr({className:e,defaultTab:a="products",onConnectStripe:s,onRefund:t,onUpgradePlan:r}){let[c,p]=Ka.useState(a),[g,v]=Ka.useState("list"),[y,n]=Ka.useState(null),b=Ha(),{status:S,error:x}=Va(),d=S?.currency,u=H=>{n(H),v("edit")},h=()=>{n(null),v("create")},R=()=>{v("list"),n(null)},C=H=>{n(H),v("edit")};return(0,ue.jsxs)("div",{className:e,children:[(0,ue.jsxs)("div",{className:"af-mb-6",children:[(0,ue.jsx)("h2",{className:"af-text-lg af-font-semibold",children:"Payments"}),(0,ue.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"Manage your products, orders, and payment setup."})]}),(0,ue.jsxs)(ks,{value:c,onValueChange:H=>p(H),children:[(0,ue.jsxs)(Xa,{className:"af-mb-4 af-h-auto af-flex-wrap",children:[(0,ue.jsxs)(ea,{value:"products",className:"af-gap-2",children:[(0,ue.jsx)(Mt.Package,{className:"af-h-4 af-w-4"}),"Products"]}),(0,ue.jsxs)(ea,{value:"orders",className:"af-gap-2",children:[(0,ue.jsx)(Mt.Receipt,{className:"af-h-4 af-w-4"}),"Orders"]}),(0,ue.jsxs)(ea,{value:"invoices",className:"af-gap-2",children:[(0,ue.jsx)(Mt.FileText,{className:"af-h-4 af-w-4"}),"Invoices"]}),(0,ue.jsxs)(ea,{value:"customers",className:"af-gap-2",children:[(0,ue.jsx)(Mt.Users,{className:"af-h-4 af-w-4"}),"Customers"]}),(0,ue.jsxs)(ea,{value:"setup",className:"af-gap-2",children:[(0,ue.jsx)(Mt.Settings,{className:"af-h-4 af-w-4"}),"Setup"]})]}),(0,ue.jsx)(ta,{value:"products",children:g==="list"?(0,ue.jsx)(ds,{productsState:b,onSelectProduct:u,onCreateProduct:h}):(0,ue.jsxs)("div",{children:[(0,ue.jsxs)("div",{className:"af-mb-6",children:[(0,ue.jsxs)(w,{variant:"ghost",size:"sm",onClick:R,className:"af-mb-2 af--ml-2",children:[(0,ue.jsx)(Mt.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Back to Products"]}),(0,ue.jsx)("h3",{className:"af-text-lg af-font-semibold",children:g==="create"?"New Product":y?.name})]}),g==="create"&&!d?(0,ue.jsx)("div",{className:"af-flex af-items-center af-justify-center af-py-8",children:x?(0,ue.jsxs)("div",{className:"af-text-destructive",children:["Error: ",x.message]}):(0,ue.jsx)("div",{className:"af-text-muted-foreground",children:"Loading..."})}):(0,ue.jsx)(ms,{productsState:b,product:g==="edit"?y:null,currency:d??"usd",onSaved:C},g==="edit"?y?.id??"edit":"new")]})}),(0,ue.jsx)(ta,{value:"orders",children:(0,ue.jsx)(Ba,{onRefund:t})}),(0,ue.jsx)(ta,{value:"invoices",children:(0,ue.jsx)(Ii,{onConnectStripe:s,onUpgradePlan:r,needsPaidPlan:S?.reason==="needs_paid_plan"})}),(0,ue.jsx)(ta,{value:"customers",children:(0,ue.jsx)(hs,{})}),(0,ue.jsx)(ta,{value:"setup",children:(0,ue.jsx)(xs,{onConnectStripe:s,onUpgradePlan:r})})]})]})}function Ii({onConnectStripe:e,onUpgradePlan:a,needsPaidPlan:s}){let t=Ma(),[r,c]=Ka.useState(null),[p,g]=Ka.useState(!1);return(0,ue.jsxs)(ue.Fragment,{children:[!p&&(0,ue.jsx)($a,{retainers:t.retainers,isLoading:t.isLoading,error:t.error,onRetry:()=>void t.refetch(),activeRetainerId:r?.id??null,onShowCycles:v=>c(y=>y?.id===v.id?null:v),total:t.total,endedTotal:t.endedTotal,isLoadingMore:t.isLoadingMore,onLoadMore:()=>void t.loadMore(),includeEnded:t.includeEnded,onIncludeEndedChange:t.setIncludeEnded}),(0,ue.jsx)(Ua,{onDetailOpenChange:g,onConnectStripe:e,onUpgradePlan:a,needsPaidPlan:s,retainerFilter:r?{id:r.id,label:`${r.customer_name??r.customer_email??"Recurring invoices"} \u2014 ${r.description}`}:null,onClearRetainerFilter:()=>c(null)})]})}var hn=se(require("react"),1);var ca=se(require("react"),1);function Ns({refreshKey:e}={}){let{api:a}=te(),[s,t]=ca.useState(null),[r,c]=ca.useState(!0),[p,g]=ca.useState(null),v=ca.useRef(0),y=ca.useCallback(async()=>{let n=++v.current;try{c(!0),g(null);let b=await a.getBusinessSummary({tzOffsetMinutes:-new Date().getTimezoneOffset()});if(n!==v.current)return;t(b)}catch(b){if(n!==v.current)return;g(b instanceof Error?b:new Error("Failed to fetch summary"))}finally{n===v.current&&c(!1)}},[a,e]);return ca.useEffect(()=>{y()},[y]),{summary:s,isLoading:r,error:p,refetch:y}}var Le=require("react/jsx-runtime");function yn(e,a){if(e.settled.complete)return{headline:z(e.settled.gross,e.settled.currency),extra:[]};let s=e.totals[0];return s?{headline:z(s.gross,s.currency),extra:e.totals.slice(1).map(t=>`+ ${z(t.gross,t.currency)} in ${t.currency.toUpperCase()}`)}:{headline:z(0,a),extra:[]}}function ur({refreshKey:e,initialOrderId:a,isOwner:s=!1,onRefund:t,onBackToWebsite:r,className:c}){let{summary:p,isLoading:g,error:v,refetch:y}=Ns({refreshKey:e}),[n,b]=hn.useState(!1),S=(0,Le.jsx)("div",{className:"af-mb-6",children:v&&!p?(0,Le.jsx)(Tt,{message:"Couldn't load your revenue summary.",onRetry:()=>y()}):(0,Le.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-3 sm:af-grid-cols-3",children:g||!p?(0,Le.jsxs)(Le.Fragment,{children:[(0,Le.jsx)(Ve,{label:"This week",children:(0,Le.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,Le.jsx)(Ve,{label:"Today",children:(0,Le.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,Le.jsx)(Ve,{label:"Needs attention",children:(0,Le.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,Le.jsx)(Le.Fragment,{children:(()=>{let x=yn(p.revenue.d7,p.currency),d=yn(p.revenue.today,p.currency);return(0,Le.jsxs)(Le.Fragment,{children:[(0,Le.jsx)(Ve,{label:"This week",detail:x.extra.length>0?(0,Le.jsx)("div",{children:x.extra.map(u=>(0,Le.jsx)("div",{children:u},u))}):void 0,children:x.headline}),(0,Le.jsx)(Ve,{label:"Today",detail:d.extra.length>0?(0,Le.jsx)("div",{children:d.extra.map(u=>(0,Le.jsx)("div",{children:u},u))}):void 0,children:d.headline}),(0,Le.jsx)(Ve,{label:"Needs attention",valueClassName:p.needs_attention.count>0?"af-text-2xl af-font-semibold af-tabular-nums af-text-destructive":"af-text-2xl af-font-semibold af-tabular-nums af-text-muted-foreground",detail:p.needs_attention.count===0?"All clear":p.needs_attention.items[0]?.title??void 0,children:p.needs_attention.count})]})})()})})});return(0,Le.jsxs)(Ct,{title:"Orders",onBackToWebsite:r,hideBackButton:n,className:c,children:[S,(0,Le.jsx)(Ba,{refreshKey:e,initialOrderId:a,onDetailOpenChange:b,showFulfillment:!0,onRefund:s?t:void 0,emptyMessage:"Orders will appear here \u2014 your store is live."})]})}var ha=se(require("react"),1),Qa=require("lucide-react");var fa=se(require("react"),1),Qs=require("lucide-react");var it=require("react/jsx-runtime"),xn="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",Ea="af-text-xs af-text-muted-foreground",js="af-text-sm af-font-medium af-tabular-nums",Di=3;function Li(e){let a=e.pricing,s=a?.currency??e.currency;if(a?.custom_amount??e.allow_custom_amount){let v=e.min_amount??a?.min_amount??null,y=e.max_amount??a?.max_amount??null;return v!==null&&y!==null?{text:`Buyer chooses (${z(v,s)} \u2013 ${z(y,s)})`,compareAt:null}:{text:"Buyer chooses",compareAt:null}}let r=a?.min_amount??e.unit_amount,c=a?.max_amount??e.unit_amount;if(r===null||c===null)return{text:"Varies by variant",compareAt:null};let p=r===c?z(r,s):`${z(r,s)} \u2013 ${z(c,s)}`,g=a?.compare_at_amount??null;return{text:p,compareAt:g!==null&&g>c?z(g,s):null}}function qs({product:e,className:a}){let s=Li(e);return(0,it.jsxs)("div",{className:a,children:[(0,it.jsx)("span",{className:js,children:s.text}),s.compareAt&&(0,it.jsx)("span",{className:"af-ml-2 af-text-xs af-text-muted-foreground af-line-through af-tabular-nums",children:s.compareAt})]})}function Gs({product:e}){let a=e.inventory;return a?a.tracked?a.out_of_stock?(0,it.jsx)(W,{variant:"destructive",children:"Out of stock"}):a.low_stock?(0,it.jsxs)(W,{variant:"warning",children:["Low stock \xB7 ",a.available]}):a.allow_backorder?(0,it.jsx)(W,{variant:"secondary",children:"Backorder"}):a.available===null?null:(0,it.jsxs)(W,{variant:"secondary",children:["In stock \xB7 ",a.available]}):null:e.stats?.low_stock?(0,it.jsx)(W,{variant:"warning",children:"Low stock"}):null}function Ws(e){switch(e.fulfillment.type){case"download":return"Digital download";case"redirect":return"Redirect";default:return e.collect_shipping_address?"Ships":null}}function Ks(e){return e.map(a=>z(a.amount,a.currency)).join(" + ")}function Nn(e){return e.allow_custom_amount?e.min_amount!==null&&e.max_amount!==null?`Buyer chooses (${z(e.min_amount,e.currency)} \u2013 ${z(e.max_amount,e.currency)})`:"Buyer chooses":e.unit_amount===null?"\u2014":z(e.unit_amount,e.currency)}function wn({variant:e}){let a=e.inventory;if(!a||!a.tracked)return(0,it.jsx)("span",{className:"af-text-muted-foreground",children:"Not tracked"});let s=a.available??0;return a.allow_backorder?(0,it.jsxs)("span",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,it.jsx)("span",{className:"af-tabular-nums",children:Math.max(0,s)}),(0,it.jsx)(W,{variant:"secondary",children:s<0?`${Math.abs(s)} on backorder`:"Backorder"})]}):s<=0?(0,it.jsx)(W,{variant:"destructive",children:"Out of stock"}):(0,it.jsxs)("span",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,it.jsx)("span",{className:"af-tabular-nums",children:s}),s<=Di&&(0,it.jsx)(W,{variant:"warning",children:"Low"})]})}var V=require("react/jsx-runtime");function ws({title:e,children:a,className:s}){return(0,V.jsxs)("section",{className:M("af-rounded-lg af-border af-p-4",s),children:[(0,V.jsx)("h4",{className:xn,children:e}),(0,V.jsx)("div",{className:"af-mt-3",children:a})]})}function Sn({src:e,className:a}){let[s,t]=fa.useState(!1);return fa.useEffect(()=>t(!1),[e]),!e||s?(0,V.jsx)("div",{"aria-hidden":"true",className:M("af-flex af-items-center af-justify-center af-bg-muted",a),children:(0,V.jsx)(Qs.ImageOff,{className:"af-h-8 af-w-8 af-text-muted-foreground"})}):(0,V.jsx)("img",{src:e,alt:"",loading:"lazy",onError:()=>t(!0),className:M("af-object-cover",a)})}function Pn({product:e,showStats:a=!1,onBack:s}){let{api:t}=te(),[r,c]=fa.useState(null),[p,g]=fa.useState(!0),[v,y]=fa.useState(null),[n,b]=fa.useState(0),[S,x]=fa.useState(0);fa.useEffect(()=>{let f=!1;return g(!0),y(null),t.getProduct(e.id).then(L=>{f||c(L)}).catch(L=>{f||y(L instanceof Error?L:new Error("Failed to load product"))}).finally(()=>{f||g(!1)}),()=>{f=!0}},[t,e.id,n]);let d=r?{...e,...r}:e,u=d.images.length>0?d.images:e.images,h=u[S]??u[0],R=Ws(d),C=a?d.stats:void 0,H=r?.variants??[],G=H.length>1;return(0,V.jsxs)("div",{children:[(0,V.jsxs)("div",{className:"af-mb-6",children:[(0,V.jsxs)(w,{variant:"ghost",size:"sm",onClick:s,className:"af-mb-2 af--ml-2",children:[(0,V.jsx)(Qs.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Products"]}),(0,V.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,V.jsx)("h3",{className:"af-text-xl af-font-semibold af-break-words",children:d.name}),(0,V.jsx)(W,{variant:d.active?"success":"secondary",children:d.active?"Active":"Draft"}),(0,V.jsx)(Gs,{product:d})]}),(0,V.jsx)(qs,{product:d,className:"af-mt-1"}),!d.active&&(0,V.jsx)("p",{className:"af-mt-1 af-text-xs af-text-muted-foreground",children:"Drafts are hidden from your website and can't be bought."})]}),(0,V.jsxs)("div",{className:"af-grid af-gap-6 lg:af-grid-cols-[280px_1fr]",children:[(0,V.jsxs)("div",{className:"af-space-y-2",children:[(0,V.jsx)(Sn,{src:h,className:"af-h-48 af-w-full af-rounded-md af-border"}),u.length>1&&(0,V.jsx)("div",{className:"af-flex af-flex-wrap af-gap-2",children:u.map((f,L)=>(0,V.jsx)("button",{type:"button",onClick:()=>x(L),"aria-label":`Show photo ${L+1} of ${u.length}`,"aria-pressed":L===S,className:M("af-rounded-md af-border focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",L===S?"af-ring-2 af-ring-ring":"af-opacity-80 hover:af-opacity-100"),children:(0,V.jsx)(Sn,{src:f,className:"af-h-12 af-w-12 af-rounded-md"})},`${f}-${L}`))})]}),(0,V.jsxs)("div",{className:"af-space-y-6",children:[d.description&&(0,V.jsx)(ws,{title:"Description",children:(0,V.jsx)("p",{className:"af-whitespace-pre-wrap af-break-words af-text-sm",children:d.description})}),p?(0,V.jsxs)(ws,{title:"Variants",children:[(0,V.jsx)("div",{className:"af-space-y-2","aria-hidden":"true",children:[0,1,2].map(f=>(0,V.jsxs)("div",{className:"af-flex af-items-center af-justify-between af-gap-4",children:[(0,V.jsx)(K,{className:"af-h-4 af-w-28"}),(0,V.jsx)(K,{className:"af-h-4 af-w-20"}),(0,V.jsx)(K,{className:"af-h-4 af-w-16"})]},f))}),(0,V.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading product details"})]}):v?(0,V.jsx)(Tt,{message:"Couldn't load this product's variants.",onRetry:()=>b(f=>f+1)}):G?(0,V.jsx)(ws,{title:`Variants \xB7 ${H.length}`,children:(0,V.jsx)("div",{className:"af-overflow-x-auto",children:(0,V.jsxs)(_e,{children:[(0,V.jsx)(ke,{children:(0,V.jsxs)(oe,{children:[(0,V.jsx)(I,{children:"Variant"}),(0,V.jsx)(I,{children:"SKU"}),(0,V.jsx)(I,{className:"af-text-right",children:"Price"}),(0,V.jsx)(I,{children:"Stock"})]})}),(0,V.jsx)(Ee,{children:H.map(f=>(0,V.jsxs)(oe,{children:[(0,V.jsxs)(D,{className:"af-font-medium",children:[f.title||f.option_values.map(L=>L.value).join(" / ")||"\u2014",!f.active&&(0,V.jsx)(W,{variant:"secondary",className:"af-ml-2",children:"Hidden"})]}),(0,V.jsx)(D,{className:"af-text-muted-foreground",children:f.sku?(0,V.jsx)("code",{className:"af-font-mono af-text-xs af-break-all",children:f.sku}):"\u2014"}),(0,V.jsxs)(D,{className:"af-whitespace-nowrap af-text-right",children:[(0,V.jsx)("div",{className:js,children:Nn(f)}),f.compare_at_amount!==null&&f.unit_amount!==null&&f.compare_at_amount>f.unit_amount&&(0,V.jsx)("div",{className:"af-text-xs af-text-muted-foreground af-line-through af-tabular-nums",children:z(f.compare_at_amount,f.currency)})]}),(0,V.jsx)(D,{className:"af-text-sm",children:(0,V.jsx)(wn,{variant:f})})]},f.id))})]})})}):null,(0,V.jsx)(ws,{title:"Details",children:(0,V.jsxs)("dl",{className:"af-space-y-2",children:[(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Delivery"}),(0,V.jsx)("dd",{className:"af-text-sm",children:R==="Digital download"?d.fulfillment.filename??"Digital download":R??"Nothing to deliver"})]}),d.fulfillment.type==="redirect"&&d.fulfillment.url&&(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Sends buyers to"}),(0,V.jsx)("dd",{className:"af-break-all af-text-sm",children:d.fulfillment.url})]}),(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Shipping address"}),(0,V.jsx)("dd",{className:"af-text-sm",children:d.collect_shipping_address?"Collected at checkout":"Not collected"})]}),(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Most one buyer can order"}),(0,V.jsx)("dd",{className:"af-text-sm af-tabular-nums",children:d.max_quantity})]})]})}),C&&(0,V.jsx)(ws,{title:"Last 30 days",children:(0,V.jsxs)("dl",{className:"af-grid af-grid-cols-3 af-gap-3",children:[(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Units sold"}),(0,V.jsx)("dd",{className:"af-text-sm af-font-medium af-tabular-nums",children:C.units_sold})]}),(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Orders"}),(0,V.jsx)("dd",{className:"af-text-sm af-font-medium af-tabular-nums",children:C.orders})]}),(0,V.jsxs)("div",{children:[(0,V.jsx)("dt",{className:Ea,children:"Gross"}),(0,V.jsx)("dd",{className:js,children:C.gross.length>0?Ks(C.gross):"\u2014"})]})]})}),(0,V.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"Ask the AI to change anything here \u2014 price, stock, photos, wording."})]})]})]})}var B=require("react/jsx-runtime"),Oi={all:"All",active:"Active",draft:"Draft",low_stock:"Low stock",out_of_stock:"Out of stock"},Bi=["all","active","draft","low_stock","out_of_stock"];function Rn(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`,s=new Date(a).getTime();return Number.isNaN(s)?0:s}function Cn(e){return e.inventory?.low_stock??e.stats?.low_stock??!1}function Ai(e){return e.inventory?.out_of_stock??!1}function Fi(e,a){switch(a){case"active":return e.active;case"draft":return!e.active;case"low_stock":return Cn(e);case"out_of_stock":return Ai(e);default:return!0}}function Ui({product:e,showStats:a,onOpen:s}){let t=e.images[0],r=a?e.stats:void 0,c=Ws(e),p=e.variant_count??0;return(0,B.jsxs)("button",{type:"button",onClick:s,"aria-label":`Open ${e.name}`,className:"af-block af-w-full af-overflow-hidden af-rounded-md af-border af-text-left af-transition-colors hover:af-bg-muted/50 focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:[t?(0,B.jsx)("img",{src:t,alt:"",loading:"lazy",className:"af-h-36 af-w-full af-object-cover"}):(0,B.jsx)("div",{"aria-hidden":"true",className:"af-flex af-h-36 af-w-full af-items-center af-justify-center af-bg-muted",children:(0,B.jsx)(Qa.ImageOff,{className:"af-h-8 af-w-8 af-text-muted-foreground"})}),(0,B.jsxs)("div",{className:"af-space-y-2 af-p-3",children:[(0,B.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,B.jsxs)("div",{className:"af-min-w-0",children:[(0,B.jsx)("div",{className:"af-truncate af-text-sm af-font-medium af-text-foreground",children:e.name}),(0,B.jsx)(qs,{product:e,className:"af-mt-0.5"})]}),!e.active&&(0,B.jsx)(W,{variant:"secondary",className:"af-shrink-0",children:"Draft"})]}),(0,B.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,B.jsx)(Gs,{product:e}),p>1&&(0,B.jsxs)(W,{variant:"outline",children:[p," variants"]}),c&&(0,B.jsx)(W,{variant:"outline",children:c})]}),r&&(0,B.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:[r.units_sold," sold",r.gross.length>0?` \xB7 ${Ks(r.gross)}`:""," ",(0,B.jsx)("span",{className:"af-text-muted-foreground/70",children:"last 30 days"})]})]})]})}function $i(){return(0,B.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-4 sm:af-grid-cols-2 lg:af-grid-cols-3",children:[0,1,2,3,4,5].map(e=>(0,B.jsxs)("div",{"aria-hidden":"true",className:"af-overflow-hidden af-rounded-md af-border",children:[(0,B.jsx)("div",{className:"af-h-36 af-w-full af-animate-pulse af-bg-muted"}),(0,B.jsxs)("div",{className:"af-space-y-2 af-p-3",children:[(0,B.jsx)(K,{className:"af-w-32"}),(0,B.jsx)(K,{className:"af-w-16"}),(0,B.jsx)(K,{className:"af-h-5 af-w-24"})]})]},e))})}function mr({refreshKey:e,showStats:a=!1,onBackToWebsite:s,className:t}){let{products:r,isLoading:c,error:p,refetch:g}=Ha({includeStats:a,refreshKey:e}),[v,y]=ha.useState(null),[n,b]=ha.useState("all"),[S,x]=ha.useState(""),[d,u]=ha.useState("newest"),h=ha.useMemo(()=>{if(!a)return null;let k=null;for(let ne of r)!ne.stats||ne.stats.units_sold===0||(!k||ne.stats.units_sold>(k.stats?.units_sold??0))&&(k=ne);return k},[r,a]),R=ha.useMemo(()=>r.filter(Cn).length,[r]),C=S.trim().toLowerCase(),H=ha.useMemo(()=>{let ne=[...r.filter(m=>Fi(m,n)&&(!C||m.name.toLowerCase().includes(C)))];return d==="name"?ne.sort((m,Y)=>m.name.localeCompare(Y.name)):d==="best_selling"?ne.sort((m,Y)=>(Y.stats?.units_sold??0)-(m.stats?.units_sold??0)):ne.sort((m,Y)=>Rn(Y.created_at)-Rn(m.created_at)),ne},[r,n,C,d]),G=n!=="all"||!!C,f=()=>{b("all"),x("")},L=c||p!==null&&r.length===0,$=(0,B.jsx)("div",{className:"af-mb-6",children:(0,B.jsx)("div",{className:M("af-grid af-grid-cols-1 af-gap-3",a?"sm:af-grid-cols-3":"sm:af-grid-cols-2"),children:L?(0,B.jsxs)(B.Fragment,{children:[(0,B.jsx)(Ve,{label:"Products",children:(0,B.jsx)(K,{className:"af-h-6 af-w-10"})}),a?(0,B.jsxs)(B.Fragment,{children:[(0,B.jsx)(Ve,{label:"Best seller",children:(0,B.jsx)(K,{className:"af-h-6 af-w-28"})}),(0,B.jsx)(Ve,{label:"Low stock",children:(0,B.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,B.jsx)(Ve,{label:"Active",children:(0,B.jsx)(K,{className:"af-h-6 af-w-10"})})]}):(0,B.jsxs)(B.Fragment,{children:[(0,B.jsx)(Ve,{label:"Products",children:r.length}),a?(0,B.jsxs)(B.Fragment,{children:[(0,B.jsx)(Ve,{label:"Best seller",valueClassName:"af-text-sm af-font-medium",detail:h?`${h.stats?.units_sold??0} sold, last 30 days`:void 0,children:h?(0,B.jsx)("span",{className:"af-block af-truncate af-text-sm af-font-medium",children:h.name}):(0,B.jsx)("span",{className:"af-block af-text-sm af-font-medium af-text-muted-foreground",children:"No sales yet"})}),(0,B.jsx)(Ve,{label:"Low stock",children:R})]}):(0,B.jsx)(Ve,{label:"Active",children:r.filter(k=>k.active).length})]})})}),Q=(0,B.jsxs)("div",{className:"af-mb-4 af-space-y-3",children:[(0,B.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,B.jsx)("div",{role:"group","aria-label":"Product views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:Bi.map(k=>(0,B.jsx)("button",{type:"button","aria-pressed":n===k,onClick:()=>b(k),className:M("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",n===k?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:Oi[k]},k))}),(0,B.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,B.jsx)(Qa.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,B.jsx)(me,{type:"text",value:S,onChange:k=>x(k.target.value),placeholder:"Search products","aria-label":"Search products by name",className:"af-pl-8"})]}),(0,B.jsxs)(Lt,{value:d,onValueChange:k=>u(k),children:[(0,B.jsx)(Nt,{className:"af-h-9 af-w-[150px]","aria-label":"Sort products",children:(0,B.jsx)(Ot,{})}),(0,B.jsxs)(wt,{children:[(0,B.jsx)(Fe,{value:"newest",children:"Newest"}),(0,B.jsx)(Fe,{value:"name",children:"Name A\u2013Z"}),a&&(0,B.jsx)(Fe,{value:"best_selling",children:"Best selling"})]})]}),G&&(0,B.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:f,children:"Clear filters"})]}),c&&r.length===0?(0,B.jsx)(K,{className:"af-h-4 af-w-28"}):(0,B.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:G?`${H.length} of ${r.length} products`:`${r.length} ${r.length===1?"product":"products"}${R>0?` \xB7 ${R} low stock`:""}`})]}),q;return v?q=(0,B.jsx)(Pn,{product:v,showStats:a,onBack:()=>{y(null),g()}},v.id):c&&r.length===0?q=(0,B.jsxs)(B.Fragment,{children:[Q,(0,B.jsx)($i,{}),(0,B.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading products"})]}):p&&r.length===0?q=(0,B.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,B.jsx)(Tt,{message:"Couldn't load your products.",onRetry:()=>g()})}):r.length===0?q=(0,B.jsxs)(ia,{icon:(0,B.jsx)(Qa.Package,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:["No products yet.",(0,B.jsx)("br",{}),"Ask the AI to add one \u2014 try \u201Cadd a $25 t-shirt in S, M and L\u201D."]}):q=(0,B.jsxs)(B.Fragment,{children:[Q,p&&(0,B.jsx)("div",{className:"af-mb-4",children:(0,B.jsx)(Tt,{message:"Couldn't refresh your products \u2014 showing the last version loaded.",onRetry:()=>g()})}),H.length===0?(0,B.jsxs)(ia,{children:["No products match these filters.",(0,B.jsx)("br",{}),(0,B.jsx)(w,{type:"button",variant:"ghost",size:"sm",className:"af-mt-2",onClick:f,children:"Show all products"})]}):(0,B.jsx)("div",{className:"af-grid af-grid-cols-1 af-gap-4 sm:af-grid-cols-2 lg:af-grid-cols-3",children:H.map(k=>(0,B.jsx)(Ui,{product:k,showStats:a,onOpen:()=>y(k)},k.id))})]}),(0,B.jsxs)(Ct,{title:"Products",onBackToWebsite:s,hideBackButton:!!v,className:t,children:[!v&&$,q]})}var Xt=se(require("react"),1),We=require("lucide-react");var Et=se(require("react"),1);function Ss({initialPageSize:e=25,refreshKey:a}={}){let{api:s}=te(),[t,r]=Et.useState([]),[c,p]=Et.useState(!0),[g,v]=Et.useState(null),[y,n]=Et.useState(0),[b]=Et.useState(e),[S,x]=Et.useState(""),d=Et.useRef(0),u=Et.useCallback(async()=>{let h=++d.current;try{p(!0),v(null);let R={limit:b,offset:y*b};S.trim()&&(R.q=S.trim());let C=await s.getPeople(R);if(h!==d.current)return;r(C.people)}catch(R){if(h!==d.current)return;v(R instanceof Error?R:new Error("Failed to fetch people"))}finally{h===d.current&&p(!1)}},[s,y,b,S,a]);return Et.useEffect(()=>{u()},[u]),{people:t,isLoading:c,error:g,page:y,pageSize:b,hasNextPage:t.length===b,hasPreviousPage:y>0,setPage:n,q:S,setQ:h=>{x(h),n(0)},refetch:u}}var da=se(require("react"),1);function Ps(e,{refreshKey:a}={}){let{api:s}=te(),[t,r]=da.useState(null),[c,p]=da.useState(e!==null),[g,v]=da.useState(null),y=da.useRef(0),n=da.useCallback(async()=>{if(e===null){r(null),p(!1),v(null);return}let b=++y.current;try{p(!0),v(null);let S=await s.getPerson(e);if(b!==y.current)return;r(S)}catch(S){if(b!==y.current)return;v(S instanceof Error?S:new Error("Failed to fetch person"))}finally{b===y.current&&p(!1)}},[s,e,a]);return da.useEffect(()=>{n()},[n]),{person:t,isLoading:c,error:g,refetch:n}}var o=require("react/jsx-runtime"),pr="af-text-[11px] af-font-semibold af-uppercase af-tracking-wide af-text-muted-foreground",Vi="af-text-xs af-text-muted-foreground";function Za(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function gr(e,a){let s=a?`Re: ${a}`:"Re: your message";return`mailto:${encodeURIComponent(e)}?subject=${encodeURIComponent(s)}`}function Hi(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function Mi({value:e,label:a}){let[s,t]=Xt.useState(!1);return Hi()?(0,o.jsxs)(w,{variant:"outline",size:"sm",onClick:async()=>{try{await navigator.clipboard.writeText(e),t(!0),setTimeout(()=>t(!1),1500)}catch{}},"aria-label":a,className:"af-shrink-0",children:[s?(0,o.jsx)(We.Check,{className:"af-h-3.5 af-w-3.5 af-mr-1"}):(0,o.jsx)(We.ClipboardCopy,{className:"af-h-3.5 af-w-3.5 af-mr-1"}),s?"Copied":"Copy email"]}):null}function Tn(e){return(0,o.jsxs)("span",{className:"af-inline-flex af-flex-wrap af-gap-1",children:[e.sources.includes("customer")&&(0,o.jsxs)(W,{variant:"success",children:["Customer \xB7 ",e.order_count," ",e.order_count===1?"order":"orders"]}),e.sources.includes("lead")&&(0,o.jsx)(W,{variant:"secondary",children:"Contact form"})]})}function _n(e){return e.name?e.name:e.email?e.email:(0,o.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"Form submission"})}var zi=/^(message|messages|comment|comments|enquiry|inquiry|question|questions|details|notes|note|body)$/i,ji=40;function qi(e){let a=Object.entries(e).filter(([r])=>!r.startsWith("_")),t=a.find(([r,c])=>zi.test(r)&&typeof c=="string"&&c.trim())??a.filter(([,r])=>typeof r=="string"&&r.trim().length>=ji).sort((r,c)=>String(c[1]).trim().length-String(r[1]).trim().length)[0];return t?{message:String(t[1]),rest:a.filter(([r])=>r!==t[0])}:{message:null,rest:a}}function Gi(e){return e.replace(/_/g," ")}function Wi(e){if(typeof e=="string")return e;if(e==null)return"";try{return JSON.stringify(e)}catch{return String(e)}}function Ki({data:e}){let{message:a,rest:s}=qi(e);return a===null&&s.length===0?(0,o.jsx)("p",{className:"af-text-sm af-text-muted-foreground",children:"No data submitted"}):(0,o.jsxs)("div",{className:"af-space-y-3",children:[a!==null&&(0,o.jsx)("p",{className:"af-whitespace-pre-wrap af-break-words af-text-sm",children:a}),s.length>0&&(0,o.jsx)("dl",{className:"af-space-y-1",children:s.map(([t,r])=>(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-gap-x-2",children:[(0,o.jsx)("dt",{className:M(Vi,"af-w-28 af-shrink-0 af-capitalize"),children:Gi(t)}),(0,o.jsx)("dd",{className:"af-min-w-0 af-break-words af-text-sm",children:Wi(r)})]},t))})]})}function kn({formName:e,createdAt:a,data:s}){return(0,o.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,o.jsxs)("div",{className:"af-mb-2 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,o.jsx)(W,{variant:"outline",children:"Form submission (unverified)"}),e&&(0,o.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:e}),(0,o.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:Za(a)})]}),(0,o.jsx)(Ki,{data:s})]})}function Qi(e){return e.charged_total??e.amount_total}function Zi({email:e,refreshKey:a,onBack:s}){let{person:t,isLoading:r,error:c,refetch:p}=Ps(e,{refreshKey:a}),g=gr(e,t?.submissions[0]?.form_name);return(0,o.jsxs)("div",{children:[(0,o.jsxs)("div",{className:"af-mb-6",children:[(0,o.jsxs)(w,{variant:"ghost",size:"sm",onClick:s,className:"af-mb-2 af--ml-2",children:[(0,o.jsx)(We.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"People"]}),(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-start af-justify-between af-gap-3",children:[(0,o.jsxs)("div",{className:"af-min-w-0",children:[r?(0,o.jsx)(K,{className:"af-h-7 af-w-48"}):(0,o.jsx)("h3",{className:"af-text-xl af-font-semibold af-break-all",children:t?.name||e}),(0,o.jsxs)("div",{className:"af-mt-1 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,o.jsx)("span",{className:"af-break-all af-text-sm af-text-muted-foreground",children:e}),(0,o.jsx)(Mi,{value:e,label:"Copy email address"})]})]}),(0,o.jsx)(w,{asChild:!0,children:(0,o.jsxs)("a",{href:g,children:[(0,o.jsx)(We.Mail,{className:"af-h-4 af-w-4 af-mr-1"}),"Reply"]})})]})]}),r?(0,o.jsxs)("div",{className:"af-space-y-3","aria-hidden":"true",children:[[0,1,2].map(v=>(0,o.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,o.jsx)(K,{className:"af-w-40"}),(0,o.jsx)(K,{className:"af-mt-2 af-w-64"})]},v)),(0,o.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading this person"})]}):c||!t?(0,o.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,o.jsx)(Tt,{message:"Couldn't load this person.",onRetry:()=>p()})}):(0,o.jsxs)("div",{className:"af-space-y-6",children:[t.customer&&(0,o.jsxs)("div",{children:[(0,o.jsx)("h4",{className:M(pr,"af-mb-3"),children:"Customer"}),(0,o.jsxs)("dl",{className:"af-space-y-1 af-text-sm",children:[t.customer.name&&(0,o.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,o.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Name"}),(0,o.jsx)("dd",{children:t.customer.name})]}),t.customer.phone&&(0,o.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,o.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Phone"}),(0,o.jsx)("dd",{children:t.customer.phone})]}),(0,o.jsxs)("div",{className:"af-flex af-gap-2",children:[(0,o.jsx)("dt",{className:"af-w-32 af-shrink-0 af-text-muted-foreground",children:"Customer since"}),(0,o.jsx)("dd",{children:Za(t.customer.created_at)})]})]})]}),t.orders.length>0&&(0,o.jsxs)("div",{children:[(0,o.jsx)("h4",{className:M(pr,"af-mb-3"),children:"Purchases"}),(0,o.jsx)("div",{className:"af-overflow-x-auto",children:(0,o.jsxs)(_e,{children:[(0,o.jsx)(ke,{children:(0,o.jsxs)(oe,{children:[(0,o.jsx)(I,{children:"Order"}),(0,o.jsx)(I,{children:"Date"}),(0,o.jsx)(I,{className:"af-text-right",children:"Total"}),(0,o.jsx)(I,{children:"Status"})]})}),(0,o.jsx)(Ee,{children:t.orders.map(v=>(0,o.jsxs)(oe,{children:[(0,o.jsx)(D,{className:"af-whitespace-nowrap af-font-medium",children:ba(v)}),(0,o.jsx)(D,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Za(v.created_at)}),(0,o.jsx)(D,{className:"af-whitespace-nowrap af-text-right af-font-medium af-tabular-nums",children:z(Qi(v),v.currency)}),(0,o.jsx)(D,{children:(0,o.jsx)(Ca,{status:v.status})})]},v.id))})]})}),(0,o.jsx)("p",{className:"af-mt-2 af-text-xs af-text-muted-foreground",children:"Ask the AI to open one of these orders for the full details."})]}),t.submissions.length>0&&(0,o.jsxs)("div",{children:[(0,o.jsx)("h4",{className:M(pr,"af-mb-3"),children:"Messages"}),(0,o.jsx)("div",{className:"af-space-y-3",children:t.submissions.map(v=>(0,o.jsx)(kn,{formName:v.form_name,createdAt:v.created_at,data:v.data},v.id))})]})]})]})}function Ji({submissionId:e,onBack:a}){let{api:s}=te(),[t,r]=Xt.useState(null),[c,p]=Xt.useState(!0),[g,v]=Xt.useState(null),[y,n]=Xt.useState(0);return Xt.useEffect(()=>{let b=!1;return p(!0),v(null),s.getSubmission(e).then(S=>{b||r(S)}).catch(S=>{b||v(S instanceof Error?S:new Error("Failed to load submission"))}).finally(()=>{b||p(!1)}),()=>{b=!0}},[s,e,y]),(0,o.jsxs)("div",{children:[(0,o.jsxs)("div",{className:"af-mb-6",children:[(0,o.jsxs)(w,{variant:"ghost",size:"sm",onClick:a,className:"af-mb-2 af--ml-2",children:[(0,o.jsx)(We.ArrowLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"People"]}),(0,o.jsx)("h3",{className:"af-text-xl af-font-semibold",children:"Form submission"}),c?(0,o.jsx)(K,{className:"af-mt-1 af-h-4 af-w-32"}):t&&(0,o.jsx)("p",{className:"af-mt-1 af-text-sm af-text-muted-foreground",children:Za(t.metadata.submitted_at)})]}),c?(0,o.jsxs)("div",{className:"af-rounded-md af-border af-p-3","aria-hidden":"true",children:[(0,o.jsx)(K,{className:"af-w-40"}),(0,o.jsx)(K,{className:"af-mt-2 af-w-64"}),(0,o.jsx)(K,{className:"af-mt-2 af-w-52"}),(0,o.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading this submission"})]}):g||!t?(0,o.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,o.jsx)(Tt,{message:"Couldn't load this submission.",onRetry:()=>n(b=>b+1)})}):(0,o.jsxs)("div",{className:"af-space-y-3",children:[(0,o.jsx)(kn,{formName:null,createdAt:t.metadata.submitted_at,data:t.data}),(0,o.jsx)("p",{className:"af-text-xs af-text-muted-foreground",children:"No email address came with this submission, so there is nobody to reply to."})]})]})}var Xi={all:"All",customers:"Customers",leads:"Leads"},Yi=["all","customers","leads"];function eo(e,a){return a==="customers"?e.sources.includes("customer"):a==="leads"?e.sources.includes("lead"):!0}function vr({refreshKey:e,onBackToWebsite:a,className:s}){let{people:t,isLoading:r,error:c,page:p,pageSize:g,hasNextPage:v,hasPreviousPage:y,setPage:n,q:b,setQ:S,refetch:x}=Ss({refreshKey:e}),[d,u]=Xt.useState({kind:"list"}),[h,R]=Xt.useState(""),[C,H]=Xt.useState("all"),G=q=>{q.email?u({kind:"person",email:q.email}):q.submission_id&&u({kind:"submission",submissionId:q.submission_id})},f=()=>{u({kind:"list"}),x()},L=q=>{let k=q.trim();k!==b&&S(k)},$=t.filter(q=>eo(q,C)),Q;if(d.kind==="person")Q=(0,o.jsx)(Zi,{email:d.email,refreshKey:e,onBack:f});else if(d.kind==="submission")Q=(0,o.jsx)(Ji,{submissionId:d.submissionId,onBack:f});else{let q=(0,o.jsxs)("div",{className:"af-mb-4 af-flex af-flex-wrap af-items-center af-gap-2",children:[(0,o.jsx)("div",{role:"group","aria-label":"People views",className:"af-inline-flex af-items-center af-gap-1 af-rounded-lg af-bg-muted af-p-1",children:Yi.map(k=>(0,o.jsx)("button",{type:"button","aria-pressed":C===k,onClick:()=>H(k),className:M("af-whitespace-nowrap af-rounded-md af-px-3 af-py-1 af-text-sm af-font-medium af-transition-colors focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",C===k?"af-bg-background af-text-foreground af-shadow":"af-text-muted-foreground hover:af-text-foreground"),children:Xi[k]},k))}),(0,o.jsxs)("div",{className:"af-relative af-w-full af-max-w-xs",children:[(0,o.jsx)(We.Search,{className:"af-pointer-events-none af-absolute af-left-2.5 af-top-1/2 af-h-4 af-w-4 af--translate-y-1/2 af-text-muted-foreground"}),(0,o.jsx)(me,{type:"text",value:h,onChange:k=>R(k.target.value),onBlur:()=>L(h),onKeyDown:k=>{k.key==="Enter"&&(k.preventDefault(),L(h))},placeholder:"Search by name or email","aria-label":"Search people by name or email",className:"af-pl-8"})]}),b&&(0,o.jsx)(w,{type:"button",variant:"ghost",size:"sm",onClick:()=>{R(""),S("")},children:"Clear search"})]});if(r)Q=(0,o.jsxs)("div",{children:[q,(0,o.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(k=>(0,o.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,o.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,o.jsx)(K,{className:"af-h-4 af-w-32"}),(0,o.jsx)(K,{className:"af-h-4 af-w-16"})]}),(0,o.jsx)(K,{className:"af-mt-2 af-h-5 af-w-28"}),(0,o.jsx)(K,{className:"af-mt-2 af-h-3 af-w-56"})]},k))}),(0,o.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,o.jsxs)(_e,{children:[(0,o.jsx)(ke,{children:(0,o.jsxs)(oe,{children:[(0,o.jsx)(I,{children:"Person"}),(0,o.jsx)(I,{children:"Came from"}),(0,o.jsx)(I,{children:"Latest message"}),(0,o.jsx)(I,{children:"Last activity"}),(0,o.jsx)(I,{className:"af-w-8"}),(0,o.jsx)(I,{className:"af-w-8"})]})}),(0,o.jsx)(Ee,{"aria-hidden":"true",children:[0,1,2,3,4].map(k=>(0,o.jsxs)(oe,{children:[(0,o.jsx)(D,{children:(0,o.jsx)(K,{className:"af-h-4 af-w-40"})}),(0,o.jsx)(D,{children:(0,o.jsx)(K,{className:"af-h-5 af-w-24"})}),(0,o.jsx)(D,{children:(0,o.jsx)(K,{className:"af-h-4 af-w-56"})}),(0,o.jsx)(D,{children:(0,o.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,o.jsx)(D,{}),(0,o.jsx)(D,{})]},k))})]})}),(0,o.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading people"})]});else if(c)Q=(0,o.jsxs)("div",{children:[q,(0,o.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,o.jsx)(Tt,{message:"Couldn't load your leads.",onRetry:()=>x()})})]});else if($.length===0&&p===0){let k=!!b||C!=="all";Q=(0,o.jsxs)("div",{children:[k&&q,k?(0,o.jsx)(ia,{children:b?"Nobody matches that search.":C==="customers"?"Nobody has bought anything yet.":"No form submissions yet."}):(0,o.jsx)(ia,{icon:(0,o.jsx)(We.Users,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:"Form submissions and customers will show up here."})]})}else if(t.length===0)Q=(0,o.jsxs)("div",{children:[q,(0,o.jsx)(ia,{children:"No more results."}),(0,o.jsx)("div",{className:"af-flex af-justify-end af-px-2 af-py-4",children:(0,o.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>n(p-1),children:[(0,o.jsx)(We.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]})})]});else{let k=p*g+1,ne=p*g+t.length;Q=(0,o.jsxs)("div",{children:[q,(0,o.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:$.map((m,Y)=>(0,o.jsxs)("div",{onClick:()=>G(m),className:"af-cursor-pointer af-rounded-md af-border af-p-3 hover:af-bg-muted/50",children:[(0,o.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,o.jsxs)("div",{className:"af-min-w-0",children:[(0,o.jsx)("button",{type:"button",onClick:Pe=>{Pe.stopPropagation(),G(m)},"aria-label":`Open ${m.name||m.email||"this form submission"}`,className:"af-block af-max-w-full af-truncate af-rounded-sm af-text-left af-text-sm af-font-medium hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:_n(m)}),m.name&&m.email&&(0,o.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:m.email})]}),(0,o.jsxs)("div",{className:"af-flex af-shrink-0 af-items-center af-gap-1",children:[(0,o.jsx)("span",{className:"af-text-xs af-text-muted-foreground",children:Za(m.last_activity_at)}),m.email&&(0,o.jsx)(w,{asChild:!0,variant:"ghost",size:"icon",className:"af-h-8 af-w-8",children:(0,o.jsx)("a",{href:gr(m.email),onClick:Pe=>Pe.stopPropagation(),"aria-label":`Email ${m.email}`,children:(0,o.jsx)(We.Mail,{className:"af-h-4 af-w-4"})})})]})]}),(0,o.jsx)("div",{className:"af-mt-2",children:Tn(m)}),m.latest_message_excerpt&&(0,o.jsx)("p",{className:"af-mt-2 af-line-clamp-2 af-break-words af-text-xs af-text-muted-foreground",children:m.latest_message_excerpt})]},m.email??m.submission_id??Y))}),(0,o.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,o.jsxs)(_e,{children:[(0,o.jsx)(ke,{children:(0,o.jsxs)(oe,{children:[(0,o.jsx)(I,{children:"Person"}),(0,o.jsx)(I,{children:"Came from"}),(0,o.jsx)(I,{children:"Latest message"}),(0,o.jsx)(I,{children:"Last activity"}),(0,o.jsx)(I,{className:"af-w-8",children:(0,o.jsx)("span",{className:"af-sr-only",children:"Reply"})}),(0,o.jsx)(I,{className:"af-w-8"})]})}),(0,o.jsx)(Ee,{children:$.map((m,Y)=>(0,o.jsxs)(oe,{onClick:()=>G(m),className:"af-cursor-pointer focus-within:af-bg-muted/50",children:[(0,o.jsxs)(D,{className:"af-max-w-[260px] af-font-medium",children:[(0,o.jsx)("button",{type:"button",onClick:Pe=>{Pe.stopPropagation(),G(m)},"aria-label":`Open ${m.name||m.email||"this form submission"}`,className:"af-block af-max-w-full af-truncate af-rounded-sm af-text-left hover:af-underline focus-visible:af-outline-none focus-visible:af-ring-2 focus-visible:af-ring-ring",children:_n(m)}),m.name&&m.email&&(0,o.jsx)("div",{className:"af-truncate af-text-xs af-font-normal af-text-muted-foreground",children:m.email})]}),(0,o.jsx)(D,{children:Tn(m)}),(0,o.jsx)(D,{className:"af-max-w-[320px]",children:m.latest_message_excerpt?(0,o.jsx)("span",{className:"af-line-clamp-1 af-break-words af-text-sm af-text-muted-foreground",children:m.latest_message_excerpt}):(0,o.jsx)("span",{className:"af-text-muted-foreground",children:"\u2014"})}),(0,o.jsx)(D,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Za(m.last_activity_at)}),(0,o.jsx)(D,{className:"af-w-8",children:m.email&&(0,o.jsx)(w,{asChild:!0,variant:"ghost",size:"icon",className:"af-h-8 af-w-8",children:(0,o.jsx)("a",{href:gr(m.email),onClick:Pe=>Pe.stopPropagation(),"aria-label":`Email ${m.email}`,children:(0,o.jsx)(We.Mail,{className:"af-h-4 af-w-4"})})})}),(0,o.jsx)(D,{className:"af-w-8 af-text-right",children:(0,o.jsx)(We.ChevronRight,{"aria-hidden":"true",className:"af-h-4 af-w-4 af-text-muted-foreground/50"})})]},m.email??m.submission_id??Y))})]})}),(0,o.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,o.jsx)("span",{className:"af-text-sm af-text-muted-foreground",children:C==="all"?`Showing ${k}-${ne}`:`${$.length} of ${t.length} on this page`}),(0,o.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,o.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>n(p-1),disabled:!y,children:[(0,o.jsx)(We.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,o.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>n(p+1),disabled:!v,children:["Next",(0,o.jsx)(We.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}}return(0,o.jsx)(Ct,{title:"Leads",onBackToWebsite:a,hideBackButton:d.kind!=="list",className:s,children:Q})}var za=require("lucide-react");var xt=se(require("react"),1);function Rs({initialPageSize:e=25,refreshKey:a}={}){let{api:s}=te(),[t,r]=xt.useState([]),[c,p]=xt.useState(0),[g,v]=xt.useState(!0),[y,n]=xt.useState(null),[b,S]=xt.useState(0),[x,d]=xt.useState(e),[u,h]=xt.useState(void 0),R=xt.useRef(0),C=xt.useCallback(async()=>{let G=++R.current;try{v(!0),n(null);let f={limit:x,offset:b*x};u&&(f.status=u);let L=await s.getSubscriptions(f);if(G!==R.current)return;r(L.subscriptions),p(L.total)}catch(f){if(G!==R.current)return;n(f instanceof Error?f:new Error("Failed to fetch subscriptions"))}finally{G===R.current&&v(!1)}},[s,b,x,u,a]);xt.useEffect(()=>{C()},[C]);let H=Math.ceil(c/x);return{subscriptions:t,total:c,isLoading:g,error:y,page:b,pageSize:x,hasNextPage:b<H-1,hasPreviousPage:b>0,setPage:S,setPageSize:G=>{d(G),S(0)},status:u,setStatus:G=>{h(G),S(0)},refetch:C}}var E=require("react/jsx-runtime");function En(e){let a=e.includes("T")?e:`${e.replace(" ","T")}Z`;return new Date(a).toLocaleString(void 0,{month:"short",day:"numeric",year:"numeric"})}function In({sub:e}){return e.status==="canceled"?(0,E.jsx)(W,{variant:"outline",children:"Canceled"}):e.status==="past_due"?(0,E.jsx)(W,{variant:"destructive",children:"Past due"}):e.status==="active"?e.cancel_at_period_end?(0,E.jsx)(W,{variant:"warning",children:"Scheduled to cancel"}):(0,E.jsx)(W,{variant:"success",children:"Active"}):(0,E.jsx)(W,{variant:"outline",title:e.status,children:"Managed in Stripe"})}function to(e,a){let s=z(a,e.currency);return e.plan_interval_count===1?`${s} / ${e.plan_interval}`:`${s} every ${e.plan_interval_count} ${e.plan_interval}s`}function Dn({sub:e}){let a=e.current_amount!==e.plan_amount;return(0,E.jsxs)("div",{children:[(0,E.jsx)("div",{className:"af-font-medium af-tabular-nums",children:to(e,e.current_amount)}),a&&(0,E.jsxs)("div",{className:"af-text-xs af-text-muted-foreground",children:["signed up at ",z(e.plan_amount,e.currency)]})]})}function Ln(e){return e.status==="canceled"?"\u2014":e.cancel_at_period_end?e.current_period_end?`Ends ${En(e.current_period_end)}`:"Ends at period end":e.current_period_end?En(e.current_period_end):"\u2014"}function br({refreshKey:e,onBackToWebsite:a,className:s}){let{subscriptions:t,total:r,isLoading:c,error:p,page:g,pageSize:v,hasNextPage:y,hasPreviousPage:n,setPage:b,refetch:S}=Rs({refreshKey:e}),x;if(c)x=(0,E.jsxs)("div",{children:[(0,E.jsx)("div",{className:"af-space-y-3 lg:af-hidden","aria-hidden":"true",children:[0,1,2,3,4].map(d=>(0,E.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,E.jsxs)("div",{className:"af-flex af-items-center af-justify-between",children:[(0,E.jsx)(K,{className:"af-h-4 af-w-40"}),(0,E.jsx)(K,{className:"af-h-4 af-w-20"})]}),(0,E.jsx)(K,{className:"af-mt-2 af-h-3 af-w-32"}),(0,E.jsx)(K,{className:"af-mt-2 af-h-5 af-w-24"})]},d))}),(0,E.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,E.jsxs)(_e,{children:[(0,E.jsx)(ke,{children:(0,E.jsxs)(oe,{children:[(0,E.jsx)(I,{children:"Subscriber"}),(0,E.jsx)(I,{children:"Plan"}),(0,E.jsx)(I,{className:"af-text-right",children:"Amount"}),(0,E.jsx)(I,{children:"Renews"}),(0,E.jsx)(I,{children:"Status"})]})}),(0,E.jsx)(Ee,{"aria-hidden":"true",children:[0,1,2,3,4].map(d=>(0,E.jsxs)(oe,{children:[(0,E.jsx)(D,{children:(0,E.jsx)(K,{className:"af-h-4 af-w-44"})}),(0,E.jsx)(D,{children:(0,E.jsx)(K,{className:"af-h-4 af-w-32"})}),(0,E.jsx)(D,{className:"af-text-right",children:(0,E.jsx)(K,{className:"af-ml-auto af-h-4 af-w-24"})}),(0,E.jsx)(D,{children:(0,E.jsx)(K,{className:"af-h-4 af-w-24"})}),(0,E.jsx)(D,{children:(0,E.jsx)(K,{className:"af-h-5 af-w-20"})})]},d))})]})}),(0,E.jsx)("span",{className:"af-sr-only",role:"status",children:"Loading subscriptions"})]});else if(p)x=(0,E.jsx)("div",{className:"af-flex af-justify-center af-py-8",children:(0,E.jsx)(Tt,{message:"Couldn't load your subscriptions.",onRetry:()=>S()})});else if(t.length===0&&g===0)x=(0,E.jsx)(ia,{icon:(0,E.jsx)(za.Repeat,{className:"af-mb-4 af-h-12 af-w-12 af-text-muted-foreground"}),children:"Subscriptions appear here when a customer subscribes to one of your products."});else if(t.length===0)x=(0,E.jsxs)("div",{children:[(0,E.jsx)(ia,{children:"No more results."}),(0,E.jsx)("div",{className:"af-flex af-justify-end af-px-2 af-py-4",children:(0,E.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>b(g-1),children:[(0,E.jsx)(za.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]})})]});else{let d=g*v+1,u=g*v+t.length;x=(0,E.jsxs)("div",{children:[(0,E.jsx)("div",{className:"af-space-y-3 lg:af-hidden",children:t.map(h=>(0,E.jsxs)("div",{className:"af-rounded-md af-border af-p-3",children:[(0,E.jsxs)("div",{className:"af-flex af-items-start af-justify-between af-gap-2",children:[(0,E.jsxs)("div",{className:"af-min-w-0",children:[(0,E.jsx)("div",{className:"af-truncate af-text-sm af-font-medium",children:h.customer_email||(0,E.jsx)("span",{className:"af-text-muted-foreground af-italic",children:"No email"})}),(0,E.jsx)("div",{className:"af-truncate af-text-xs af-text-muted-foreground",children:h.plan_title||"Untitled plan"})]}),(0,E.jsx)("div",{className:"af-shrink-0 af-text-right",children:(0,E.jsx)(Dn,{sub:h})})]}),(0,E.jsxs)("div",{className:"af-mt-2 af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,E.jsx)(In,{sub:h}),h.is_test&&(0,E.jsx)(W,{variant:"outline",children:"Test"}),(0,E.jsx)("span",{className:"af-ml-auto af-text-xs af-text-muted-foreground",children:Ln(h)})]})]},h.id))}),(0,E.jsx)("div",{className:"af-hidden af-overflow-x-auto lg:af-block",children:(0,E.jsxs)(_e,{children:[(0,E.jsx)(ke,{children:(0,E.jsxs)(oe,{children:[(0,E.jsx)(I,{children:"Subscriber"}),(0,E.jsx)(I,{children:"Plan"}),(0,E.jsx)(I,{className:"af-text-right",children:"Amount"}),(0,E.jsx)(I,{children:"Renews"}),(0,E.jsx)(I,{children:"Status"})]})}),(0,E.jsx)(Ee,{children:t.map(h=>(0,E.jsxs)(oe,{children:[(0,E.jsx)(D,{className:"af-max-w-[240px]",children:(0,E.jsx)("div",{className:"af-truncate af-font-medium",children:h.customer_email||(0,E.jsx)("span",{className:"af-italic af-text-muted-foreground",children:"No email"})})}),(0,E.jsx)(D,{className:"af-max-w-[220px]",children:(0,E.jsx)("div",{className:"af-truncate",children:h.plan_title||(0,E.jsx)("span",{className:"af-text-muted-foreground",children:"Untitled plan"})})}),(0,E.jsx)(D,{className:"af-whitespace-nowrap af-text-right",children:(0,E.jsx)(Dn,{sub:h})}),(0,E.jsx)(D,{className:"af-whitespace-nowrap af-text-muted-foreground",children:Ln(h)}),(0,E.jsx)(D,{children:(0,E.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-gap-1",children:[(0,E.jsx)(In,{sub:h}),h.is_test&&(0,E.jsx)(W,{variant:"outline",children:"Test"})]})})]},h.id))})]})}),(0,E.jsxs)("div",{className:"af-flex af-flex-wrap af-items-center af-justify-between af-gap-2 af-px-2 af-py-4",children:[(0,E.jsx)("span",{className:"af-text-sm af-text-muted-foreground",children:`Showing ${d}-${u} of ${r}`}),(0,E.jsxs)("div",{className:"af-flex af-items-center af-gap-2",children:[(0,E.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>b(g-1),disabled:!n,children:[(0,E.jsx)(za.ChevronLeft,{className:"af-h-4 af-w-4 af-mr-1"}),"Previous"]}),(0,E.jsxs)(w,{variant:"outline",size:"sm",onClick:()=>b(g+1),disabled:!y,children:["Next",(0,E.jsx)(za.ChevronRight,{className:"af-h-4 af-w-4 af-ml-1"})]})]})]})]})}return(0,E.jsx)(Ct,{title:"Subscriptions",onBackToWebsite:a,className:s,children:x})}var yr=se(require("react"),1);var ze=require("react/jsx-runtime");function hr({refreshKey:e,initialInvoiceId:a,onBackToWebsite:s,onConnectStripe:t,onUpgradePlan:r,needsPaidPlan:c,className:p}){let{aggregates:g,isLoading:v,error:y,refetch:n}=Fa({refreshKey:e,initialPageSize:1,includeAggregates:!0}),b=Ma({refreshKey:e}),[S,x]=yr.useState(!1),[d,u]=yr.useState(null),h=(0,ze.jsx)("div",{className:"af-mb-6 af-grid af-grid-cols-1 af-gap-3 sm:af-grid-cols-3",children:y||!v&&!g?(0,ze.jsxs)("div",{role:"alert",className:"sm:af-col-span-3 af-rounded-md af-border af-p-3 af-text-sm",children:["Couldn't load invoice totals. ",(0,ze.jsx)(w,{type:"button",variant:"link",size:"sm",onClick:()=>void n(),children:"Retry totals"})]}):v||!g?(0,ze.jsxs)(ze.Fragment,{children:[(0,ze.jsx)(Ve,{label:"Outstanding",children:(0,ze.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,ze.jsx)(Ve,{label:"Overdue",children:(0,ze.jsx)(K,{className:"af-h-6 af-w-24"})}),(0,ze.jsx)(Ve,{label:"Paid this month",children:(0,ze.jsx)(K,{className:"af-h-6 af-w-24"})})]}):(0,ze.jsxs)(ze.Fragment,{children:[(0,ze.jsx)(Ve,{label:"Outstanding",detail:`${g.outstanding.count} open`,children:z(g.outstanding.amount,g.currency)}),(0,ze.jsx)(Ve,{label:"Overdue",detail:g.overdue.count===0?"Nothing overdue":`${g.overdue.count} overdue`,valueClassName:g.overdue.count>0?"af-text-lg af-font-semibold af-text-destructive":void 0,children:z(g.overdue.amount,g.currency)}),(0,ze.jsx)(Ve,{label:"Paid this month",detail:`${g.paid_this_month.count} paid`,children:z(g.paid_this_month.amount,g.currency)})]})});return(0,ze.jsxs)(Ct,{title:"Invoices",onBackToWebsite:s,hideBackButton:S,className:p,children:[h,!S&&(0,ze.jsx)($a,{retainers:b.retainers,isLoading:b.isLoading,error:b.error,onRetry:()=>void b.refetch(),activeRetainerId:d?.id??null,onShowCycles:R=>u(C=>C?.id===R.id?null:R),total:b.total,endedTotal:b.endedTotal,isLoadingMore:b.isLoadingMore,onLoadMore:()=>void b.loadMore(),includeEnded:b.includeEnded,onIncludeEndedChange:b.setIncludeEnded}),(0,ze.jsx)(Ua,{refreshKey:e,initialInvoiceId:a,onDetailOpenChange:x,onConnectStripe:t,onUpgradePlan:r,needsPaidPlan:c,retainerFilter:d?{id:d.id,label:`${d.customer_name??d.customer_email??"Recurring invoices"} \u2014 ${d.description}`}:null,onClearRetainerFilter:()=>u(null)})]})}0&&(module.exports={AifeaturesApiError,AifeaturesProvider,CustomerDetail,CustomersList,FormSettings,FormSubmissions,FormsDashboard,FormsList,InvoiceDetail,InvoiceLabelBadge,InvoicesList,InvoicesView,LeadsView,OrderDetail,OrderStatusBadge,OrdersList,OrdersView,PaymentsDashboard,PaymentsSetup,ProductSettings,ProductsList,ProductsView,RetainerStatusBadge,RetainersPanel,SubmissionDetail,SubscriptionsView,VariantsEditor,ViewShell,WebhookDeliveries,WebhookSettings,chargedTotal,createApiClient,formatPrice,isZeroDecimalCurrency,toMajorUnits,toMinorUnits,useAifeatures,useAifeaturesContext,useBusinessSummary,useCustomers,useEventDestinations,useForms,useInvoices,useOrders,usePaymentsStatus,usePeople,usePerson,useProducts,useRetainers,useSubmissions,useSubscriptions});
|