1import{j as r}from"./query-Q_ThwKGI.js";import{u as P,r as l}from"./react-vendor-wefok28f.js";import{u as J}from"./index-C4Kq7kkR.js";import{s as v}from"./client-DjLwxtrO.js";import{u as K}from"./useLogger-CDsXA2_R.js";import{trackRedirectEvent as M,initializeTrackingForCampaign as Q,trackPageView as U}from"./trackingScripts-1XOvU7Jx.js";import{r as V}from"./redirectService-hDxAAvGF.js";import{F as Y}from"./FormUnavailableError-D5g8vwrQ.js";import"./supabase-COHOFxoQ.js";import"./icons-By8asZST.js";import"./consolidatedMessageService-CxI_JEnX.js";const le=()=>{const{campaignId:c,userCode:_,campaignSlug:h}=P(),{toast:b}=J(),i=K("OptimizedRedirect"),[F,E]=l.useState(!0),[N,k]=l.useState(!1),[d,j]=l.useState(null),[q,G]=l.useState(!1),[a,T]=l.useState(null),[m,A]=l.useState(null),[n,D]=l.useState({title:"Join our WhatsApp Group",subtitle:"Click the button below to join the group",title_size:"large",subtitle_size:"medium",title_color:"#222222",subtitle_color:"#666666",button_text:"Join Group",button_color:"#25D366",background_color:"#ffffff",redirect_delay:0,full_groups_message:"All groups are currently full. Please try again later."}),I=async()=>{try{i.info("ð Starting optimized redirect flow",{campaignId:c,userCode:_,campaignSlug:h});let e=v.from("campaigns").select("id, user_id, status");if(c)/^\d+$/.test(c)?e=e.like("id::text",`%${c}%`):e=e.eq("id",c);else if(_&&h){const x=parseInt(_);if(!isNaN(x)){const{data:w}=await v.from("profiles").select("id").eq("user_code",x).single();w&&(e=e.eq("user_id",w.id).eq("slug",h))}}const{data:s,error:t}=await e.maybeSingle();if(t)throw t;if(!s)throw new Error("Campaign not found");i.debug("Checking campaign owner account status",{userId:s.user_id});const{data:p,error:o}=await v.from("profiles").select("subscription_plan, trial_end_date, leads_count, leads_limit").eq("id",s.user_id
1).maybeSingle();if(o||!p)throw i.error("Failed to fetch profile",{error:o}),new Error("Campaign owner not found");const B=new Date,S=p.trial_end_date?new Date(p.trial_end_date):null,R=p.subscription_plan==="FREE"&&S&&B>S;if(R)throw i.info("Account blocked, not loading campaign",{isTrialExpired:R}),new Error("OWNER_BLOCKED");let g=v.from("campaigns").select(` 2 id, 3 name, 4 status, 5 redirect_settings, 6 first_message_settings, 7 campaign_groups!inner( 8 id, 9 group_id, 10 whatsapp_groups!inner( 11 id, 12 name, 13 invite_link, 14 status, 15 current_participants, 16 max_participants 17 ) 18 ) 19 `);if(c)/^\d+$/.test(c)?g=g.like("id::text",`%${c}%`):g=g.eq("id",c);else if(_&&h){const x=parseInt(_);if(!isNaN(x)){const{data:w}=await v.from("profiles").select("id").eq("user_code",x).single();w&&(g=g.eq("user_id",w.id).eq("slug",h))}}const{data:f,error:z}=await g.maybeSingle();if(z)throw z;if(!f)throw new Error("Campaign not found");const u={id:f.id,name:f.name,status:f.status||"active",redirect_settings:f.redirect_settings||{},first_message_settings:f.first_message_settings||{},campaign_groups:f.campaign_groups||[]};return u.redirect_settings&&typeof u.redirect_settings=="object"&&D(x=>({...x,...u.redirect_settings})),T(u),i.info("â Campaign loaded",{id:u.id,name:u.name,status:u.status,hasFirstMessageSettings:!!u.first_message_settings}),u}catch(e){throw i.error("â Error loading campaign",{error:e}),e}},O=async e=>{try{if(e.status==="inactive")return i.info("â ï¸ Campaign is inactive, showing full groups message"),null;i.info("ð Finding available group");const s=e.campaign_groups.filter(p=>{const o=p.whatsapp_groups;return o.status==="available"&&o.current_participants<o.max_participants});if(s.length===0)return i.warning("â ï¸ No available groups found"),null;const t=s[0].whatsapp_groups;return A(t),i.info("â Available group found",{id:t.id,name:t.name,participants:`${t.current_participants}/${t.max_participants}`}),t}catch(s){return i.error("â Error finding available group",{error:s}),null}},$=e=>{try{i.info("ð Initializing tracking"),Q(e),U(`WhatsApp Group Redirect - ${a==null?void 0:a.name}`,window.location.href),i.info("â Tracking initialized")}catch(s){i.error("â Error initializing tracking",{error:s})}},C=async()=>{if(!m||!a){b({title:"Error",description:"No available group found",variant:"destructive"});return}k(!0);try{i.info("ð Starting redirect with enhanced messaging:",{groupId:m.id,groupName:m.name,campaignId:a.id}),await V(m.id,{toast:b},a.id),a.redirect_settings&&M(a.id,m.id),b({title:"Redirecting",description:"You are being redirected to the WhatsApp group"}),window.location.href=m.invite_link}catch(e){i.error("â Error during redirect",{error:e}),k(!1),b({title:"Error",description:"Failed to redirect to the WhatsApp group",variant:"destructive"})}};l.useEffect(()=>(a!=null&&a.name&&(document.title=a.name),()=>{document.title="Leads Flow"}),[a==null?void 0:a.name]),l.useEffect(()=>{(async()=>{var s;try{const t=await I(),p=await O(t);t.redirect_settings&&$(t.redirect_settings);const o=(s=t.redirect_settings)==null?void 0:s.redirect_delay;o&&o>0&&p&&j(o)}catch(t){if(i.error("â Initialization failed",{error:t}),(t==null?void 0:t.message)==="OWNER_BLOCKED"){G(!0),E(!1);return}b({title:"Error",description:"Failed to load campaign details",variant:"destructive"})}finally{E(!1)}})()},[]),l.useEffect(()=>{if(d&&d>0){const e=setTimeout(()=>{j(d-1),d===1&&C()},1e3);return()=>clearTimeout(e)}},[d]);const L=e=>{switch(e){case"small":return"text-xl md:text-xl";case"medium":return"text-2xl md:text-2xl";case"extra large":return"text-4xl md:text-4xl";case"large":default:return"text-3xl md:text-3xl"}},W=e=>{switch(e){case"small":return"text-lg md:text-lg";case"medium":return"text-xl md:text-xl";case"large":return"text-2xl md:text-2xl";case"extra large":return"text-3xl md:text-3xl";default:return"text-lg md:text-lg"}};if(F)return r.jsx("div",{className:"min-h-screen flex items-center justify-center p-4",style:{backgroundColor:n.background_color},children:r.jsxs("div",{className:"text-center",children:[r.jsx("div",{className:"w-16 h-16 border-4 border-purple-200 border-t-purple-600 rounded-full animate-spin mx-auto mb-4"}),r.jsx("p",{className:"text-muted-foreground",children:"Loading group information..."})]})});if(q)return r.jsx(Y,{isOwnerBlocked:!0});const y=!m;return r.jsx("div",{className:"min-h-screen flex items-center justify-center p-4",style:{backgroundColor:n.background_color},children:r.jsxs("div",{className:"max-w-md w-full p-8 space-y-6 bg-white rounded-xl shadow-lg animate-fade-in",children:[r.jsxs("div",{className:"text-center space-y-4",children:[r.jsx("h1",{className:`font-semibold ${L(n.title_size)}`,style:{color:n.title_color},children:y?n.full_groups_message:n.title}),!y&&r.jsxs(r.Fragment,{children:[r.jsx("p",{className:`${W(n.subtitle_size)}`,style:{color:n.subtitle_color},children:n.subtitle}),d!==null&&d>0&&r.jsx("div",{className:"mt-4",children:r.jsxs("div",{className:"text-sm font-medium py-2 px-4 bg-muted inline-block rounded-full",children:["Redirecting in ",r.jsx("span",{className:"font-bold text-purple-600",children:d})," ",d===1?"second":"seconds","..."]})})]})]}),!y&&r.jsx("button",{className:"w-full py-3 px-6 rounded-lg font-semibold text-white transition-all duration-200 hover:scale-105 disabled:opacity-50 disabled:cursor-not-allowed",style:{backgroundColor:n.button_color},onClick:C,disabled:N,children:N?r.jsxs("div",{className:"flex items-center justify-center gap-2",children:[r.jsx("div",{className:"w-4 h-4 border-2 border-white border-t-transparent roun
19ded-full animate-spin"}),"Redirecting..."]}):n.button_text})]})})};export{le 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.