import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
import { resolveDefaultEmailFromAddress } from "@open-mercato/shared/lib/email/config";
import { notificationDeliveryConfigSchema } from "../data/validators.js";
const NOTIFICATIONS_DELIVERY_CONFIG_KEY = "delivery_strategies";
const envString = (value) => {
  if (!value) return void 0;
  const trimmed = value.trim();
  return trimmed.length ? trimmed : void 0;
};
const resolveEnvDefaults = () => {
  const appUrl = envString(
    process.env.NOTIFICATIONS_APP_URL || process.env.APPLICATION_URL || process.env.NEXT_PUBLIC_APP_URL || process.env.APP_URL
  );
  const panelPath = envString(process.env.NOTIFICATIONS_PANEL_PATH);
  const emailEnabled = parseBooleanWithDefault(process.env.NOTIFICATIONS_EMAIL_ENABLED, true);
  const emailFrom = resolveDefaultEmailFromAddress();
  const emailReplyTo = envString(process.env.NOTIFICATIONS_EMAIL_REPLY_TO || process.env.ADMIN_EMAIL);
  const emailSubjectPrefix = envString(process.env.NOTIFICATIONS_EMAIL_SUBJECT_PREFIX);
  return {
    appUrl,
    panelPath,
    emailEnabled,
    emailFrom,
    emailReplyTo,
    emailSubjectPrefix
  };
};
const DEFAULT_NOTIFICATION_DELIVERY_CONFIG = (() => {
  const env = resolveEnvDefaults();
  return {
    appUrl: env.appUrl,
    panelPath: env.panelPath ?? "/backend/notifications",
    strategies: {
      database: { enabled: true },
      email: {
        enabled: env.emailEnabled,
        from: env.emailFrom,
        replyTo: env.emailReplyTo,
        subjectPrefix: env.emailSubjectPrefix
      },
      custom: {}
    }
  };
})();
const normalizeDeliveryConfig = (input) => {
  const parsed = notificationDeliveryConfigSchema.safeParse(input ?? {});
  if (!parsed.success) {
    return { ...DEFAULT_NOTIFICATION_DELIVERY_CONFIG };
  }
  const value = parsed.data ?? {};
  const strategies = value.strategies ?? {};
  return {
    appUrl: value.appUrl,
    panelPath: value.panelPath ?? DEFAULT_NOTIFICATION_DELIVERY_CONFIG.panelPath,
    strategies: {
      database: {
        enabled: DEFAULT_NOTIFICATION_DELIVERY_CONFIG.strategies.database.enabled
      },
      email: {
        enabled: strategies.email?.enabled ?? DEFAULT_NOTIFICATION_DELIVERY_CONFIG.strategies.email.enabled,
        from: strategies.email?.from,
        replyTo: strategies.email?.replyTo,
        subjectPrefix: strategies.email?.subjectPrefix
      },
      custom: strategies.custom ?? {}
    }
  };
};
async function resolveNotificationDeliveryConfig(resolver, options) {
  const fallback = options?.defaultValue ?? DEFAULT_NOTIFICATION_DELIVERY_CONFIG;
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    return { ...fallback };
  }
  try {
    const value = await service.getValue("notifications", NOTIFICATIONS_DELIVERY_CONFIG_KEY, { defaultValue: fallback });
    return normalizeDeliveryConfig(value);
  } catch {
    return { ...fallback };
  }
}
async function saveNotificationDeliveryConfig(resolver, config) {
  let service;
  try {
    service = resolver.resolve("moduleConfigService");
  } catch {
    throw new Error("Configuration service unavailable");
  }
  const normalized = normalizeDeliveryConfig(config);
  await service.setValue("notifications", NOTIFICATIONS_DELIVERY_CONFIG_KEY, normalized);
}
function resolveNotificationPanelUrl(config) {
  const base = config.appUrl || process.env.APPLICATION_URL || process.env.NEXT_PUBLIC_APP_URL || process.env.APP_URL;
  if (!base || !base.trim()) {
    return null;
  }
  return `${base.replace(/\/$/, "")}${config.panelPath}`;
}
export {
  DEFAULT_NOTIFICATION_DELIVERY_CONFIG,
  NOTIFICATIONS_DELIVERY_CONFIG_KEY,
  resolveNotificationDeliveryConfig,
  resolveNotificationPanelUrl,
  saveNotificationDeliveryConfig
};
//# sourceMappingURL=deliveryConfig.js.map
