1import{u as es,r as c,s as p,R as l,j as e,C as je,l as ge,m as fe,B as x,bm as ve,a as _e,S as U,F as O,I as M,J as z,K as _,ar as ss,aA as Se,ai as S,e as rs,bE as os,aB as Ne,aC as we,aD as be,aE as Ce,an as q,aF as ye}from"./index-DbUPl3PC.js";import{T as Te,a as Pe,b as V,c as g,d as Le,e as f}from"./table-CHZFxP31.js";import{S as ts}from"./switch-5nMfL5C7.js";import{T as ns,a as as,b as Fe,c as ke}from"./tabs-CXFGBUnK.js";import{T as is}from"./TablePagination-D_rZNVJd.js";import{D as ls}from"./download-Dr2kzgCW.js";import{E as De}from"./external-link-DW49lBch.js";import{C as cs}from"./copy-xjlC3KC1.js";import{R as ds}from"./refresh-cw-CJLggVLQ.js";import{T as ms}from"./trophy-Chf00AqI.js";import{L as us}from"./link-BKFNMjt8.js";import{U as ps}from"./upload-p8PDaxto.js";import"./chevron-left-C5gI7Bao.js";const N=["event","merchandise","platinum","gold","silver","community"],W={event:"Event Sponsor",merchandise:"Merchandise Sponsor",platinum:"Platinum (Premium)",gold:"Gold (Premium)",silver:"Silver (Standard)",community:"Community"},Ae={merchandise:5,event:5,"port-a-pot":5,platinum:4,gold:3,silver:2,community:1};function xs(o){return o.toUpperCase().replace(/[^A-Z0-9]/g,"").substring(0,20)}function hs(o){return new Date(o).getFullYear().toString()}function Ls(){const{activeEvent:o}=es(),[w,ee]=c.useState([]),[v,Ie]=c.useState([]),[se,Re]=c.useState([]),[$e,Ee]=c.useState(!0),[Ue,L]=c.useState(!1),[F,re]=c.useState(null),[B,Y]=c.useState(""),[oe,H]=c.useState(""),[k,D]=c.useState(""),[te,G]=c.useState(!0),[ne,ae]=c.useState(!1),[J,K]=c.useState("url"),[Oe,A]=c.useState(!1),[b,ie]=c.useState(""),[I,le]=c.useState("community"),[Z,ce]=c.useState(1),[C,Me]=c.useState(15),[y,ze]=c.useState("active");c.useEffect(()=>{Q(),o!=null&&o.id&&(T(),X())},[o==null?void 0:o.id]);const Q=async()=>{const{data:s,error:t}=await p.from("sponsors").select("*").order("name");if(t){l.error("Failed to load sponsors");return}const{data:r,error:n}=await p.from("event_sponsors").select("sponsor_id, event:events(event_date)");if(!n&&r){const i={};r.forEach(a=>{var m;if((m=a.event)!=null&&m.event_date){const u=new Date(a.event.event_date).getFullYear();i[a.sponsor_id]||(i[a.sponsor_id]=[]),i[a.sponsor_id].includes(u)||i[a.sponsor_id].push(u)}});const d=(s||[]).map(a=>{var m;return{...a,years_sponsored:((m=i[a.id])==null?void 0:m.length)||0,first_year:i[a.id]?Math.min(...i[a.id]):void 0}});ee(d)}else ee(s||[]);Ee(!1)},T=async()=>{if(!(o!=null&&o.id))return;const{data:s,error:t}=await p.from("event_sponsors").select("*, sponsor:sponsors(*)").eq("event_id",o.id).order("sort_order");if(t){l.error("Failed to load event sponsors");return}const r=(s||[]).sort((n,i)=>{const d=N.indexOf(n.tier),a=N.indexOf(i.tier);return d!==a?d-a:n.sort_order-i.sort_order});Ie(r)},X=async()=>{if(!(o!=null&&o.id))return;const{data:s,error:t}=await p.from("promo_codes").select("id, code, sponsor_id, max_uses, current_uses, is_active").eq("event_id",o.id).not("sponsor_id","is",null);!t&&s&&Re(s)},de=async(s,t,r)=>{if(!(o!=null&&o.id)||!o.event_date)return;const n=Ae[r]||1,i=hs(o.event_date);let a=`${xs(t)}${i}`;for(let u=0;u<10;u++){const R=u===0?a:`${a}-${u+1}`,{data:$}=await p.from("promo_codes").select("id").eq("code",R).maybeSingle();if(!$){a=R;break}if(u===9){l.error("Could not generate a unique promo code");return}}const{error:m}=await p.from("promo_codes").insert({code:a,discount_type:"flat",discount_value:45,applies_to_types:["registration"],max_uses:n,max_items_per_order:n,discount_mode:"per_item",event_id:o.id,sponsor_id:s,is_active:!0});if(m){console.error("Failed to create promo code:",m),l.error("Failed to create promo code");return}l.success(`Promo code ${a} created ($45 off à ${n} registrations)`),X()},qe=async(s,t)=>{if(!(o!=null&&o.id))return;const r=Ae[t]||1,n=se.find(d=>d.sponsor_id===s);if(!n)return;const{error:i}=await p.from("promo_codes").update({max_uses:r,max_items_per_order:r}).eq("id",n.id);if(i){l.error("Failed to update promo code limits");return}l.success(`Promo code updated to ${r} free registrations`),X()},Ve=s=>{navigator.clipboard.writeText(s),l.success(`Copied ${s} to clipboard`)},We=async s=>{ae(!0);try{const t=s.name.split(".").pop(),r=`${Date.now()}-${Math.random().toString(36).substring(7)}.${t}`,{error:n}=await p.storage.from("sponsor-logos").upload(r,s);if(n)throw n;const{data:{publicUrl:i}}=p.storage.from("sponsor-logos").getPublicUrl(r);return i}catch{return l.error("Failed to upload logo"),null}finally{ae(!1)}},me=s=>{s?(re(s),Y(s.name),H(s.website_url||""),D(s.logo_url||""),G(s.is_active)):(re(null),Y(""),H(""),D(""),G(!0)),K("url"),L(!0)},Be=async()=>
1{if(!B.trim()){l.error("Sponsor name is required");return}const s={name:B.trim(),website_url:oe.trim()||null,logo_url:k.trim()||null,is_active:te};if(F){const{error:t}=await p.from("sponsors").update(s).eq("id",F.id);if(t){l.error("Failed to update sponsor");return}l.success("Sponsor updated")}else{const{error:t}=await p.from("sponsors").insert(s);if(t){l.error("Failed to create sponsor");return}l.success("Sponsor created")}L(!1),Q(),T()},Ye=async s=>{if(!confirm("Delete this sponsor? This will remove them from all events."))return;const{error:t}=await p.from("sponsors").delete().eq("id",s);if(t){l.error("Failed to delete sponsor");return}l.success("Sponsor deleted"),Q(),T()},He=()=>{ie(""),le("community"),A(!0)},Ge=async()=>{if(!b||!(o!=null&&o.id)){l.error("Please select a sponsor");return}if(v.find(d=>d.sponsor_id===b)){l.error("This sponsor is already assigned to this event");return}const t=v.filter(d=>d.tier===I),r=t.length>0?Math.max(...t.map(d=>d.sort_order)):-1,{error:n}=await p.from("event_sponsors").insert({event_id:o.id,sponsor_id:b,tier:I,sort_order:r+1});if(n){l.error("Failed to assign sponsor");return}const i=w.find(d=>d.id===b);i&&await de(b,i.name,I),l.success("Sponsor assigned to event"),A(!1),T()},Je=async(s,t,r)=>{const{error:n}=await p.from("event_sponsors").update({tier:r}).eq("id",s);if(n){l.error("Failed to update tier");return}await qe(t,r),l.success("Tier updated"),T()},Ke=async s=>{if(!confirm("Remove this sponsor from the event?"))return;const{error:t}=await p.from("event_sponsors").delete().eq("id",s);if(t){l.error("Failed to remove sponsor");return}l.success("Sponsor removed from event"),T()},ue=s=>se.find(t=>t.sponsor_id===s),Ze=()=>{var $;if(!o||v.length===0)return;const s=h=>{const j=h==null?"":String(h);return/[",\n\r]/.test(j)?`"${j.replace(/"/g,'""')}"`:j},t=[...v].sort((h,j)=>{const E=N.indexOf(h.tier)-N.indexOf(j.tier);return E!==0?E:h.sort_order-j.sort_order}),r=["Tier","Sponsor Name","Website","Promo Code","Promo Uses","Sort Order"],n=t.map(h=>{var xe,he;const j=ue(h.sponsor_id),E=j?`${j.current_uses}/${j.max_uses??"â"}`:"";return[W[h.tier]||h.tier,((xe=h.sponsor)==null?void 0:xe.name)||"",((he=h.sponsor)==null?void 0:he.website_url)||"",(j==null?void 0:j.code)||"",E,h.sort_order].map(s).join(",")}),i=[r.join(","),...n].join(` 2`),d=new Blob([i],{type:"text/csv;charset=utf-8;"}),a=URL.createObjectURL(d),m=document.createElement("a"),u=o.slug||(($=o.name)==null?void 0:$.toLowerCase().replace(/\s+/g,"-"))||"event",R=new Date().toISOString().slice(0,10);m.href=a,m.download=`${u}-sponsors-${R}.csv`,document.body.appendChild(m),m.click(),document.body.removeChild(m),URL.revokeObjectURL(a)},pe=w.filter(s=>s.is_active&&!v.find(t=>t.sponsor_id===s.id)),P=c.useMemo(()=>y==="all"?w:w.filter(s=>y==="active"?s.is_active:!s.is_active),[w,y]),Qe=Math.ceil(P.length/C),Xe=c.useMemo(()=>{const s=(Z-1)*C;return P.slice(s,s+C)},[P,Z,C]);return c.useEffect(()=>{ce(1)},[C,y]),$e?e.jsx("div",{className:"p-4",children:"Loading sponsors..."}):e.jsxs(ns,{defaultValue:"event",className:"space-y-4",children:[e.jsxs(as,{children:[e.jsx(Fe,{value:"event",children:"Event Sponsors"}),e.jsx(Fe,{value:"master",children:"Master List"})]}),e.jsx(ke,{value:"event",children:e.jsxs(je,{children:[e.jsxs(ge,{className:"flex flex-row items-center justify-between",children:[e.jsxs(fe,{children:[(o==null?void 0:o.name)||"No Event Selected"," - Sponsors"]}),o&&e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(x,{onClick:Ze,size:"sm",variant:"outline",disabled:v.length===0,children:[e.jsx(ls,{className:"w-4 h-4 mr-2"}),"Download CSV"]}),e.jsxs(x,{onClick:He,size:"sm",children:[e.jsx(ve,{className:"w-4 h-4 mr-2"}),"Assign Sponsor"]})]})]}),e.jsx(_e,{children:o?v.length===0?e.jsx("p",{className:"text-muted-foreground",children:"No sponsors assigned to this event yet."}):e.jsx("div",{className:"space-y-6",children:N.map(s=>{const t=v.filter(r=>r.tier===s);return t.length===0?null:e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-semibold text-muted-foreground uppercase tracking-wider mb-3",children:W[s]}),e.jsxs(Te,{children:[e.jsx(Pe,{children:e.jsxs(V,{children:[e.jsx(g,{children:"Logo"}),e.jsx(g,{children:"Name"}),e.jsx(g,{children:"Website"}),e.jsx(g,{children:"Tier"}),e.jsx(g,{children:"Promo Code"}),e.jsx(g,{className:"w-[100px]",children:"Actions"})]})}),e.jsx(Le,{children:t.map(r=>{var i,d,a,m;
2const n=ue(r.sponsor_id);return e.jsxs(V,{children:[e.jsx(f,{children:(i=r.sponsor)!=null&&i.logo_url?e.jsx("img",{src:r.sponsor.logo_url,alt:(d=r.sponsor)==null?void 0:d.name,className:"h-10 w-20 object-contain bg-muted rounded"}):e.jsx("div",{className:"h-10 w-20 bg-muted rounded flex items-center justify-center text-xs text-muted-foreground",children:"No logo"})}),e.jsx(f,{className:"font-medium",children:(a=r.sponsor)==null?void 0:a.name}),e.jsx(f,{children:((m=r.sponsor)==null?void 0:m.website_url)&&e.jsxs("a",{href:r.sponsor.website_url,target:"_blank",rel:"noopener noreferrer",className:"text-primary hover:underline inline-flex items-center gap-1",children:[e.jsx(De,{className:"w-3 h-3"}),"Visit"]})}),e.jsx(f,{children:e.jsxs(U,{value:r.tier,onValueChange:u=>Je(r.id,r.sponsor_id,u),children:[e.jsx(O,{className:"w-[160px]",children:e.jsx(M,{})}),e.jsx(z,{children:N.map(u=>e.jsx(_,{value:u,children:W[u]},u))})]})}),e.jsx(f,{children:n?e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(ss,{className:"w-3.5 h-3.5 text-muted-foreground"}),e.jsx("code",{className:"text-xs font-mono bg-muted px-1.5 py-0.5 rounded",children:n.code}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",n.current_uses,"/",n.max_uses,")"]}),e.jsx(x,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>Ve(n.code),children:e.jsx(cs,{className:"w-3 h-3"})})]}):e.jsxs(x,{variant:"outline",size:"sm",className:"h-7 text-xs",onClick:()=>r.sponsor&&de(r.sponsor_id,r.sponsor.name,r.tier),children:[e.jsx(ds,{className:"w-3 h-3 mr-1"}),"Generate"]})}),e.jsx(f,{children:e.jsx(x,{variant:"ghost",size:"icon",onClick:()=>Ke(r.id),children:e.jsx(Se,{className:"w-4 h-4 text-destructive"})})})]},r.id)})})]})]},s)})}):e.jsx("p",{className:"text-muted-foreground",children:"Select an event to manage its sponsors."})})]})}),e.jsx(ke,{value:"master",children:e.jsxs(je,{children:[e.jsxs(ge,{className:"flex flex-row items-center justify-between flex-wrap gap-4",children:[e.jsx(fe,{children:"All Sponsors"}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(S,{htmlFor:"status-filter",className:"text-sm text-muted-foreground whitespace-nowrap",children:"Status:"}),e.jsxs(U,{value:y,onValueChange:s=>ze(s),children:[e.jsx(O,{id:"status-filter",className:"w-[130px]",children:e.jsx(M,{})}),e.jsxs(z,{children:[e.jsx(_,{value:"active",children:"Active"}),e.jsx(_,{value:"inactive",children:"Inactive"}),e.jsx(_,{value:"all",children:"All"})]})]})]}),e.jsxs(x,{onClick:()=>me(),size:"sm",children:[e.jsx(ve,{className:"w-4 h-4 mr-2"}),"Add Sponsor"]})]})]}),e.jsx(_e,{children:P.length===0?e.jsx("p",{className:"text-muted-foreground",children:w.length===0?"No sponsors created yet.":`No ${y} sponsors found.`}):e.jsxs(e.Fragment,{children:[e.jsxs(Te,{children:[e.jsx(Pe,{children:e.jsxs(V,{children:[e.jsx(g,{children:"Logo"}),e.jsx(g,{children:"Name"}),e.jsx(g,{children:"Years"}),e.jsx(g,{children:"Website"}),e.jsx(g,{children:"Status"}),e.jsx(g,{className:"w-[100px]",children:"Actions"})]})}),e.jsx(Le,{children:Xe.map(s=>e.jsxs(V,{children:[e.jsx(f,{children:s.logo_url?e.jsx("img",{src:s.logo_url,alt:s.name,className:"h-10 w-20 object-contain bg-muted rounded"}):e.jsx("div",{className:"h-10 w-20 bg-muted rounded flex items-center justify-center text-xs text-muted-foreground",children:"No logo"})}),e.jsx(f,{className:"font-medium",children:e.jsxs("div",{className:"flex items-center gap-2",children:[s.name,(s.years_sponsored||0)>=5&&e.jsx("span",{title:"Loyal Sponsor (5+ years)",children:e.jsx(ms,{className:"w-4 h-4 text-amber-500"})})]})}),e.jsx(f,{children:s.years_sponsored?e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(rs,{className:"w-3.5 h-3.5 text-muted-foreground"}),e.jsx("span",{className:"font-medium",children:s.years_sponsored}),s.first_year&&e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(since ",s.first_year,")"]})]}):e.jsx("span",{className:"text-muted-foreground text-sm",children:"â"})}),e.jsx(f,{children:s.website_url&&e.jsxs("a",{href:s.website_url,target:"_blank",rel:"noopener noreferrer",className:"text-primary hover:underline inline-flex items-center gap-1",children:[e.jsx(De,{className:"w-3 h-3"}),(()=>{try{return new URL(s.website_url).hostname}catch{return s.website_url}})()]})}),e.jsx(f,{children:e.jsx("span",{className:`text-sm ${s.is_active?"text-green-600":"text-muted-foreground"}`,children:s.is_active?"Active":"Inactive"})}),e.jsx(f,{children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx(x,{variant:"ghost",size:"icon",onClick:()=>me(s),children:e.jsx(os,{className:"w-4 h-4"})}),e.jsx(x,{variant:"ghost",size:"icon",onClick:()=>Ye(s.id),children:e.jsx(Se,{className:"w-4 h-4 text-destructive"})})]})})]},s.id))})]}),e.jsx(is,{currentPage:Z,totalPages:Qe,totalItems:P.length,itemsPerPage:C,onPageChange:ce,onItemsPerPageChange:Me,itemLabel:"sponsors"})]})})]})}),e.jsx(Ne,{open:Ue,onOpenChange:L,children:e.jsxs(we,{className:"max-w-md",children:[e.jsx(be,{children:e.jsx(Ce,{children:F?"Edit Sponsor":"Add Sponsor"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(S,{htmlFor:"sponsorName",children:"Name *"}),e.jsx(q,{id:"sponsorName",value:B,onChange:s=>Y(s.target.value),placeholder:"Company Name"})]}),e.jsxs("div",{children:[e.jsx(S,{htmlFor:"sponsorWebsite",children:"Website URL"}),e.jsx(q,{id:"sponsorWebsite",value:oe,onChange:s=>H(s.target.value),placeholder:"https://example.com"})]}),e.jsxs("div",{children:[e.jsx(S,{children:"Logo"}),e.jsxs("div",{className:"flex gap-2 mb-2",children:[e.jsxs(x,{type:"button",variant:J==="url"?"default":"outline",size:"sm",onClick:()=>K("url"),children:[e.jsx(us,{className:"w-4 h-4 mr-1"}),"URL"]}),e.jsxs(x,{type:"button",variant:J==="upload"?"default":"outline",size:"sm",onClick:()=>K("upload"),children:[e.jsx(ps,{className:"w-4 h-4 mr-1"}),"Upload"]})]}),J==="url"?e.jsx(q,{value:k,onChange:s=>D(s.target.value),placeholder:"https://example.com/logo.png"}):e.jsx(q,{type:"file",accept:"image/*",disabled:ne,onChange:async s=>{var r;const t=(r=s.target.files)==null?void 0:r[0];if(t){const n=await We(t);n&&D(n)}}}),k&&e.jsx("div",{className:"mt-2",children:e.jsx("img",{src:k,alt:"Preview",className:"h-16 w-32 object-contain bg-muted rounded border"})})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(ts,{id:"sponsorActive",checked:te,onCheckedChange:G}),e.jsx(S,{htmlFor:"sponsorActive",children:"Active"})]})]}),e.jsxs(ye,{children:[e.jsx(x,{variant:"outline",onClick:()=>L(!1),children:"Cancel"}),e.jsx(x,{onClick:Be,disabled:ne,children:F?"Update":"Create"})]})]})}),e.jsx(Ne,{open:Oe,onOpenChange:A,children:e.jsxs(we,{className:"max-w-md",children:[e.jsx(be,{children:e.jsx(Ce,{children:"Assign Sponsor to Event"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(S,{children:"Sponsor"}),e.jsxs(U,{value:b,onValueChange:ie,children:[e.jsx(O,{children:e.jsx(M,{placeholder:"Select a sponsor..."})}),e.jsx(z,{children:pe.length===0?e.jsx(_,{value:"",disabled:!0,children:"No available sponsors"}):pe.map(s=>e.jsx(_,{value:s.id,children:s.name},s.id))})]})]}),e.jsxs("div",{children:[e.jsx(S,{children:"Tier"}
2),e.jsxs(U,{value:I,onValueChange:le,children:[e.jsx(O,{children:e.jsx(M,{})}),e.jsx(z,{children:N.map(s=>e.jsx(_,{value:s,children:W[s]},s))})]})]})]}),e.jsxs(ye,{children:[e.jsx(x,{variant:"outline",onClick:()=>A(!1),children:"Cancel"}),e.jsx(x,{onClick:Ge,children:"Assign"})]})]})})]})}export{Ls as SponsorManagementPanel};
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.