1import{j as e,C,b as V,f as T,B as w,U as W,a as D,r as m,s as y,c as Y,d as J}from"./index-DVrL3xID.js";import{L as $}from"./Layout-By6nV2kW.js";import{T as I}from"./trending-up-HNiDSAZu.js";import{S as K}from"./star-BTEbqbLn.js";import{Z as Q}from"./zap-B6OikXqr.js";import{I as O}from"./input-BbuQ1qqy.js";import{S as F,a as U,b as G,c as q,d as N}from"./select-BA4zyYMp.js";import{S as X}from"./slider-BcGA6KLr.js";import{F as ee}from"./filter-I9bKgcKE.js";import{S as A}from"./search-DHJuPoDr.js";import{B as S}from"./index-CRlXEeIp.js";import{S as E}from"./skeleton-B-lycmoz.js";import{U as se}from"./users-Cy3sXXAx.js";import{C as ae}from"./copy-D8s1ieUC.js";import{E as re}from"./external-link-BNp9lGXe.js";import{L as te}from"./label-CobuJheF.js";import{S as ie}from"./sparkles-DfyS1GyF.js";import{H as M}from"./heart-Boik36JO.js";import{A as ne}from"./index-p0t1FpUF.js";import{m as le}from"./proxy-D4DKdYdZ.js";import{L as ce}from"./leaf-Ch2hcwgu.js";import{S as oe}from"./shopping-cart-Chk57zJI.js";import{E as de}from"./eye-DEHf5viq.js";import{T as me,a as xe,b as z,c as B}from"./tabs-CFlsbSTs.js";import"./avatar-D_VCVAB1.js";import"./check-DfmPE1HZ.js";import"./index-BdQq_4o_.js";import"./index-BSb876Rp.js";import"./chevron-down-LCIP34Kz.js";function he(){return e.jsx("div",{className:"bg-gradient-to-r from-primary to-accent text-primary-foreground py-12",children:e.jsx("div",{className:"container mx-auto px-4",children:e.jsxs("div",{className:"max-w-4xl mx-auto text-center",children:[e.jsx("h1",{className:"text-4xl font-bold mb-4",children:"Product Discovery Hub"}),e.jsx("p",{className:"text-xl mb-8 text-green-100",children:"Discover high-converting products and generate your affiliate links instantly"}),e.jsxs("div",{className:"grid md:grid-cols-3 gap-6 mt-8",children:[e.jsxs("div",{className:"flex items-center justify-center space-x-3",children:[e.jsx(I,{className:"h-8 w-8 text-green-200"}),e.jsxs("div",{className:"text-left",children:[e.jsx("h3",{className:"font-semibold",children:"High Commissions"}),e.jsx("p",{className:"text-sm text-green-200",children:"Up to 30% commission rates"})]})]}),e.jsxs("div",{className:"flex items-center justify-center space-x-3",children:[e.jsx(K,{className:"h-8 w-8 text-green-200"}),e.jsxs("div",{className:"text-left",children:[e.jsx("h3",{className:"font-semibold",children:"Top Products"}),e.jsx("p",{className:"text-sm text-green-200",children:"Curated high-converting items"})]})]}),e.jsxs("div",{className:"flex items-center justify-center space-x-3",children:[e.jsx(Q,{className:"h-8 w-8 text-green-200"}),e.jsxs("div",{className:"text-left",children:[e.jsx("h3",{className:"font-semibold",children:"Instant Links"}),e.jsx("p",{className:"text-sm text-green-200",children:"Generate links in one click"})]})]})]})]})})})}function ue({searchTerm:c,setSearchTerm:o,category:j,setCategory:x,sortBy:u,setSortBy:f,commissionRange:d,setCommissionRange:a}){return e.jsxs(C,{className:"p-6 mb-8",children:[e.jsxs("div",{className:"flex items-center space-x-2 mb-6",children:[e.jsx(ee,{className:"h-5 w-5 text-muted-foreground"}),e.jsx("h2",{className:"text-lg font-semibold text-foreground",children:"Filter Products"})]}),e.jsxs("div",{className:"grid md:grid-cols-2 lg:grid-cols-4 gap-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"Search Products"}),e.jsxs("div",{className:"relative",children:[e.jsx(A,{className:"absolute left-3 top-1/2 transform -translate-y-1/2 text-muted-foreground h-4 w-4"}),e.jsx(O,{placeholder:"Search products...",value:c,onChange:r=>o(r.target.value),className:"pl-10"})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"Category"}),e.jsxs(F,{value:j,onValueChange:x,children:[e.jsx(U,{children:e.jsx(G,{placeholder:"All Categories"})}),e.jsxs(q,{children:[e.jsx(N,{value:"all",children:"All Categories"}),e.jsx(N,{value:"home",children:"Home & Garden"}),e.jsx(N,{value:"tech",children:"Technology"}),e.jsx(N,{value:"fashion",children:"Fashion"}),e.jsx(N,{value:"fitness",children:"Health & Fitness"}),e.jsx(N,{value:"beauty",children:"Beauty"}),e.jsx(N,{value:"food",children:"Food & Beverage"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium text-foreground",children:"Sort By"}),e.jsxs(F,{value:u,onValueChange:f,children:[e.jsx(U,{children:e.jsx(G,{placeholder:"Sort by..."})}),e.jsxs(q,{children:[e.jsx(N,{value:"commission",children:"Highe
1st Commission"}),e.jsx(N,{value:"popularity",children:"Most Popular"}),e.jsx(N,{value:"newest",children:"Newest Products"}),e.jsx(N,{value:"name",children:"Product Name"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-foreground",children:["Commission Range: ",d[0],"% - ",d[1],"%"]}),e.jsx("div",{className:"pt-2",children:e.jsx(X,{value:d,onValueChange:a,max:50,min:0,step:1,className:"w-full"})})]})]})]})}function fe({products:c,loading:o,error:j,onGenerateLink:x,generatingLinkId:u}){const{toast:f}=V(),d=async a=>{try{await navigator.clipboard.writeText(a),f({title:"Copied!",description:"Product link copied to clipboard"})}catch(r){console.error("Failed to copy to clipboard:",r)}};return o?e.jsx("div",{className:"grid md:grid-cols-2 lg:grid-cols-3 gap-6",children:Array.from({length:6}).map((a,r)=>e.jsxs(C,{className:"overflow-hidden",children:[e.jsx(E,{className:"h-48 w-full",style:{minHeight:"192px",aspectRatio:"16/9"}}),e.jsxs(T,{className:"p-4",children:[e.jsx(E,{className:"h-6 w-3/4 mb-2",style:{minHeight:"24px"}}),e.jsx(E,{className:"h-4 w-full mb-4",style:{minHeight:"16px"}}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx(E,{className:"h-4 w-16",style:{minHeight:"16px"}}),e.jsx(E,{className:"h-4 w-20",style:{minHeight:"16px"}})]})]})]},r))}
1):j?e.jsxs("div",{className:"text-center py-12",children:[e.jsxs("p",{className:"text-red-600 mb-4",children:["Error loading products: ",j]}),e.jsx(w,{onClick:()=>window.location.reload(),children:"Retry"})]}):c.length===0?e.jsxs("div",{className:"text-center py-12",children:[e.jsx("p",{className:"text-muted-foreground mb-4",children:"No products found matching your criteria"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Try adjusting your filters to see more products"})]}):e.jsx("div",{className:"grid md:grid-cols-2 lg:grid-cols-3 gap-6",children:c.map(a=>e.jsxs(C,{className:"overflow-hidden hover:shadow-lg transition-shadow",children:[e.jsxs("div",{className:"relative",children:[e.jsx("img",{src:a.image_url||"https://images.unsplash.com/photo-1560472354-b33ff0c44a43?w=400&h=300&fit=crop",alt:a.name,className:"w-full h-48 object-cover",style:{minHeight:"192px",aspectRatio:"16/9"},loading:"lazy"}),e.jsx("div",{className:"absolute top-4 right-4",children:e.jsxs(S,{variant:"secondary",className:"bg-green-100 text-green-800",children:[a.commission_rate,"% commission"]})}),a.category&&e.jsx("div",{className:"absolute top-4 left-4",children:e.jsx(S,{variant:"outline",className:"bg-background/90",children:a.category})})]}),e.jsxs(T,{className:"p-4",children:[e.jsx("h3",{className:"font-semibold text-lg mb-2 line-clamp-2",children:a.name}),e.jsx("p",{className:"text-muted-foreground text-sm mb-3 line-clamp-2",children:a.description||"High-quality product with great affiliate potential"}),e.jsxs("div",{className:"flex justify-between items-center mb-3",children:[e.jsxs("span",{className:"text-2xl font-bold text-primary",children:["$",a.price]}),e.jsxs("span",{className:"text-sm text-muted-foreground",children:["by ",a.merchant_name||"Merchant"]})]}),e.jsxs("div",{className:"flex justify-between text-sm text-muted-foreground mb-4",children:[e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx(I,{className:"h-4 w-4"}),e.jsxs("span",{children:[a.clicks||0," clicks"]})]}),e.jsxs("div",{className:"flex items-center space-x-1",children:[e.jsx(se,{className:"h-4 w-4"}),e.jsxs("span",{children:[a.conversions||0," sales"]})]})]})]}),e.jsxs(W,{className:"p-4 pt-0 space-y-2",children:[e.jsx(w,{onClick:()=>x(a.id),disabled:u===a.id,className:"w-full",children:u===a.id?"Generating...":"Generate My Link"}),e.jsxs("div",{className:"flex space-x-2 w-full",children:[e.jsxs(w,{variant:"outline",size:"sm",onClick:()=>d(`${window.location.origin}/products/${a.id}`),className:"flex-1",children:[e.jsx(ae,{className:"h-4 w-4 mr-1"}),"Copy Product Link"]}),e.jsxs(w,{variant:"outline",size:"sm",onClick:()=>window.open(`/products/${a.id}`,"_blank"),className:"flex-1",children:[e.jsx(re,{className:"h-4 w-4 mr-1"}),"View Product"]})]})]})]},a.id))})}function ge(){const{user:c}=D(),{toast:o}=V(),[j,x]=m.useState([]),[u,f]=m.useState(!0),[d,a]=m.useState(null),[r,h]=m.useState(null);m.useEffect(()=>{v()},[]);const v=async()=>{try{f(!0),a(null);const{data:s,error:l}=await y.from("products").select(` 2 *, 3 profiles!inner(name) 4 `).eq("status","active").order("commission_rate",{ascending:!1});if(l)throw console.error("Products fetch error:",l),l;const{data:b,error:i}=await y.from("vendor_products").select(` 5 *, 6 profiles!vendor_products_vendor_id_fkey(name) 7 `).eq("is_active",!0).order("commission_rate",{ascending:!1});i&&console.error("Vendor products fetch error:",i);const{data:p}=await y.from("affiliate_links").select("product_id, clicks, conversions").not("product_id","is",null),_=new Map;p==null||p.forEach(n=>{const k=_.get(n.product_id)||{clicks:0,conversions:0};_.set(n.product_id,{clicks:k.clicks+(n.clicks||0),conversions:k.conversions+(n.conversions||0)})});const H=(s==null?void 0:s.map(n=>{var k,P,L;return{...n,merchant_name:((k=n.profiles)==null?void 0:k.name)||"Unknown Merchant",clicks:((P=_.get(n.id))==null?void 0:P.clicks)||0,conversions:((L=_.get(n.id))==null?void 0:L.conversions)||0,source:"platform"}}))||[],t=(b==null?void 0:b.map(n=>{var P,L,R;const k=typeof n.metadata=="object"&&n.metadata!==null?n.metadata:{};return{id:n.id,name:n.name,description:n.description,price:n.price,commission_rate:n.commission_rate,image_url:n.image_url,category:k.category||null,created_at:n.created_at,merchant_name:((P=n.profiles)==null?void 0:P.name)||"Vendor",clicks:((L=_.get(n.id))==null?void 0:L.clicks)||0,conversions:((R=_.get(n.id))==null?void 0:R.conversions)||0,source:"vendor"}}))||[],Z=[...H,...t];x(Z)}catch(s){console.error("Error fetching products:",s),a(s.message)}finally{f(!1)}};
7return{products:j,loading:u,error:d,generateAffiliateLink:async s=>{if(!(c!=null&&c.id)){o({title:"Authentication Required",description:"Please login to generate affiliate links",variant:"destructive"});return}try{h(s);const l=`${c.id.slice(0,8)}-${s.slice(0,8)}-${Date.now().toString(36)}`,{data:b,error:i}=await y.from("affiliate_links").insert({affiliate_id:c.id,product_id:s,link_code:l,approval_status:"approved"}).select().single();if(i)throw console.error("Affiliate link creation error:",i),i;const p=`${window.location.origin}/ref/${l}`;await navigator.clipboard.writeText(p),o({title:"Affiliate Link Generated!",description:"Your unique affiliate link has been copied to clipboard"})}catch(l){console.error("Error generating affiliate link:",l),o({title:"Error",description:l.message||"Failed to generate affiliate link",variant:"destructive"})}finally{h(null)}},generatingLinkId:r,refetch:v}}function pe(){const[c,o]=m.useState([]),[j,x]=m.useState(!0),[u,f]=m.useState(null);m.useEffect(()=>{d()},[]);const d=async()=>{try{x(!0),f(null);const{data:{user:r},error:h}=await y.auth.getUser();if(h||!r){o([]),x(!1);return}const{data:v,error:g}=await y.from("external_affiliate_products").select("id, product_title, product_description, product_price, commission_rate, product_image_url").eq("admin_approved",!0).limit(20);let s=[],l=null;try{const t=await y.from("products").select("*").eq("is_active",!0).limit(20);s=t.data||[],l=t.error}catch(t){l=t}const{data:b,error:i}=await y.from("affiliate_links").select("product_id").eq("affiliate_id",r.id);g&&console.error("External products error:",g),l&&console.error("Regular products error:",l),i&&console.error("Links error:",i);const p=[...(v||[]).map(t=>({id:t.id,name:t.product_title||"Unnamed Product",description:t.product_description||"No description available",price:Number(t.product_price)||0,commission_rate:Number(t.commission_rate)||0,image_url:t.product_image_url||null,category:"General",sustainability_score:Math.floor(Math.random()*3)+8,performance:{clicks:0,conversions:0,revenue:0},saved:!1})),...(s||[]).map(t=>({id:String(t.id),name:String(t.name||"Unnamed Product"),description:String(t.description||"No description available"),price:Number(t.price)||0,commission_rate:Number(t.commission_rate)||10,image_url:t.image_url||null,category:String(t.category||"General"),sustainability_score:Math.floor(Math.random()*3)+8,performance:{clicks:0,conversions:0,revenue:0},saved:!1}))],_=new Set((b||[]).map(t=>t.product_id)),H=p.map(t=>({...t,saved:_.has(t.id)}));o(H)}catch(r){console.error("Error fetching product recommendations:",r),f(r instanceof Error?r.message:"Failed to fetch recommendations")}finally{x(!1)}};return{recommendations:c,loading:j,error:u,toggleSaved:async r=>{try{const{data:{user:h}}=await y.auth.getUser();if(!h)return;const v=c.find(g=>g.id===r);if(!v)return;if(v.saved)await y.from("affiliate_links").delete().eq("affiliate_id",h.id).eq("product_id",r);else{const g=`${h.id.slice(0,8)}-${r.slice(0,8)}-${Date.now()}`;await y.from("affiliate_links").insert({affiliate_id:h.id,product_id:r,link_code:g})}o(g=>g.map(s=>s.id===r?{...s,saved:!s.saved}:s))}catch(h){console.error("Error toggling saved product:",h)}},refetch:d}}function je(){const{recommendations:c,loading:o,error:j,toggleSaved:x}=pe(),[u,f]=m.useState(""),[d,a]=m.useState("all"),[r,h]=m.useState(!1),v=["all","Lifestyle","Tech","Home","Health"];if(o)return e.jsx("div",{className:"animate-pulse",children:"Loading product recommendations..."});if(j)return e.jsxs("div",{className:"text-destructive",children:["Error loading recommendations: ",j]});const g=c.filter(s=>{const l=s.name.toLowerCase().includes(u.toLowerCase())||s.description.toLowerCase().includes(u.toLowerCase()),b=d==="all"||s.category===d,i=!r||s.saved;return l&&b&&i});return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(C,{children:[e.jsxs(Y,{children:[e.jsxs(J,{className:"flex items-center gap-2",children:[e.jsx(ie,{className:"h-5 w-5 text-primary"}),"AI-Powered Product Recommendations"]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Personalized product suggestions based on your audience and performance data"})]}),e.jsxs(T,{className:"space-y-4",children:[e.jsxs("div",{className:"flex flex-col md:flex-row gap-4",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx(te,{htmlFor:"search",children:"Search Products"}),e.jsxs("div",{className:"relative",children:[e.jsx(A,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(O,{id:"search",placeholder:"Search by name or description...",value:u,onChange:s=>
7f(s.target.value),className:"pl-10"})]})]}),e.jsx("div",{className:"flex gap-2",children:e.jsxs(w,{variant:r?"default":"outline",size:"sm",onClick:()=>h(!r),className:"flex items-center gap-1",children:[e.jsx(M,{className:`h-4 w-4 ${r?"fill-current":""}`}),"Saved Only"]})})]}),e.jsx("div",{className:"flex gap-2 flex-wrap",children:v.map(s=>e.jsx(w,{variant:d===s?"default":"outline",size:"sm",onClick:()=>a(s),children:s.charAt(0).toUpperCase()+s.slice(1)},s))})]})]}),e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:e.jsx(ne,{children:g.map(s=>e.jsx(le.div,{initial:{opacity:0,y:20},animate:{opacity:1,y:0},exit:{opacity:0,y:-20},transition:{duration:.3},children:e.jsxs(C,{className:"group hover:shadow-lg transition-all duration-300 hover:-translate-y-1",children:[e.jsxs("div",{className:"relative",children:[e.jsx("img",{src:s.image_url,alt:s.name,className:"w-full h-48 object-cover rounded-t-lg"}),e.jsx(w,{variant:"ghost",size:"sm",className:"absolute top-2 right-2 bg-white/80 hover:bg-white",onClick:()=>x(s.id),children:e.jsx(M,{className:`h-4 w-4 ${s.saved?"fill-current text-red-500":""}`})}),e.jsx("div",{className:"absolute bottom-2 left-2",children:e.jsxs(S,{className:"bg-green-100 text-green-800",children:[e.jsx(ce,{className:"h-3 w-3 mr-1"}),s.sustainability_score,"/10"]})})]}),e.jsxs(T,{className:"p-4 space-y-3",children:[e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold line-clamp-1",children:s.name}),e.jsx("p",{className:"text-sm text-muted-foreground line-clamp-2",children:s.description})]}),e.jsxs("div",{className:"flex flex-wrap gap-1",children:[e.jsxs(S,{variant:"outline",className:"text-xs bg-primary/10 text-primary",children:[s.commission_rate,"% Commission"]}),s.sustainability_score&&s.sustainability_score>8&&e.jsx(S,{variant:"outline",className:"text-xs bg-green-100 text-green-800",children:"Eco-Friendly"})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsxs("span",{className:"text-lg font-bold",children:["$",s.price]}),e.jsxs(S,{variant:"secondary",className:"bg-yellow-100 text-yellow-800",children:[s.commission_rate,"% commission"]})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-2 text-center text-xs text-muted-foreground",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:s.performance.clicks}),e.jsx("div",{children:"Clicks"})]}),e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:s.performance.conversions}),e.jsx("div",{children:"Sales"})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"font-medium",children:["$",s.performance.revenue]}),e.jsx("div",{children:"Revenue"})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(w,{size:"sm",className:"flex-1",children:[e.jsx(oe,{className:"h-4 w-4 mr-1"}),"Get Link"]}),e.jsx(w,{variant:"outline",size:"sm",children:e.jsx(de,{className:"h-4 w-4"})})]})]})]})},s.id))})}),g.length===0&&e.jsx(C,{children:e.jsxs(T,{className:"p-8 text-center",children:[e.jsx(A,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium mb-2",children:"No products found"}),e.jsx("p",{className:"text-muted-foreground",children:"Try adjusting your search criteria or browse all categories"})]})})]})}function We(){const{isAuthenticated:c}=D(),[o,j]=m.useState(""),[x,u]=m.useState("all"),[f,d]=m.useState("commission"),[a,r]=m.useState([0,50]),{products:h,loading:v,error:g,generateAffiliateLink:s,generatingLinkId:l}=ge();if(!c)return e.jsx($,{children:e.jsxs("div",{className:"container mx-auto px-4 py-12 text-center",children:[e.jsx("h1",{className:"text-3xl font-bold mb-4",children:"Affiliate Product Hub"}),e.jsx("p",{className:"text-muted-foreground mb-8",children:"Please login to access the product discovery hub"})]})});const b=h.filter(i=>{var p;return i.name.toLowerCase().includes(o.toLowerCase())||((p=i.description)==null?void 0:p.toLowerCase().includes(o.toLowerCase()))}).filter(i=>x==="all"||i.category===x).filter(i=>i.commission_rate>=a[0]&&i.commission_rate<=a[1]).sort((i,p)=>{switch(f){case"commission":return p.commission_rate-i.commission_rate;case"popularity":return(p.clicks||0)-(i.clicks||0);case"newest":return new Date(p.created_at).getTime()-new Date(i.created_at).getTime();default:return 0}});return e.jsx($,{children:e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(he,{}),e.jsx("div",{className:"container mx-auto px-4 py-8",children:e.jsxs(me,{defaultValue:"discover",className:"mb-8",children:[e.jsxs(xe,{className:"grid w-full grid-cols-2",children:[e.jsx(z,{value:"discover",children:"Product Discovery"}),e.jsx(z,{value:"recommendations",children:"AI Recommendations"})]}),e.jsxs(B,{value:"discover",className:"space-y-6",children:[e.jsx(ue,{searchTerm:o,setSearchTerm:j,category:x,setCategory:u,sortBy:f,setSortBy:d,commissionRange:a,setCommissionRange:r}),e.jsx(fe,{products:b,loading:v,error:g,onGenerateLink:s,generatingLinkId:l})]}),e.jsx(B,{value:"recommendations",children:e.jsx(je,{})})]})})]})})}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.