1import{u as Z,j as e,B as D,s as v}from"./index-B8sBaXQd.js";import{r as l}from"./vendor-recharts-PDzeV0Q9.js";import{A as ee}from"./AdminShell-Daupe0PS.js";import{C as p,b as C,c as S,a as u}from"./card-Cw1kiO5Q.js";import{I as z}from"./input-CdVRlp_C.js";import{P as se}from"./progress-BfDUL6ep.js";import{C as M}from"./circle-check-big-BECeyRf0.js";import{L as te}from"./loader-circle-DttseWp6.js";import{U as re}from"./upload-horg_POA.js";import{C as ae}from"./circle-alert-CgZ3UpaC.js";import"./Layout-DAWUt-Eh.js";import"./Footer-DeVgdLil.js";import"./Header-BUQn9Pzh.js";import"./search-ptOco6OC.js";import"./map-pin-BTYmtfJU.js";import"./store-CnfieG8L.js";import"./building-x2E7TkaF.js";import"./shield-BYC-J9mv.js";import"./popify-logo-j04LgRya.js";import"./list-CtVT9Y2l.js";import"./calendar-DmfXVeb3.js";import"./users-DHPuX-8_.js";import"./user-BT4chubZ.js";import"./separator-v-28rTKx.js";import"./telescope-CHzSYNhV.js";import"./building-2-cBWZmyXA.js";import"./file-text-SdZWtsDM.js";import"./chart-column-DgCCpDjK.js";import"./trending-up-CxKrPUSf.js";import"./trophy-C6J7-1he.js";import"./mail-Cik6hlfX.js";import"./chevron-right-BbRMN46B.js";import"./chevron-down-B1LDaoge.js";const _=500;function ie(n){const r=n.split(` 2`);if(r.length<2)return[n];const f=r[0],o=r.slice(1).filter(a=>a.trim()!=="");if(o.length<=_)return[n];const x=[];for(let a=0;a<o.length;a+=_){const g=o.slice(a,a+_);x.push([f,...g].join(` 3`))}return x}function ne(n,r){return{inserted:n.inserted+r.inserted,skipped:n.skipped+r.skipped,errors:[...n.errors,...r.errors]}}function De(){const n=Z(),[r,f]=l.useState(null),[o,x]=l.useState(null),[a,g]=l.useState(null),[j,k]=l.useState(!1),[K,d]=l.useState(""),[A,U]=l.useState(0),[c,R]=l.useState(null),[V,N]=l.useState(null),[B,q]=l.useState(!1),[H,W]=l.useState(!1);l.useEffect(()=>{(async()=>{const{data:{session:s}}=await v.auth.getSession();if(!s){n("/auth");return}const{data:i}=await v.from("user_roles").select("role").eq("user_id",s.user.id).eq("role","admin").maybeSingle();if(!i){n("/");return}W(!0)})()},[n]);const Y=async()=>{try{d("Loading bundled CSV files...");const[t,s,i]=await Promise.all([fetch("/import-data/organizers-3.csv"),fetch("/import-data/markets-3.csv"),fetch("/import-data/market_dates-3.csv")]);t.ok&&f(new File([await t.blob()],"organizers-2.csv")),s.ok&&x(new File([await s.blob()],"markets-2.csv")),i.ok&&g(new File([await i.blob()],"market_dates-2.csv")),q(!0),d("")}catch(t){console.error("Failed to auto-load files:",t),d("")}},w=t=>new Promise((s,i)=>{const m=new FileReader;m.onload=()=>s(m.result),m.onerror=i,m.readAsText(t)}),G=async()=>{k(!0),R(null),N(null);try{d("Reading CSV files...");const t=r?await w(r):null,s=o?await w(o):null,i=a?await w(a):null;if(!t&&!s&&!i){N("Please select at least one file to import."),k(!1);return}const{data:{session:m}}=await v.auth.getSession();if(!m){N("You must be logged in."),k(!1);return}const E={},F=async(X,L,O)=>{const b=ie(X),y=L.replace("_csv","");let I={inserted:0,skipped:0,errors:[]};for(let h=0;h<b.length;h++){d(`Importing ${O} (batch ${h+1}/${b.length})...`),U(Math.round((h+1)/b.length*100));const{data:P,error:$}=await v.functions.invoke("import-csv-data",{body:{[L]:b[h]}});if($)throw new Error(`${O}: ${$.message}`);P.results[y]&&(I=ne(I,P.results[y]))}E[y]=I};t&&await F(t,"organizers_csv","organizers"),s&&await F(s,"markets_csv","markets"),i&&await F(i,"market_dates_csv","market dates"),R(E)}catch(t){N(t.message||"Import failed")}finally{k(!1),d("")}},J=c?Object.values(c).reduce((t,s)=>t+s.inserted,0):0,Q=c?Object.values(c).reduce((t,s)=>t+s.skipped,0):0,T=c?Object.values(c).reduce((t,s)=>t+s.errors.length,0):0;return H?e.jsxs(ee,{title:"CSV Data Import",description:"Import organizers, markets, and market dates from CSV files. Files are processed in dependency order: organizers â markets â market dates.",children:[e.jsxs("div",{className:"grid gap-4 mb-6",children:[e.jsxs(p,{children:[e.jsx(C,{className:"pb-3",children:e.jsx(S,{className:"text-base",children:"1. Organizers CSV"})}),e.jsxs(u,{children:[e.jsx(z,{type:"file",accept:".csv",onChange:t=>{var s;return f(((s=t.target.files)==null?void 0:s[0])||null)}}),r&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[r.name," (",(r.size/1024).toFixed(1)," KB)"]})]})]}),e.jsxs(p,{children:[e.jsx(C,{className:"pb-3",children:e.jsx(S,{className:"text-base",children:"2. Markets CSV"})}),e.jsxs(u,{children:[e.jsx(z,{type:"file",accept:".csv",onChange:t=>{var s;return x(((s=t.target.files)==null?void 0:s[0])||null)}}),o&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[o.name,"
3(",(o.size/1024).toFixed(1)," KB)"]})]})]}),e.jsxs(p,{children:[e.jsx(C,{className:"pb-3",children:e.jsx(S,{className:"text-base",children:"3. Market Dates CSV"})}),e.jsxs(u,{children:[e.jsx(z,{type:"file",accept:".csv",onChange:t=>{var s;return g(((s=t.target.files)==null?void 0:s[0])||null)}}),a&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-1",children:[a.name," (",(a.size/1024).toFixed(1)," KB)"]})]})]})]}),!B&&e.jsx(D,{onClick:Y,variant:"outline",className:"w-full mb-3",size:"lg",children:"Load Bundled CSV Files"}),B&&e.jsxs("p",{className:"text-sm text-green-600 mb-3 flex items-center gap-1",children:[e.jsx(M,{className:"w-4 h-4"})," All 3 files loaded automatically. Click Start Import below."]}),e.jsx(D,{onClick:G,disabled:j||!r&&!o&&!a,className:"w-full mb-3",size:"lg",children:j?e.jsxs(e.Fragment,{children:[e.jsx(te,{className:"w-4 h-4 mr-2 animate-spin"}),K]}):e.jsxs(e.Fragment,{children:[e.jsx(re,{className:"w-4 h-4 mr-2"}),"Start Import"]})}),j&&A>0&&e.jsx(se,{value:A,className:"mb-6"}),!j&&e.jsx("div",{className:"mb-3"}),V&&e.jsx(p,{className:"border-destructive mb-6",children:e.jsx(u,{className:"pt-4",children:e.jsxs("div",{className:"flex items-start gap-2 text-destructive",children:[e.jsx(ae,{className:"w-5 h-5 mt-0.5 shrink-0"}),e.jsx("p",{children:V})]})})}),c&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(p,{className:"border-primary",children:e.jsxs(u,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-primary mb-2",children:[e.jsx(M,{className:"w-5 h-5"}),e.jsx("span",{className:"font-semibold",children:"Import Complete"})]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[J," inserted · ",Q," skipped · ",T," errors"]})]})}),Object.entries(c).map(([t,s])=>e.jsxs(p,{children:[e.jsx(C,{className:"pb-2",children:e.jsx(S,{className:"text-base capitalize",children:t.replace("_"," ")})}),e.jsxs(u,{children:[e.jsxs("div",{className:"grid grid-cols-3 gap-4 text-sm mb-2",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Inserted:"})," ",e.jsx("span",{className:"font-medium text-primary",children:s.inserted})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Skipped:"})," ",e.jsx("span",{className:"font-medium",children:s.skipped})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Errors:"})," ",e.jsx("span",{className:`font-medium ${s.errors.length>0?"text-destructive":""}`,children:s.errors.length})]})]}),s.errors.length>0&&e.jsx("div",{className:"mt-2 max-h-40 overflow-y-auto bg-muted rounded p-2",children:s.errors.map((i,m)=>e.jsx("p",{className:"text-xs text-destructive",children:i},m))})]})]},t))]})]}):null}export{De 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.