(globalThis.TURBOPACK||(globalThis.TURBOPACK=[])).push(["object"==typeof document?document.currentScript:void 0,83353,e=>{"use strict";var a=e.i(18566),t=e.i(2169);e.s(["useAppRouter",0,function(){let e=(0,a.useRouter)(),{startLoading:s}=(0,t.useLoader)();return{...e,push:(a,t)=>{console.log("[Loader] Navigation started:",a),s&&s(),e.push(a,t)},replace:(a,t)=>{console.log("[Loader] Navigation started:",a),s&&s(),e.replace(a,t)}}}])},77071,e=>{"use strict";let a=(0,e.i(56420).default)("plus",[["path",{d:"M5 12h14",key:"1ays0h"}],["path",{d:"M12 5v14",key:"s699le"}]]);e.s(["Plus",0,a],77071)},12498,e=>{"use strict";var a=e.i(65464);let t=new class{async fetchCoupons(e){let{data:t,error:s}=await a.supabase.from("coupons").select("*").eq("vendor_id",e).order("created_at",{ascending:!1});if(s)throw console.error("Error fetching coupons:",s),s;return t||[]}async fetchCouponDetails(e){let{data:t,error:s}=await a.supabase.from("coupons").select("*").eq("id",e).single();if(s)throw s;let o=[],i=[];if("category"===t.apply_type){let{data:t}=await a.supabase.from("coupon_categories").select(`
          category_id,
          categories (
            name
          )
        `).eq("coupon_id",e);o=t||[]}else if("product"===t.apply_type){let{data:t}=await a.supabase.from("coupon_products").select(`
          product_id,
          products (
            product_name
          )
        `).eq("coupon_id",e);i=t||[]}return{...t,selectedCategories:o,selectedProducts:i}}async createCoupon(e,t){let{data:s,error:o}=await a.supabase.from("coupons").insert([e]).select().single();if(o){if("23505"===o.code)throw Error("This coupon code has already been created. Please try a different code or generate a new one.");throw o}let i=s.id;if("category"===e.apply_type&&t.categories?.length>0){let e=t.categories.map(e=>({coupon_id:i,category_id:e}));await a.supabase.from("coupon_categories").insert(e)}else if("product"===e.apply_type&&t.products?.length>0){let e=t.products.map(e=>({coupon_id:i,product_id:e}));await a.supabase.from("coupon_products").insert(e)}return s}async updateCouponStatus(e,t){let{data:s,error:o}=await a.supabase.from("coupons").update({is_active:t}).eq("id",e).select().single();if(o)throw o;return s}async deleteCoupon(e){let{error:t}=await a.supabase.from("coupons").delete().eq("id",e);if(t)throw t;return!0}async fetchVendorProducts(e){let{data:t,error:s}=await a.supabase.from("products").select("id, product_name, category_id").eq("vendor_id",e).order("product_name");return s?(console.warn("Failed to fetch products:",s.message),[]):t||[]}async fetchCategories(){let{data:e,error:t}=await a.supabase.from("categories").select("id, name").order("name");return t?(console.warn("Failed to fetch categories:",t.message),[]):e||[]}async validateCoupon(e,t,s){let{subtotal:o,products:i}=t,{data:l,error:n}=await a.supabase.from("coupons").select("*").eq("code",e).single();if(n||!l)return{isValid:!1,error:"Invalid coupon code"};if(!l.is_active)return{isValid:!1,error:"This coupon is no longer active"};let r=new Date;if(new Date(l.start_date)>r)return{isValid:!1,error:"This coupon is not yet valid"};if(new Date(l.end_date)<r)return{isValid:!1,error:"This coupon has expired"};if(l.usage_limit>0&&l.used_count>=l.usage_limit)return{isValid:!1,error:"Coupon usage limit has been reached"};if(s){let{count:e}=await a.supabase.from("coupon_usages").select("*",{count:"exact",head:!0}).eq("coupon_id",l.id).eq("user_id",s);if(e>=l.per_user_limit)return{isValid:!1,error:"You have reached your usage limit for this coupon"}}if(l.min_order_amount&&o<l.min_order_amount)return{isValid:!1,error:`Minimum order amount is ₹${l.min_order_amount}`};let c=!1,d=0;if("all"===l.apply_type)d=o,c=!0;else if("category"===l.apply_type){let{data:e}=await a.supabase.from("coupon_categories").select("category_id").eq("coupon_id",l.id),t=e.map(e=>e.category_id);for(let e of i)t.includes(e.category_id)&&(d+=e.price*e.quantity,c=!0)}else if("product"===l.apply_type){let{data:e}=await a.supabase.from("coupon_products").select("product_id").eq("coupon_id",l.id),t=e.map(e=>e.product_id);for(let e of i)t.includes(e.id)&&(d+=e.price*e.quantity,c=!0)}if(!c)return{isValid:!1,error:"Your cart does not contain any items eligible for this coupon."};let u=0;return"percentage"===l.type?(u=d*(l.value/100),l.max_discount&&u>l.max_discount&&(u=l.max_discount)):"fixed"===l.type&&(u=l.value),u=Math.min(u,d),{isValid:!0,couponId:l.id,discountAmount:u}}};e.s(["couponsRepository",0,t])},86682,e=>{e.v({actionLink:"coupons-module__6TITaa__actionLink",actionLinks:"coupons-module__6TITaa__actionLinks",actionsBar:"coupons-module__6TITaa__actionsBar",analyticsCard:"coupons-module__6TITaa__analyticsCard",analyticsGrid:"coupons-module__6TITaa__analyticsGrid",cardTitle:"coupons-module__6TITaa__cardTitle",cardValue:"coupons-module__6TITaa__cardValue",container:"coupons-module__6TITaa__container",deleteLink:"coupons-module__6TITaa__deleteLink",errorText:"coupons-module__6TITaa__errorText",formControl:"coupons-module__6TITaa__formControl",formGrid:"coupons-module__6TITaa__formGrid",formGroup:"coupons-module__6TITaa__formGroup",formLabel:"coupons-module__6TITaa__formLabel",formSection:"coupons-module__6TITaa__formSection",header:"coupons-module__6TITaa__header",inputAddon:"coupons-module__6TITaa__inputAddon",inputWithAddon:"coupons-module__6TITaa__inputWithAddon",pageButton:"coupons-module__6TITaa__pageButton",pageControls:"coupons-module__6TITaa__pageControls",pagination:"coupons-module__6TITaa__pagination",primaryButton:"coupons-module__6TITaa__primaryButton",searchInput:"coupons-module__6TITaa__searchInput",secondaryButton:"coupons-module__6TITaa__secondaryButton",sectionTitle:"coupons-module__6TITaa__sectionTitle",selectInput:"coupons-module__6TITaa__selectInput",statusActive:"coupons-module__6TITaa__statusActive",statusBadge:"coupons-module__6TITaa__statusBadge",statusExpired:"coupons-module__6TITaa__statusExpired",statusInactive:"coupons-module__6TITaa__statusInactive",table:"coupons-module__6TITaa__table",tableContainer:"coupons-module__6TITaa__tableContainer",title:"coupons-module__6TITaa__title"})},87550,e=>{"use strict";var a=e.i(43476),t=e.i(71645),s=e.i(77071),o=e.i(66595),i=e.i(80799),l=e.i(66794),n=e.i(56420);let r=(0,n.default)("activity",[["path",{d:"M22 12h-2.48a2 2 0 0 0-1.93 1.46l-2.35 8.36a.25.25 0 0 1-.48 0L9.24 2.18a.25.25 0 0 0-.48 0l-2.35 8.36A2 2 0 0 1 4.49 12H2",key:"169zse"}]]);var c=e.i(48161);let d=(0,n.default)("circle-alert",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["line",{x1:"12",x2:"12",y1:"8",y2:"12",key:"1pkeuh"}],["line",{x1:"12",x2:"12.01",y1:"16",y2:"16",key:"4dfq90"}]]);var u=e.i(83353),p=e.i(12498),_=e.i(65464),h=e.i(86682),m=e.i(44824);e.s(["default",0,function(){let e=(0,u.useAppRouter)(),[n,f]=(0,t.useState)(!1);(0,t.useEffect)(()=>{f(!0)},[]);let{data:g,analytics:y,loading:x,error:v,filters:T,pagination:j,actions:w}=function(){let[e,a]=(0,t.useState)([]),[s,o]=(0,t.useState)(!0),[i,l]=(0,t.useState)(null),[n,r]=(0,t.useState)(""),[c,d]=(0,t.useState)("all"),[u,h]=(0,t.useState)("all"),[m,f]=(0,t.useState)("all"),[g,y]=(0,t.useState)(1);(0,t.useEffect)(()=>{x()},[]);let x=async()=>{try{o(!0);let{data:{user:e}}=await _.supabase.auth.getUser();if(!e)throw Error("Not authenticated");let{data:t}=await _.supabase.from("vendor_profiles").select("id").eq("user_id",e.id).single();if(!t)throw Error("Vendor profile not found");let s=await p.couponsRepository.fetchCoupons(t.id);a(s),l(null)}catch(e){console.warn("Failed to load coupons:",e.message),l(`Failed to load coupons: ${e.message}`),a([])}finally{o(!1)}},v=async e=>{try{return await p.couponsRepository.deleteCoupon(e),a(a=>a.filter(a=>a.id!==e)),!0}catch(e){throw console.warn("Failed to delete coupon:",e.message),e}},T=async(e,t)=>{try{let s=!t;return await p.couponsRepository.updateCouponStatus(e,s),a(a=>a.map(a=>a.id===e?{...a,is_active:s}:a)),s}catch(e){throw console.warn("Failed to toggle status:",e.message),e}},j=(0,t.useMemo)(()=>e.filter(e=>{let a=e.code?.toLowerCase().includes(n.toLowerCase()),t=e.is_active,s=new Date(e.end_date)<new Date,o=!0;"active"===c&&(o=t&&!s),"inactive"===c&&(o=!t),"expired"===c&&(o=s);let i=!0;"all"!==u&&(i=e.type===u);let l=!0;return"all"!==m&&(l=e.apply_type===m),a&&o&&i&&l}),[e,n,c,u,m]),w=Math.ceil(j.length/10);return{data:j.slice((g-1)*10,10*g),analytics:(0,t.useMemo)(()=>{let a=e.length,t=0,s=0,o=0,i=0,l=new Date;return e.forEach(e=>{e.is_active&&new Date(e.end_date)>l?t++:new Date(e.end_date)<l&&s++,o+=e.used_count||0,i+=e.total_discount_given||0}),{total:a,active:t,expired:s,redemptions:o,totalDiscount:i}},[e]),loading:s,error:i,filters:{searchQuery:n,setSearchQuery:r,statusFilter:c,setStatusFilter:d,typeFilter:u,setTypeFilter:h,applyScopeFilter:m,setApplyScopeFilter:f},pagination:{currentPage:g,setCurrentPage:y,totalPages:w},actions:{deleteCoupon:v,toggleStatus:T,refresh:x}}}();if(!n)return null;if(x&&0===g.length)return(0,a.jsx)("div",{className:h.default.container,style:{display:"flex",justifyContent:"center",alignItems:"center",minHeight:"60vh"},children:(0,a.jsx)(m.default,{text:"Loading Coupons..."})});let C=async e=>{window.confirm("Are you sure you want to delete this coupon? This action cannot be undone.")&&await w.deleteCoupon(e)};return(0,a.jsxs)("div",{className:h.default.container,children:[(0,a.jsxs)("div",{className:h.default.header,children:[(0,a.jsxs)("div",{children:[(0,a.jsx)("h1",{className:h.default.title,children:"Coupons"}),(0,a.jsx)("p",{style:{color:"var(--color-text-secondary)",marginTop:"4px"},children:"Manage discounts, deals, and promotional campaigns."})]}),(0,a.jsxs)("button",{className:h.default.primaryButton,onClick:()=>e.push("/coupons/create"),children:[(0,a.jsx)(s.Plus,{size:18}),"Create Coupon"]})]}),v&&(0,a.jsx)("div",{className:h.default.errorText,children:v}),(0,a.jsxs)("div",{className:h.default.analyticsGrid,children:[(0,a.jsxs)("div",{className:h.default.analyticsCard,children:[(0,a.jsxs)("div",{className:h.default.cardTitle,children:[(0,a.jsx)(i.Tag,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Coupons"]}),(0,a.jsx)("div",{className:h.default.cardValue,children:y.total})]}),(0,a.jsxs)("div",{className:h.default.analyticsCard,children:[(0,a.jsxs)("div",{className:h.default.cardTitle,children:[(0,a.jsx)(c.CheckCircle,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle",color:"var(--color-success)"}}),"Active Coupons"]}),(0,a.jsx)("div",{className:h.default.cardValue,children:y.active})]}),(0,a.jsxs)("div",{className:h.default.analyticsCard,children:[(0,a.jsxs)("div",{className:h.default.cardTitle,children:[(0,a.jsx)(d,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle",color:"var(--color-danger)"}}),"Expired Coupons"]}),(0,a.jsx)("div",{className:h.default.cardValue,children:y.expired})]}),(0,a.jsxs)("div",{className:h.default.analyticsCard,children:[(0,a.jsxs)("div",{className:h.default.cardTitle,children:[(0,a.jsx)(r,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Redemptions"]}),(0,a.jsx)("div",{className:h.default.cardValue,children:y.redemptions})]}),(0,a.jsxs)("div",{className:h.default.analyticsCard,children:[(0,a.jsxs)("div",{className:h.default.cardTitle,children:[(0,a.jsx)(l.Settings,{size:16,style:{display:"inline",marginRight:8,verticalAlign:"middle"}}),"Total Discount Given"]}),(0,a.jsxs)("div",{className:h.default.cardValue,children:["₹",y.totalDiscount]})]})]}),(0,a.jsxs)("div",{className:h.default.actionsBar,children:[(0,a.jsxs)("div",{style:{position:"relative",flex:1,minWidth:"250px"},children:[(0,a.jsx)(o.Search,{size:18,style:{position:"absolute",left:12,top:12,color:"var(--color-text-secondary)"}}),(0,a.jsx)("input",{type:"text",placeholder:"Search by coupon code...",className:h.default.searchInput,style:{paddingLeft:40,width:"100%"},value:T.searchQuery,onChange:e=>T.setSearchQuery(e.target.value)})]}),(0,a.jsxs)("select",{className:h.default.selectInput,value:T.statusFilter,onChange:e=>T.setStatusFilter(e.target.value),children:[(0,a.jsx)("option",{value:"all",children:"All Status"}),(0,a.jsx)("option",{value:"active",children:"Active"}),(0,a.jsx)("option",{value:"inactive",children:"Inactive"}),(0,a.jsx)("option",{value:"expired",children:"Expired"})]}),(0,a.jsxs)("select",{className:h.default.selectInput,value:T.typeFilter,onChange:e=>T.setTypeFilter(e.target.value),children:[(0,a.jsx)("option",{value:"all",children:"All Types"}),(0,a.jsx)("option",{value:"percentage",children:"Percentage"}),(0,a.jsx)("option",{value:"fixed",children:"Fixed Amount"})]}),(0,a.jsxs)("select",{className:h.default.selectInput,value:T.applyScopeFilter,onChange:e=>T.setApplyScopeFilter(e.target.value),children:[(0,a.jsx)("option",{value:"all",children:"All Scopes"}),(0,a.jsx)("option",{value:"all_products",children:"Entire Store"}),(0,a.jsx)("option",{value:"category",children:"Categories"}),(0,a.jsx)("option",{value:"product",children:"Specific Products"})]})]}),(0,a.jsxs)("div",{className:h.default.tableContainer,children:[(0,a.jsxs)("table",{className:h.default.table,children:[(0,a.jsx)("thead",{children:(0,a.jsxs)("tr",{children:[(0,a.jsx)("th",{children:"CODE"}),(0,a.jsx)("th",{children:"TYPE"}),(0,a.jsx)("th",{children:"DISCOUNT"}),(0,a.jsx)("th",{children:"USAGE"}),(0,a.jsx)("th",{children:"VALIDITY"}),(0,a.jsx)("th",{children:"STATUS"}),(0,a.jsx)("th",{children:"ACTIONS"})]})}),(0,a.jsx)("tbody",{children:0===g.length?(0,a.jsx)("tr",{children:(0,a.jsx)("td",{colSpan:"7",style:{textAlign:"center",padding:"32px"},children:(0,a.jsx)("p",{style:{color:"var(--color-text-secondary)"},children:"No coupons found matching your criteria."})})}):g.map(t=>{let s=new Date(t.end_date)<new Date,o=h.default.statusInactive,i="Inactive";return t.is_active&&!s?(o=h.default.statusActive,i="Active"):s&&(o=h.default.statusExpired,i="Expired"),(0,a.jsxs)("tr",{children:[(0,a.jsx)("td",{children:(0,a.jsx)("strong",{children:t.code})}),(0,a.jsx)("td",{style:{textTransform:"capitalize"},children:t.type}),(0,a.jsx)("td",{children:"percentage"===t.type?`${t.value}%`:`₹${t.value}`}),(0,a.jsxs)("td",{children:[t.used_count||0," / ",0===t.usage_limit?"∞":t.usage_limit]}),(0,a.jsxs)("td",{children:[new Date(t.start_date).toLocaleDateString()," - ",(0,a.jsx)("br",{}),new Date(t.end_date).toLocaleDateString()]}),(0,a.jsx)("td",{children:(0,a.jsx)("span",{className:`${h.default.statusBadge} ${o}`,children:i})}),(0,a.jsx)("td",{children:(0,a.jsxs)("div",{className:h.default.actionLinks,children:[(0,a.jsx)("button",{className:h.default.actionLink,onClick:()=>e.push(`/coupons/${t.id}`),children:"Edit"}),(0,a.jsx)("button",{className:h.default.actionLink,onClick:()=>w.toggleStatus(t.id,t.is_active),children:t.is_active?"Deactivate":"Activate"}),(0,a.jsx)("button",{className:`${h.default.actionLink} ${h.default.deleteLink}`,onClick:()=>C(t.id),children:"Delete"})]})})]},t.id)})})]}),j.totalPages>1&&(0,a.jsxs)("div",{className:h.default.pagination,children:[(0,a.jsxs)("span",{style:{color:"var(--color-text-secondary)",fontSize:14},children:["Showing Page ",j.currentPage," of ",j.totalPages]}),(0,a.jsxs)("div",{className:h.default.pageControls,children:[(0,a.jsx)("button",{className:h.default.pageButton,disabled:1===j.currentPage,onClick:()=>j.setCurrentPage(e=>e-1),children:"Previous"}),(0,a.jsx)("button",{className:h.default.pageButton,disabled:j.currentPage===j.totalPages,onClick:()=>j.setCurrentPage(e=>e+1),children:"Next"})]})]})]})]})}],87550)}]);