@aifeatures/admin-react 0.5.3 → 0.5.4

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 CHANGED
@@ -1,4 +1,4 @@
1
1
  "use client";
2
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 oi={};rn(oi,{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(oi);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");function V(...e){return(0,tr.twMerge)((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"),ln=(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(ln({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 fn=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}));fn.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 cn=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}));cn.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 dn=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}));dn.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 un=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}));un.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 mn(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:["(",mn(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 pn(e){return new Date(e).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function gn(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 vn(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=gn(D.data),z=vn(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:pn(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"),bn=(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(bn(),e),...t}));le.displayName="Label";var gr=require("class-variance-authority");var vr=require("react/jsx-runtime"),yn=(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(yn({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 hn=$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)(hn,{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 xn=new Set(["bif","clp","djf","gnf","jpy","kmf","krw","mga","pyg","rwf","vnd","vuv","xaf","xof","xpf"]);function Lt(e){return xn.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 Nn(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 wn(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:Nn(p)}),(0,X.jsx)(B,{className:"af-text-muted-foreground",children:wn(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 Sn(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:["(",Sn(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 Pn=["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 Pn.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"),Rn={pending:"Pending",paid:"Paid",failed:"Failed",expired:"Expired",refunded:"Refunded",partially_refunded:"Partially refunded",disputed:"Disputed"};function Cn(e){return Rn[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:Cn(e)})}function ua({status:e}){return(0,Qa.jsx)(ya,{status:e})}function _a({order:e,className:t}){return e.shipping_address?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 Tn(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,kn=["paid","partially_refunded"],_n=["paid","partially_refunded","disputed"],Dn=/^[A-Za-z0-9 _-]*$/,Sr=100;function Is(e,t,a,s,r){let l=kn.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=Tn(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 En(){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(!En())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 On({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 Ln(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.shipping_address,Ee=typeof n.merchant_fulfilled_at=="string",ia=_n.includes(n.status),Ht=Q.trim(),Ta=Dn.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=Ln(n),Zr=n.shipping_address?Rr(n.shipping_address).join(`
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:["(",mn(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 pn(e){return new Date(e).toLocaleString(void 0,{month:"short",day:"numeric",hour:"2-digit",minute:"2-digit"})}function gn(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 vn(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=gn(D.data),z=vn(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:pn(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"),bn=(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(bn(),e),...t}));le.displayName="Label";var gr=require("class-variance-authority");var vr=require("react/jsx-runtime"),yn=(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(yn({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 hn=$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)(hn,{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 xn=new Set(["bif","clp","djf","gnf","jpy","kmf","krw","mga","pyg","rwf","vnd","vuv","xaf","xof","xpf"]);function Lt(e){return xn.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 Nn(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 wn(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:Nn(p)}),(0,X.jsx)(B,{className:"af-text-muted-foreground",children:wn(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 Sn(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:["(",Sn(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 Pn=["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 Pn.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"),Rn={pending:"Pending",paid:"Paid",failed:"Failed",expired:"Expired",refunded:"Refunded",partially_refunded:"Partially refunded",disputed:"Disputed"};function Cn(e){return Rn[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:Cn(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 Tn(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,kn=["paid","partially_refunded"],_n=["paid","partially_refunded","disputed"],Dn=/^[A-Za-z0-9 _-]*$/,Sr=100;function Is(e,t,a,s,r){let l=kn.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=Tn(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 En(){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(!En())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 On({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 Ln(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=_n.includes(n.status),Ht=Q.trim(),Ta=Dn.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=Ln(n),Zr=n.shipping_address?Rr(n.shipping_address).join(`
4
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-wrap 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-[200px]","aria-label":"Tracking number (optional)",disabled:A}),(0,o.jsxs)(N,{variant:"default",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)(On,{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"),Bn={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:Bn[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 In(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"}),In(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 An(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:An(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",Fn=3;function Un(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=Un(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<=Fn&&(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"),Vn={all:"All",active:"Active",draft:"Draft",low_stock:"Low stock",out_of_stock:"Out of stock"},Hn=["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 $n(e){return e.inventory?.out_of_stock??!1}function Mn(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 $n(e);default:return!0}}function zn({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 jn(){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=>Mn(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:Hn.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:Vn[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)(jn,{}),(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)(zn,{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",qn="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 Wn(){return typeof navigator<"u"&&!!navigator.clipboard?.writeText}function Gn({value:e,label:t}){let[a,s]=It.useState(!1);return Wn()?(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 Kn=/^(message|messages|comment|comments|enquiry|inquiry|question|questions|details|notes|note|body)$/i,Jn=40;function Zn(e){let t=Object.entries(e).filter(([r])=>!r.startsWith("_")),s=t.find(([r,l])=>Kn.test(r)&&typeof l=="string"&&l.trim())??t.filter(([,r])=>typeof r=="string"&&r.trim().length>=Jn).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 Qn(e){return e.replace(/_/g," ")}function Yn(e){if(typeof e=="string")return e;if(e==null)return"";try{return JSON.stringify(e)}catch{return String(e)}}function Xn({data:e}){let{message:t,rest:a}=Zn(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(qn,"af-w-28 af-shrink-0 af-capitalize"),children:Qn(s)}),(0,i.jsx)("dd",{className:"af-min-w-0 af-break-words af-text-sm",children:Yn(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)(Xn,{data:a})]})}function ei(e){return e.charged_total??e.amount_total}function ti({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)(Gn,{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(ei(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 ai({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 si={all:"All",customers:"Customers",leads:"Leads"},ri=["all","customers","leads"];function ni(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=>ni(j,T)),Q;if(f.kind==="person")Q=(0,i.jsx)(ti,{email:f.email,refreshKey:e,onBack:D});else if(f.kind==="submission")Q=(0,i.jsx)(ai,{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:ri.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:si[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 ii(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:ii(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});