"use strict";(()=>{var e={};e.id=9146,e.ids=[9146],e.modules={53524:e=>{e.exports=require("@prisma/client")},30517:e=>{e.exports=require("next/dist/compiled/next-server/app-route.runtime.prod.js")},25528:e=>{e.exports=require("next/dist/client/components/action-async-storage.external.js")},91877:e=>{e.exports=require("next/dist/client/components/request-async-storage.external.js")},25319:e=>{e.exports=require("next/dist/client/components/static-generation-async-storage.external.js")},39491:e=>{e.exports=require("assert")},14300:e=>{e.exports=require("buffer")},32081:e=>{e.exports=require("child_process")},6113:e=>{e.exports=require("crypto")},9523:e=>{e.exports=require("dns")},82361:e=>{e.exports=require("events")},57147:e=>{e.exports=require("fs")},13685:e=>{e.exports=require("http")},95687:e=>{e.exports=require("https")},41808:e=>{e.exports=require("net")},22037:e=>{e.exports=require("os")},71017:e=>{e.exports=require("path")},63477:e=>{e.exports=require("querystring")},12781:e=>{e.exports=require("stream")},24404:e=>{e.exports=require("tls")},57310:e=>{e.exports=require("url")},73837:e=>{e.exports=require("util")},59796:e=>{e.exports=require("zlib")},74910:(e,r,t)=>{t.r(r),t.d(r,{headerHooks:()=>g,originalPathname:()=>y,requestAsyncStorage:()=>q,routeModule:()=>x,serverHooks:()=>w,staticGenerationAsyncStorage:()=>m,staticGenerationBailout:()=>h});var i={};t.r(i),t.d(i,{GET:()=>p,POST:()=>l}),t(95655);var o=t(83323),s=t(54647),n=t(66886),a=t(30953),d=t(3471),u=t(69542),c=t(82280);async function l(e){try{let r;let t=await (0,d.getServerSession)(u.L),i=await e.json(),{items:o,customerName:s,email:l,phone:p,address:f,country:x,notes:q,verificationCode:m}=i,w=await Promise.all(o.map(async e=>{let r={quantity:e.quantity,price:0,product:{connect:{id:e.product.id}}};return e.selectedVariant?.id&&(r.variant={connect:{id:e.selectedVariant.id}}),r})),g=null;if(t?.user?.id){let e=await a._.user.findUnique({where:{id:t.user.id}});e&&(g=t.user.id)}if(!g){if(!m)return n.Z.json({error:"Verification code is required for guest checkout"},{status:400});let e=await a._.verificationToken.findUnique({where:{identifier_token:{identifier:l,token:m}}});if(!e)return n.Z.json({error:"Invalid verification code"},{status:400});if(new Date>e.expires)return await a._.verificationToken.delete({where:{identifier_token:{identifier:l,token:m}}}),n.Z.json({error:"Verification code expired"},{status:400});await a._.verificationToken.delete({where:{identifier_token:{identifier:l,token:m}}})}if(l){let e=await a._.customer.findUnique({where:{email:l}});if(e)r=e.id;else{let e=await a._.customer.create({data:{email:l,name:s,phone:p}});r=e.id}}let h=await a._.order.create({data:{total:0,userId:g,customerId:r,customerName:s,address:f,email:l,phone:p,country:x,notes:q,status:"new",items:{create:w}}});return(async()=>{try{console.log("Sending order emails...");let e=await a._.order.findUnique({where:{id:h.id},include:{items:{include:{product:!0,variant:!0}}}});e&&(await Promise.all([c.y.sendOrderConfirmation(e),c.y.sendAdminNotification(e)]),console.log("Order emails sent successfully"))}catch(e){console.error("Failed to send order emails:",e)}})(),n.Z.json(h)}catch(e){return console.error("Error creating order:",e),n.Z.json({error:"Error creating order",details:e instanceof Error?e.message:String(e)},{status:500})}}async function p(e){try{await (0,d.getServerSession)(u.L);let e=await a._.order.findMany({include:{items:{include:{product:!0,variant:!0}},user:!0},orderBy:{createdAt:"desc"}});return n.Z.json(e)}catch(e){return console.error("Error fetching orders:",e),n.Z.json({error:"Error fetching orders"},{status:500})}}let f=o.AppRouteRouteModule,x=new f({definition:{kind:s.x.APP_ROUTE,page:"/api/orders/route",pathname:"/api/orders",filename:"route",bundlePath:"app/api/orders/route"},resolvedPagePath:".\\app\\api\\orders\\route.ts",nextConfigOutput:"standalone",userland:i}),{requestAsyncStorage:q,staticGenerationAsyncStorage:m,serverHooks:w,headerHooks:g,staticGenerationBailout:h}=x,y="/api/orders/route"}};var r=require("../../../webpack-runtime.js");r.C(e);var t=e=>r(r.s=e),i=r.X(0,[4650,9727,3497,6886,3330,6161,2280,9542],()=>t(74910));module.exports=i})();