var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
  if (typeof require !== "undefined")
    return require.apply(this, arguments);
  throw Error('Dynamic require of "' + x + '" is not supported');
});

// src/libs/email/client.ts
import { createClient, Temv1alpha1 } from "@scaleway/sdk";
import { Logger } from "@contractspec/lib.logger";
var DEFAULT_FROM = {
  email: "noreply@transactional.contractspec.io",
  name: "ContractSpec"
};
var DEFAULT_TEAM_INBOX = {
  email: "contact@contractspec.io",
  name: "ContractSpec Team"
};
var DEFAULT_REGION = "fr-par";
var cachedConfig = null;
var cachedClient = null;
var apiFactory = (client) => new Temv1alpha1.API(client);
var mapRegion = (value) => {
  const normalized = value?.trim().toLowerCase();
  if (normalized === "par" || normalized === "fr-par")
    return "fr-par";
  if (normalized === "ams" || normalized === "nl-ams")
    return "nl-ams";
  if (normalized === "waw" || normalized === "pl-waw")
    return "pl-waw";
  return DEFAULT_REGION;
};
var getEmailConfig = () => {
  if (cachedConfig) {
    return { ok: true, config: cachedConfig };
  }
  const accessKey = process.env.SCALEWAY_ACCESS_KEY || process.env.SCALEWAY_ACCESS_KEY_QUEUE;
  const secretKey = process.env.SCALEWAY_SECRET_KEY || process.env.SCALEWAY_SECRET_KEY_QUEUE;
  const projectId = process.env.SCALEWAY_PROJECT_ID;
  if (!accessKey || !secretKey || !projectId) {
    return {
      ok: false,
      errorMessage: "Email service is not configured. Please contact us directly at contact@contractspec.io."
    };
  }
  const region = mapRegion(process.env.SCALEWAY_REGION);
  cachedConfig = {
    accessKey,
    secretKey,
    projectId,
    region,
    defaultZone: `${region}-1`,
    from: {
      email: process.env.SCALEWAY_EMAIL_FROM_EMAIL ?? DEFAULT_FROM.email,
      name: process.env.SCALEWAY_EMAIL_FROM_NAME ?? DEFAULT_FROM.name
    },
    teamInbox: {
      email: process.env.SCALEWAY_EMAIL_TEAM_EMAIL ?? DEFAULT_TEAM_INBOX.email,
      name: process.env.SCALEWAY_EMAIL_TEAM_NAME ?? DEFAULT_TEAM_INBOX.name
    }
  };
  return { ok: true, config: cachedConfig };
};
var getTemClient = (config) => {
  if (cachedClient) {
    return cachedClient;
  }
  const client = createClient({
    accessKey: config.accessKey,
    secretKey: config.secretKey,
    defaultProjectId: config.projectId,
    defaultRegion: config.region,
    defaultZone: config.defaultZone
  });
  cachedClient = apiFactory(client);
  return cachedClient;
};
var sendEmail = async (config, request) => {
  try {
    const client = getTemClient(config);
    await client.createEmail({
      region: config.region,
      projectId: config.projectId,
      from: config.from,
      to: request.to,
      subject: request.subject,
      text: request.text,
      html: request.html || request.text,
      additionalHeaders: request.replyTo ? [{ key: "Reply-To", value: request.replyTo }] : undefined
    });
    return { success: true };
  } catch (error) {
    new Logger().error("scaleway_tem_email_send_failed", {
      context: request.context ?? "email",
      error: error instanceof Error ? error.message : error
    });
    return {
      success: false,
      error,
      errorMessage: "Failed to send email via Scaleway."
    };
  }
};
var __internal = {
  resetCaches() {
    cachedClient = null;
    cachedConfig = null;
    apiFactory = (client) => new Temv1alpha1.API(client);
  },
  setApiFactory(factory) {
    apiFactory = factory;
  },
  setClient(client) {
    cachedClient = client;
  }
};

// src/libs/email/utils.ts
var escapeHtml = (value) => value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
var formatMultilineHtml = (value) => escapeHtml(value).replaceAll(`
`, "<br />");

// src/libs/email/contact.ts
"use server";
var CONTACT_MISSING_CONFIG = "Email service is not configured. Please contact us directly at contact@contractspec.io.";
var CONTACT_SEND_ERROR = "Failed to send message. Please contact us directly at contact@contractspec.io.";
var submitContactForm = async (formData) => {
  const name = (formData.get("name") ?? "").toString().trim();
  const email = (formData.get("email") ?? "").toString().trim();
  const message = (formData.get("message") ?? "").toString().trim();
  if (!email) {
    return {
      success: false,
      text: "Please fill in all required fields."
    };
  }
  const configResult = getEmailConfig();
  if (!configResult.ok || !configResult.config) {
    return {
      success: false,
      text: configResult.errorMessage ?? CONTACT_MISSING_CONFIG
    };
  }
  const senderName = name || email;
  const emailContentText = `
New contact form submission from ${senderName}

Contact Information:
- Name: ${name || "Not provided"}
- Email: ${email}

Message:
${message || "No message provided"}

---
Submitted via ContractSpec contact form
  `.trim();
  const emailContentHtml = `
    <div style="font-family: sans-serif; max-width: 640px; margin: 0 auto;">
      <h1 style="color: #8b5cf6; margin-bottom: 12px;">New contact form submission</h1>
      <p style="margin: 0 0 12px;">From ${escapeHtml(senderName)}</p>
      <h2 style="color: #8b5cf6; margin: 16px 0 8px;">Contact Information</h2>
      <ul style="padding-left: 16px; line-height: 1.6; margin: 0 0 16px;">
        <li>Name: ${escapeHtml(name || "Not provided")}</li>
        <li>Email: ${escapeHtml(email)}</li>
      </ul>
      <h2 style="color: #8b5cf6; margin: 16px 0 8px;">Message</h2>
      <div style="border: 1px solid #e5e7eb; border-radius: 8px; padding: 12px; background: #f9fafb; white-space: pre-wrap; line-height: 1.6;">
        ${message ? formatMultilineHtml(message) : "No message provided"}
      </div>
      <p style="color: #6b7280; font-size: 12px; margin-top: 20px;">Submitted via ContractSpec contact form</p>
    </div>
  `;
  const sendResult = await sendEmail(configResult.config, {
    to: [configResult.config.teamInbox],
    subject: `New Contact Form Message from ${senderName}`,
    text: emailContentText,
    html: emailContentHtml,
    replyTo: email,
    context: "contact-form"
  });
  if (!sendResult.success) {
    return { success: false, text: CONTACT_SEND_ERROR };
  }
  return { success: true, text: "Message sent successfully!" };
};
export {
  submitContactForm
};
