// @bun
import{Logger as g}from"@contractspec/lib.logger";import{createClient as y,Temv1alpha1 as m}from"@scaleway/sdk";var c={email:"noreply@transactional.contractspec.io",name:"ContractSpec"},l={email:"contact@contractspec.io",name:"ContractSpec Team"},S="fr-par",n=null,s=null,o=(e)=>new m.API(e),C=(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 S},p=()=>{if(n)return{ok:!0,config:n};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=C(process.env.SCALEWAY_REGION);return n={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:n}},w=(e)=>{if(s)return s;let t=y({accessKey:e.accessKey,secretKey:e.secretKey,defaultProjectId:e.projectId,defaultRegion:e.region,defaultZone:e.defaultZone});return s=o(t),s},a=async(e,t)=>{try{return await w(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 g().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."}}},R={resetCaches(){s=null,n=null,o=(e)=>new m.API(e)},setApiFactory(e){o=e},setClient(e){s=e}};var b=(e)=>e.replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">","&gt;"),u=(e)=>b(e).replaceAll(`
`,"<br />");var _="Newsletter service is not configured. Please try again later.",d="Failed to subscribe. Please try again later or contact us directly.",Y=async(e)=>{let t=(e.get("email")??"").toString().trim();if(!t||!t.includes("@"))return{success:!1,text:"Please enter a valid email address."};let r=p();if(!r.ok||!r.config)return{success:!1,text:r.errorMessage??_};let i=`
Welcome to ContractSpec!

Thanks for subscribing to our newsletter. You'll receive updates on:

\u2022 New integrations (Stripe, OpenAI, Qdrant, and more)
\u2022 Product features and improvements
\u2022 New templates and examples
\u2022 Architecture insights and best practices
\u2022 Documentation updates

Stay tuned for exciting updates!

---
ContractSpec Team
https://contractspec.io

Unsubscribe: Reply to this email with "unsubscribe"
  `.trim(),E=`
    <div style="font-family: sans-serif; max-width: 640px; margin: 0 auto;">
      <h1 style="color: #8b5cf6;">Welcome to ContractSpec!</h1>
      <p>Thanks for subscribing to our newsletter. You'll receive updates on:</p>
      <ul style="line-height: 1.8;">
        <li>New integrations (Stripe, OpenAI, Qdrant, and more)</li>
        <li>Product features and improvements</li>
        <li>New templates and examples</li>
        <li>Architecture insights and best practices</li>
        <li>Documentation updates</li>
      </ul>
      <p>Stay tuned for exciting updates!</p>
      <hr style="margin: 30px 0; border: none; border-top: 1px solid #e5e7eb;" />
      <p style="color: #6b7280; font-size: 14px;">
        ContractSpec Team<br />
        <a href="https://contractspec.io" style="color: #8b5cf6;">contractspec.io</a>
      </p>
      <p style="color: #9ca3af; font-size: 12px;">
        To unsubscribe, reply to this email with "unsubscribe"
      </p>
    </div>
  `;if(!(await a(r.config,{to:[{email:t}],subject:"Welcome to ContractSpec Newsletter",text:i,html:E,context:"newsletter-welcome"})).success)return{success:!1,text:d};let f=`New newsletter subscription from: ${t}`,A=`
    <div style="font-family: sans-serif; max-width: 640px; margin: 0 auto;">
      <p style="margin: 0 0 8px;">New newsletter subscription</p>
      <p style="margin: 0;"><strong>Email:</strong> ${u(t)}</p>
    </div>
  `;if(!(await a(r.config,{to:[r.config.teamInbox],subject:`New Newsletter Subscription: ${t}`,text:f,html:A,context:"newsletter-team-notification"})).success)return{success:!1,text:d};return{success:!0,text:"Successfully subscribed!"}};export{Y as subscribeToNewsletter};
