import{Logger as m}from"@contractspec/lib.logger";import{createClient as E,Temv1alpha1 as s}from"@scaleway/sdk";var c={email:"noreply@transactional.contractspec.io",name:"ContractSpec"},l={email:"contact@contractspec.io",name:"ContractSpec Team"},p="fr-par",a=null,n=null,o=(e)=>new s.API(e),A=(e)=>{let t=e?.trim().toLowerCase();if(t==="par"||t==="fr-par")return"fr-par";if(t==="ams"||t==="nl-ams")return"nl-ams";if(t==="waw"||t==="pl-waw")return"pl-waw";return p},_=()=>{if(a)return{ok:!0,config:a};let e=process.env.SCALEWAY_ACCESS_KEY||process.env.SCALEWAY_ACCESS_KEY_QUEUE,t=process.env.SCALEWAY_SECRET_KEY||process.env.SCALEWAY_SECRET_KEY_QUEUE,r=process.env.SCALEWAY_PROJECT_ID;if(!e||!t||!r)return{ok:!1,errorMessage:"Email service is not configured. Please contact us directly at contact@contractspec.io."};let i=A(process.env.SCALEWAY_REGION);return a={accessKey:e,secretKey:t,projectId:r,region:i,defaultZone:`${i}-1`,from:{email:process.env.SCALEWAY_EMAIL_FROM_EMAIL??c.email,name:process.env.SCALEWAY_EMAIL_FROM_NAME??c.name},teamInbox:{email:process.env.SCALEWAY_EMAIL_TEAM_EMAIL??l.email,name:process.env.SCALEWAY_EMAIL_TEAM_NAME??l.name}},{ok:!0,config:a}},d=(e)=>{if(n)return n;let t=E({accessKey:e.accessKey,secretKey:e.secretKey,defaultProjectId:e.projectId,defaultRegion:e.region,defaultZone:e.defaultZone});return n=o(t),n},f=async(e,t)=>{try{return await d(e).createEmail({region:e.region,projectId:e.projectId,from:e.from,to:t.to,subject:t.subject,text:t.text,html:t.html||t.text,additionalHeaders:t.replyTo?[{key:"Reply-To",value:t.replyTo}]:void 0}),{success:!0}}catch(r){return new m().error("scaleway_tem_email_send_failed",{context:t.context??"email",error:r instanceof Error?r.message:r}),{success:!1,error:r,errorMessage:"Failed to send email via Scaleway."}}},y={resetCaches(){n=null,a=null,o=(e)=>new s.API(e)},setApiFactory(e){o=e},setClient(e){n=e}};export{f as sendEmail,_ as getEmailConfig,y as __internal};
