// @bun
var __require = import.meta.require;

// 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;
  }
};
export {
  sendEmail,
  getEmailConfig,
  __internal
};
