1import{r as g,t as M,b as O,j as e,C as D,a as F,B as v,J as B,N as _,O as H,Q as V,aF as J,i as P}from"./index-Cuo-9n-l.js";import{A as Q,U as W}from"./AdminShell-BMA_HtRl.js";import{T as z,a as K,b as $,c as L,d as q,e as E}from"./table-DDWdJSdx.js";import{D as G}from"./download-BnexdOqz.js";import{C as A}from"./circle-check-big-LWtlJXpZ.js";import{F as X}from"./file-down-D5ekK0FR.js";import"./loader-circle-Bdw2r9pR.js";import"./user-cog-BjrmXamg.js";import"./ticket-CxOMttNO.js";import"./tag-D_pnE_be.js";import"./store-CywTQdce.js";import"./star-DuTtmimG.js";import"./smartphone-Bo9kZIHZ.js";import"./mic-4_NYEjFz.js";import"./message-square-FZyxqP8G.js";import"./megaphone-cS6_-2F5.js";import"./graduation-cap-unFDRdPM.js";import"./file-text-DMYGicGj.js";import"./calendar-days-DFJTkOlI.js";import"./building-2-IlJsoHAX.js";function I(t){const i=[];let n="",a=!1;for(let s=0;s<t.length;s++){const c=t[s];c==='"'?a&&t[s+1]==='"'?(n+='"',s++):a=!a:c===","&&!a?(i.push(n.trim()),n=""):n+=c}return i.push(n.trim()),i}function Y(t){const i=t.trim().split(/\r?\n/).filter(a=>a.trim());if(i.length<2)return[];const n=I(i[0]).map(a=>a.toLowerCase().trim());return i.slice(1).map(a=>{const s=I(a),c={};return n.forEach((N,x)=>{c[N]=s[x]??""}),c})}function Z(t){const i=t["email address"]||t.email||t["e-mail"]||"";if(!i.includes("@"))return null;const n=t["first name"]||t.firstname||t.first_name||t.fname||void 0,a=t["last name"]||t.lastname||t.last_name||t.lname||void 0,s=t.status||t.optin_status||t["subscription status"]||"subscribed";return{email:i,firstName:n||void 0,lastName:a||void 0,status:s}}function ee(){const t=`email,first_name,last_name,status 2[email protected],Jane,Smith,subscribed 3[email protected],John,Doe,unsubscribed 4`,i=new Blob([t],{type:"text/csv;charset=utf-8;"}),n=URL.createObjectURL(i),a=document.createElement("a");a.href=n,a.download="newsletter-import-template.csv",a.click(),URL.revokeObjectURL(n)}function se(t,i){const n=new Set(t.map(o=>o.split(":")[0].trim())),a=i.filter(o=>!(o["email address"]||o.email||"").includes("@")),s=i.filter(o=>{const m=o["email address"]||o.email||"";return n.has(m)}),c=[...a,...s];if(c.length===0){const o=["email,error",...t.map(j=>{const[r,...d]=j.split(":");return`"${r.trim()}","${d.join(":").trim()}"`})],m=new Blob([o.join(` 5`)],{type:"text/csv;charset=utf-8;"}),l=URL.createObjectURL(m),b=document.createElement("a");b.href=l,b.download="subscriber-import-errors.csv",b.click(),URL.revokeObjectURL(l);return}const N=Object.keys(c[0]),x=new Map(t.map(o=>{const[m,...l]=o.split(":");return[m.trim(),l.join(":").trim()]})),k=[...N,"import_error"],w=c.map(o=>{const m=N.map(b=>{const j=o[b]??"";return j.includes(",")||j.includes('"')?`"${j.replace(/"/g,'""')}"`:j}),l=o["email address"]||o.email||"";return m.push(`"${x.get(l)??"Missing or invalid email"}"`),m.join(",")}),R=[k.join(","),...w].join(` 6`),h=new Blob([R],{type:"text/csv;charset=utf-8;"}),f=URL.createObjectURL(h),y=document.createElement("a");y.href=f,y.download="subscriber-import-errors.csv",y.click(),URL.revokeObjectURL(f)}function we(){const[t,i]=g.useState([]),[n,a]=g.useState(""),[s,c]=g.useState(null),[N,x]=g.useState(!1),[k,w]=g.useState(!1),R=g.useRef(null),[h,f]=g.useState(null),y=M.newsletter.importSubscribers.useMutation({onError:r=>{w(!1),f(null),O.error(r.message)}});function o(r){const d=r.target.files?.[0];if(!d)return;a(d.name),c(null);const C=new FileReader;C.onload=S=>{const U=S.target?.result;i(Y(U))},C.readAsText(d)}const l=t.map(Z).filter(Boolean).filter(r=>r!==null),b=t.length-l.length;async function j(){if(!l.length)return;w(!0);const r=1e3,d=[];for(let u=0;u<l.length;u+=r)d.push(l.slice(u,u+r));let C=0,S=0,U=0;const T=[];f({done:0,total:l.length});for(let u=0;u<d.length;u++)try{const p=await y.mutateAsync({rows:d[u]});C+=p.created,S+=p.updated,U+=p.skipped,p.errors?.length&&T.push(...p.errors),f({done:Math.min((u+1)*r,l.length),total:l.length})}catch(p){T.push(`Chunk ${u+1} failed: ${p instanceof Error?p.message:String(p)}`)}c({created:C,updated:S,skipped:U,errors:T}),w(!1),f(null),x(!0)}return e.jsxs(Q,{title:"Import Newsletter Subscribers",children:[e.jsx(D,{className:"border-border/70 mb-6",children:e.jsxs(F,{className:"p-6 space-y-4",children:[e.jsx("div",{className:"eyebrow",children:"How to import"}),e.jsxs("ol",{className:"text-sm space-y-2 list-decimal list-inside text-muted-foreground",children:[e.jsx("li",{children:"Download the template below or export directly from Mailchimp as CSV."}),e.jsxs("li",{children:["The importer automatically recognizes Mailchimp column names (",e.jsx("code",{children:"Email Address"}),", ",e.jsx("code",{children:"First Name"}),", ",e.jsx("code",{children:"Last Name"}),", ",e.jsx("code",{children:"Status"}),")."]}),e.jsxs("li",{children:["For ",e.jsx("strong",{children:"status"}),": ",e.jsx("code",{children:"subscribed"})," = active, anything else (unsubscribed, cleaned, pending) = inactive."]}),e.jsxs("li",{children:["Existing subscribers (matched by email) will be ",e.jsx("strong",{children:"updated"}),". New emails will be ",e.jsx("strong",{children:"created"}),"."]}),e.jsx("li",{children:"Unsubscribed contacts are imported as inactive â they won't receive blasts."})]}),e.jsxs(v,{variant:"outline",onClick:ee,children:[e.jsx(G,{className:"h-4 w-4 mr-2"})," Download CSV Template"]})]})}),e.jsx(D,{className:"border-border/70 mb-6",children:e.jsxs(F,{className:"p-6",children:[e.jsx("div",{className:"eyebrow mb-4",children:"Upload CSV"}),e.jsxs("div",{className:"flex items-center gap-4 flex-wrap",children:[e.jsx("input",{ref:R,type:"file",accept:".csv,text/csv",className:"hidden",onChange:o}),e.jsxs(v,{variant:"outline",onClick:()=>R.current?.click(),children:[e.jsx(W,{className:"h-4 w-4 mr-2"})," Choose CSV file"]}),n&&e.jsx("span",{className:"text-sm text-muted-foreground",children:n})]}),t.length>0&&e.jsxs("div",{className:"mt-4 space-y-2",children:[e.jsxs("div",{className:"text-sm",children:[e.jsx("span",{className:"font-medium",children:t.length})," rows found ·"," ",e.jsxs("span",{className:"text-emerald-600 font-medium",children:[l.length," valid"]}),b>0&&e.jsxs("span",{className:"text-destructive font-medium",children:[" · ",b," skipped (missing/invalid email)"]}),l.filter(r=>r.status&&r.status.toLowerCase()!=="subscribed").length>0&&e.jsxs("span",{className:"text-amber-600 font-medium",children:[" · ",l.filter(r=>r.status&&r.status.toLowerCase()!=="subscribed").length," unsubscribed (will import as inactive)"]})]}),h&&e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"text-sm text-muted-foreground",children:["Importing⦠",h.done," / ",h.total]}),e.jsx("div",{className:"w-full h-2 bg-muted rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-[var(--color-navy-600)] rounded-full transition-all",style:{width:`${h.done/h.total*100}%`}})})]}),!h&&e.jsx(v,{className:"btn-brand border-0",disabled:k||l.length===0,onClick:j,children:k?"Importingâ¦":`Import ${l.length} subscriber${l.length!==1?"s":""}`})]})]})}),e.jsx(B,{open:N,onOpenChange:x,children:e.jsxs(_,{className:"max-w-md",children:[e.jsx(H,{children:e.jsxs(V,{className:"flex items-center gap-2",children:[e.jsx(A,{className:"h-5 w-5 text-emerald-600"})," Import Complete"]})}),s&&e.jsxs("div",{className:"space-y-5",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-3 text-center",children:[e.jsxs("div",{className:"rounded-lg bg-emerald-50 border border-emerald-200 p-3",children:[e.jsx("div",{className:"text-2xl font-display text-emerald-700",children:s.created}),e.jsx("div",{className:"text-xs text-emerald-700 mt-0.5 font-medium",children:"Added"})]}),e.jsxs("div",{className:"rounded-lg bg-[var(--color-steel-50)] border border-border/70 p-3",children:[e.jsx("div",{className:"text-2xl font-display text-[var(--color-navy-700)]",children:s.updated}),e.jsx("div",{className:"text-xs text-muted-foreground mt-0.5",children:"Updated"})]}),e.jsxs("div",{className:"rounded-lg bg-muted/40 border border-border/70 p-3",children:[e.jsx("div",{className:"text-2xl font-display text-muted-foreground",children:s.skipped+s.errors.length}),e.jsx("div",{className:"text-xs text-muted-foreground mt-0.5",children:"Skipped"})]})]}),s.errors.length===0&&s.skipped===0&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-emerald-700 bg-emerald-50 rounded-lg px-3 py-2.5",children:[e.jsx(A,{className:"h-4 w-4 shrink-0"}),"All subscribers imported successfully."]}),(s.errors.length>0||s.skipped>0)&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start gap-2 text-sm bg-amber-50 border border-amber-200 rounded-lg px-3 py-2.5",children:[e.jsx(J,{className:"h-4 w-4 text-amber-600 shrink-0 mt-0.5"}),e.jsxs("div",{children:[e.jsxs("div",{className:"font-medium text-amber-800",children:[s.errors.length+s.skipped," row",s.errors.length+s.skipped!==1?"s":""," could not be imported"]}),e.jsxs("div",{className:"text-amber-700 text-xs mt-0.5",children:[s.errors.length>0&&`${s.errors.length} had errors. `,s.skipped>0&&`${s.skipped} were skipped (missing or invalid email).`]})]})]}),s.errors.length>0&&e.jsxs("div",{className:"max-h-32 overflow-y-auto rounded border border-border/70 bg-muted/30 p-3",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground mb-1.5",children:"Error details:"}),e.jsxs("ul",{className:"text-xs text-destructive space-y-1",children:[s.errors.slice(0,20).map((r,d)=>e.jsx("li",{children:r},d)),s.errors.length>20&&e.jsxs("li",{className:"text-muted-foreground",children:["â¦and ",s.errors.length-20," more"]})]})]}),e.jsxs(v,{variant:"outline",className:"w-full",onClick:()=>se(s.errors,t),children:[e.jsx(X,{className:"h-4 w-4 mr-2"})," Download Failed Rows as CSV"]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["The downloaded file contains the original row data plus an ",e.jsx("code",{children:"import_error"})," column. Correct the issues and re-upload."]})]}),e.jsxs("div",{className:"flex gap-3 pt-1",children:[e.jsxs(v,{className:"btn-brand border-0 flex-1",onClick:()=>{x(!1),i([]),c(null),a("")},children:[e.jsx(P,{className:"h-4 w-4 mr-2"})," Done"]}),(s.errors.length>0||s.skipped>0)&&e.jsx(v,{variant:"outline",onClick:()=>x(!1),children:"Upload another file"})]})]})]})}),t.length>0&&!s&&e.jsx(D,{className:"border-border/70",children:e.jsxs(F,{className:"p-0 overflow-x-auto",children:[e.jsx("div",{className:"p-4 border-b border-border/60 text-sm font-medium",children:"Preview (first 5 rows)"}),e.jsxs(z,{children:[e.jsx(K,{children:e.jsxs($,{children:[e.jsx(L,{children:"Email"}),e.jsx(L,{children:"First Name"}),e.jsx(L,{children:"Last Name"}),e.jsx(L,{children:"Status"})]})}),e.jsx(q,{children:l.slice(0,5).map((r,d)=>e.jsxs($,{children:[e.jsx(E,{className:"text-xs",children:r.email}),e.jsx(E,{className:"text-xs",children:r.firstName||"â"}),e.jsx(E,{className:"text-xs",children:r.lastName||"â"}),e.jsx(E,{className:"text-xs",children:e.jsx("span",{className:r.status?.toLowerCase()==="subscribed"||!r.status?"text-emerald-600 font-medium":"text-amber-600",children:r.status||"subscribed"})})]},d))})]})]})})]})}export{we as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.