module.exports=[19107,a=>{"use strict";let b=(0,a.i(64831).default)("arrow-left",[["path",{d:"m12 19-7-7 7-7",key:"1l729n"}],["path",{d:"M19 12H5",key:"x3x0zl"}]]);a.s(["ArrowLeft",0,b],19107)},91603,a=>{"use strict";var b=a.i(50944),c=a.i(3629);a.s(["useAppRouter",0,function(){let a=(0,b.useRouter)(),{startLoading:d}=(0,c.useLoader)();return{...a,push:(b,c)=>{console.log("[Loader] Navigation started:",b),d&&d(),a.push(b,c)},replace:(b,c)=>{console.log("[Loader] Navigation started:",b),d&&d(),a.replace(b,c)}}}])},80180,a=>{"use strict";let b=(0,a.i(64831).default)("trash-2",[["path",{d:"M10 11v6",key:"nco0om"}],["path",{d:"M14 11v6",key:"outv1u"}],["path",{d:"M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6",key:"miytrc"}],["path",{d:"M3 6h18",key:"d0wm0j"}],["path",{d:"M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2",key:"e791ji"}]]);a.s(["Trash2",0,b],80180)},40315,a=>{"use strict";var b=a.i(29562);let c=new class{async fetchCoupons(a){let{data:c,error:d}=await b.supabase.from("coupons").select("*").eq("vendor_id",a).order("created_at",{ascending:!1});if(d)throw console.error("Error fetching coupons:",d),d;return c||[]}async fetchCouponDetails(a){let{data:c,error:d}=await b.supabase.from("coupons").select("*").eq("id",a).single();if(d)throw d;let e=[],f=[];if("category"===c.apply_type){let{data:c}=await b.supabase.from("coupon_categories").select(`
          category_id,
          categories (
            name
          )
        `).eq("coupon_id",a);e=c||[]}else if("product"===c.apply_type){let{data:c}=await b.supabase.from("coupon_products").select(`
          product_id,
          products (
            product_name
          )
        `).eq("coupon_id",a);f=c||[]}return{...c,selectedCategories:e,selectedProducts:f}}async createCoupon(a,c){let{data:d,error:e}=await b.supabase.from("coupons").insert([a]).select().single();if(e){if("23505"===e.code)throw Error("This coupon code has already been created. Please try a different code or generate a new one.");throw e}let f=d.id;if("category"===a.apply_type&&c.categories?.length>0){let a=c.categories.map(a=>({coupon_id:f,category_id:a}));await b.supabase.from("coupon_categories").insert(a)}else if("product"===a.apply_type&&c.products?.length>0){let a=c.products.map(a=>({coupon_id:f,product_id:a}));await b.supabase.from("coupon_products").insert(a)}return d}async updateCouponStatus(a,c){let{data:d,error:e}=await b.supabase.from("coupons").update({is_active:c}).eq("id",a).select().single();if(e)throw e;return d}async deleteCoupon(a){let{error:c}=await b.supabase.from("coupons").delete().eq("id",a);if(c)throw c;return!0}async fetchVendorProducts(a){let{data:c,error:d}=await b.supabase.from("products").select("id, product_name, category_id").eq("vendor_id",a).order("product_name");return d?(console.warn("Failed to fetch products:",d.message),[]):c||[]}async fetchCategories(){let{data:a,error:c}=await b.supabase.from("categories").select("id, name").order("name");return c?(console.warn("Failed to fetch categories:",c.message),[]):a||[]}async validateCoupon(a,c,d){let{subtotal:e,products:f}=c,{data:g,error:h}=await b.supabase.from("coupons").select("*").eq("code",a).single();if(h||!g)return{isValid:!1,error:"Invalid coupon code"};if(!g.is_active)return{isValid:!1,error:"This coupon is no longer active"};let i=new Date;if(new Date(g.start_date)>i)return{isValid:!1,error:"This coupon is not yet valid"};if(new Date(g.end_date)<i)return{isValid:!1,error:"This coupon has expired"};if(g.usage_limit>0&&g.used_count>=g.usage_limit)return{isValid:!1,error:"Coupon usage limit has been reached"};if(d){let{count:a}=await b.supabase.from("coupon_usages").select("*",{count:"exact",head:!0}).eq("coupon_id",g.id).eq("user_id",d);if(a>=g.per_user_limit)return{isValid:!1,error:"You have reached your usage limit for this coupon"}}if(g.min_order_amount&&e<g.min_order_amount)return{isValid:!1,error:`Minimum order amount is ₹${g.min_order_amount}`};let j=!1,k=0;if("all"===g.apply_type)k=e,j=!0;else if("category"===g.apply_type){let{data:a}=await b.supabase.from("coupon_categories").select("category_id").eq("coupon_id",g.id),c=a.map(a=>a.category_id);for(let a of f)c.includes(a.category_id)&&(k+=a.price*a.quantity,j=!0)}else if("product"===g.apply_type){let{data:a}=await b.supabase.from("coupon_products").select("product_id").eq("coupon_id",g.id),c=a.map(a=>a.product_id);for(let a of f)c.includes(a.id)&&(k+=a.price*a.quantity,j=!0)}if(!j)return{isValid:!1,error:"Your cart does not contain any items eligible for this coupon."};let l=0;return"percentage"===g.type?(l=k*(g.value/100),g.max_discount&&l>g.max_discount&&(l=g.max_discount)):"fixed"===g.type&&(l=g.value),l=Math.min(l,k),{isValid:!0,couponId:g.id,discountAmount:l}}};a.s(["couponsRepository",0,c])},45122,a=>{a.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"})},13931,a=>{"use strict";let b=(0,a.i(64831).default)("power",[["path",{d:"M12 2v10",key:"mnfbl"}],["path",{d:"M18.4 6.6a9 9 0 1 1-12.77.04",key:"obofu9"}]]);a.s(["Power",0,b],13931)},97610,a=>{"use strict";var b=a.i(87924),c=a.i(72131),d=a.i(19107),e=a.i(80180),f=a.i(13931),g=a.i(91603),h=a.i(40315),i=a.i(45122);a.s(["default",0,function({id:a}){let j=(0,g.useAppRouter)(),[k,l]=(0,c.useState)(null),[m,n]=(0,c.useState)(!0),[o,p]=(0,c.useState)(null),[q,r]=(0,c.useState)(!1);(0,c.useEffect)(()=>{r(!0),s()},[a]);let s=async()=>{try{n(!0);let b=await h.couponsRepository.fetchCouponDetails(a);l(b)}catch(a){console.error(a),p("Failed to load coupon details.")}finally{n(!1)}},t=async()=>{try{await h.couponsRepository.updateCouponStatus(a,!k.is_active),l({...k,is_active:!k.is_active})}catch(a){alert("Failed to update status")}},u=async()=>{if(window.confirm("Are you sure you want to delete this coupon? This action cannot be undone."))try{await h.couponsRepository.deleteCoupon(a),j.push("/coupons")}catch(a){alert("Failed to delete coupon")}};if(!q)return null;if(m)return(0,b.jsx)("div",{className:i.default.container,children:"Loading details..."});if(o)return(0,b.jsx)("div",{className:i.default.container,children:o});if(!k)return(0,b.jsx)("div",{className:i.default.container,children:"Coupon not found."});let v=new Date(k.end_date)<new Date,w=i.default.statusInactive,x="Inactive";return k.is_active&&!v?(w=i.default.statusActive,x="Active"):v&&(w=i.default.statusExpired,x="Expired"),(0,b.jsxs)("div",{className:i.default.container,children:[(0,b.jsxs)("div",{className:i.default.header,children:[(0,b.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:16},children:[(0,b.jsx)("button",{className:i.default.secondaryButton,style:{padding:"8px"},onClick:()=>j.push("/coupons"),children:(0,b.jsx)(d.ArrowLeft,{size:18})}),(0,b.jsxs)("div",{children:[(0,b.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12},children:[(0,b.jsx)("h1",{className:i.default.title,children:k.code}),(0,b.jsx)("span",{className:`${i.default.statusBadge} ${w}`,children:x})]}),(0,b.jsxs)("p",{style:{color:"var(--color-text-secondary)",marginTop:"4px"},children:["Created on ",new Date(k.created_at).toLocaleDateString()]})]})]}),(0,b.jsxs)("div",{style:{display:"flex",gap:12},children:[(0,b.jsxs)("button",{className:i.default.secondaryButton,onClick:t,children:[(0,b.jsx)(f.Power,{size:18,style:{marginRight:8,display:"inline"}}),k.is_active?"Deactivate":"Activate"]}),(0,b.jsxs)("button",{className:i.default.secondaryButton,onClick:u,style:{color:"var(--color-danger)"},children:[(0,b.jsx)(e.Trash2,{size:18,style:{marginRight:8,display:"inline"}}),"Delete"]})]})]}),(0,b.jsxs)("div",{className:i.default.formGrid,children:[(0,b.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:24},children:[(0,b.jsxs)("div",{className:i.default.formSection,children:[(0,b.jsx)("h2",{className:i.default.sectionTitle,children:"Coupon Details"}),(0,b.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:16},children:[(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Type"}),(0,b.jsx)("p",{className:i.default.formLabel,style:{textTransform:"capitalize"},children:k.type})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Discount Value"}),(0,b.jsx)("p",{className:i.default.formLabel,children:"percentage"===k.type?`${k.value}%`:`₹${k.value}`})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Min Order Amount"}),(0,b.jsx)("p",{className:i.default.formLabel,children:k.min_order_amount?`₹${k.min_order_amount}`:"None"})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Max Discount"}),(0,b.jsx)("p",{className:i.default.formLabel,children:k.max_discount?`₹${k.max_discount}`:"None"})]})]})]}),(0,b.jsxs)("div",{className:i.default.formSection,children:[(0,b.jsx)("h2",{className:i.default.sectionTitle,children:"Schedule & Usage"}),(0,b.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:16},children:[(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Start Date"}),(0,b.jsx)("p",{className:i.default.formLabel,children:new Date(k.start_date).toLocaleString()})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"End Date"}),(0,b.jsx)("p",{className:i.default.formLabel,children:new Date(k.end_date).toLocaleString()})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Usage Limit"}),(0,b.jsx)("p",{className:i.default.formLabel,children:0===k.usage_limit?"Unlimited":k.usage_limit})]}),(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Per User Limit"}),(0,b.jsx)("p",{className:i.default.formLabel,children:k.per_user_limit})]})]})]})]}),(0,b.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:24},children:[(0,b.jsxs)("div",{className:i.default.formSection,children:[(0,b.jsx)("h2",{className:i.default.sectionTitle,children:"Scope"}),(0,b.jsxs)("div",{style:{marginBottom:16},children:[(0,b.jsx)("p",{className:i.default.cardTitle,children:"Apply Type"}),(0,b.jsxs)("p",{className:i.default.formLabel,children:["all"===k.apply_type&&"Entire Store","category"===k.apply_type&&"Specific Categories","product"===k.apply_type&&"Specific Products"]})]}),"category"===k.apply_type&&(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,style:{marginBottom:8},children:"Selected Categories"}),(0,b.jsx)("ul",{style:{paddingLeft:20,color:"var(--color-text-primary)"},children:k.selectedCategories.map(a=>(0,b.jsx)("li",{children:a.categories?.name||a.category_id},a.category_id))})]}),"product"===k.apply_type&&(0,b.jsxs)("div",{children:[(0,b.jsx)("p",{className:i.default.cardTitle,style:{marginBottom:8},children:"Selected Products"}),(0,b.jsx)("ul",{style:{paddingLeft:20,color:"var(--color-text-primary)"},children:k.selectedProducts.map(a=>(0,b.jsx)("li",{children:a.products?.product_name||a.product_id},a.product_id))})]})]}),(0,b.jsxs)("div",{className:i.default.formSection,children:[(0,b.jsx)("h2",{className:i.default.sectionTitle,children:"Redemption Statistics"}),(0,b.jsxs)("div",{className:i.default.analyticsGrid,style:{gridTemplateColumns:"1fr 1fr"},children:[(0,b.jsxs)("div",{className:i.default.analyticsCard,style:{padding:16},children:[(0,b.jsx)("div",{className:i.default.cardTitle,children:"Used Count"}),(0,b.jsx)("div",{className:i.default.cardValue,children:k.used_count||0})]}),(0,b.jsxs)("div",{className:i.default.analyticsCard,style:{padding:16},children:[(0,b.jsx)("div",{className:i.default.cardTitle,children:"Remaining"}),(0,b.jsx)("div",{className:i.default.cardValue,children:0===k.usage_limit?"∞":k.usage_limit-(k.used_count||0)})]})]})]})]})]})]})}])}];

//# sourceMappingURL=_1-s8zn_._.js.map