1import{r as o,J as k,j as e,a9 as D,aa as E,ab as C,ac as q,bZ as B,aW as R,D as T,L as h,S as M,o as $,p as L,q as U,s as c,I as _,a4 as F,ad as Q,B as j,l as V,dp as n,w as v}from"./index-Cz3wH2jj.js";function J({open:f,onOpenChange:d,productIds:a,onComplete:u}){const[i,S]=o.useState("individual"),[r,b]=o.useState("2"),[p,m]=o.useState(!1),[x,w]=o.useState(!0),{companyUuids:N}=k(),A=async()=>{if(a.length===0){n({title:"No products selected",variant:"destructive"});return}if(i==="custom"&&(!r||parseInt(r,10)<1)){n({title:"Invalid quantity",description:"Enter a number ⥠1.",variant:"destructive"});return}m(!0);try{const s={auto_split_mode:i,split_quantity:i==="custom"?Math.max(1,parseInt(r,10)||1):null};let g=0;for(let t=0;t<a.length;t+=200){const l=a.slice(t,t+200),{error:y}=await v.from("products").update(s).in("id",l);if(y)throw y;g+=l.length}if(n({title:"Auto-split updated",description:`Applied "${i}" mode${i==="custom"?` (${r} per shipment)`:""} to ${g} product(s).`}),x&&a.length>0)try{const{data:t,error:l}=await v.functions.invoke("apply-shipping-rules",{body:{applyToAllAwaiting:!0,productIds:a,companyIds:N,queue:!0}});if(l)throw new Error(l.message);if(t!=null&&t.error)throw new Error(t.error);n({title:"Reprocessing queued",description:"Awaiting Shipment orders containing these products are being reprocessed in the background."})}catch(t){n({title:"Reprocessing failed to queue",description:t instanceof Error?t.message:String(t),variant:"destructive"})}d(!1),u==null||u()}catch(s){n({title:"Update failed",description:s instanceof Error?s.message:String(s),variant:"destructive"})}finally{m(!1)}};return e.jsx(D,{open:f,onOpenChange:d,children:e.jsxs(E,{className:"max-w-md",children:[e.jsxs(C,{children:[e.jsxs(q,{className:"flex items-center gap-2",children:[e.jsx(B,{className:"h-5 w-5 text-primary"}),"Bulk Auto-Split"]}),e.jsxs(R,{children:["Apply the same auto-split behavior to"," ",e.jsx(T,{variant:"secondary",children:a.length})," selected product",a.length===1?"":"s",". Shipping rules can override this per-order."]})]}),e.jsxs("div",{className:"space-y-4 py-2",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(h,{className:"text-xs font-medium text-muted-foreground",children:"Split Mode"}),e.jsxs(M,{value:i,onValueChange:s=>S(s),children:[e.jsx($,{className:"h-10",children:e.jsx(L,{})}),e.jsxs(U,{children:[e.jsx(c,{value:"none",children:"Do not auto-split"}),e.jsx(c,{value:"bulk",children:"Exclusive Bulk Shipping"}),e.jsx(c,{value:"individual",children:"Ships Individually (1 per shipment)"}),e.jsx(c,{value:"custom",children:"Ship on Custom Quantity"})]})]})]}),i==="custom"&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(h,{className:"text-xs font-medium text-muted-foreground",children:"Units per Shipment"}),e.jsx(_,{type:"number",min:1,step:1,value:r,onChange:s=>b(s.target.value),placeholder:"e.g. 2",className:"h-10"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Each shipment will contain this many units of the product."})]}),e.jsxs("div",{className:"flex items-start gap-2 rounded-md border border-border bg-muted/30 p-3",children:[e.jsx(F,{id:"bulk-apply-awaiting",checked:x,onCheckedChange:s=>w(s===!0)}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(h,{htmlFor:"bulk-apply-awaiting",className:"text-sm font-medium cursor-pointer",children:"Apply to all Awaiting Shipment orders"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Re-runs Shipping Rules and product auto-split on every Awaiting Shipment order containing the selected products."})]})]})]}),e.jsxs(Q,{children:[e.jsx(j,{variant:"outline",onClick:()=>d(!1),disabled:p,children:"Cancel"}),e.jsxs(j,{onClick:A,disabled:p,children:[p&&e.jsx(V,{className:"mr-2 h-4 w-4 animate-spin"}),"Apply to ",a.length," product",a.length===1?"":"s"]})]})]})})}export{J as BulkAutoSplitDialog};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.