import { resolveNotificationService } from "@open-mercato/core/modules/notifications/lib/notificationService";
import { buildNotificationFromType } from "@open-mercato/core/modules/notifications/lib/notificationBuilder";
import { defaultLocale } from "@open-mercato/shared/lib/i18n/config";
import { loadDictionary } from "@open-mercato/shared/lib/i18n/server";
import { createTranslator } from "@open-mercato/shared/lib/i18n/translate";
import { notificationTypes } from "../notifications.js";
const metadata = {
  event: "ai_assistant.conversation.shared",
  persistent: true,
  id: "ai_assistant:conversation-shared-notify"
};
async function resolveDefaultLocaleStrings(titleKey, bodyKey) {
  if (!titleKey && !bodyKey) return { title: titleKey, body: bodyKey };
  try {
    const dict = await loadDictionary(defaultLocale);
    const t = createTranslator(dict);
    return {
      title: titleKey ? t(titleKey) : titleKey,
      body: bodyKey ? t(bodyKey) : bodyKey
    };
  } catch {
    return { title: titleKey, body: bodyKey };
  }
}
async function handleConversationShared(payload, ctx) {
  if (!payload?.participantUserId || !payload.tenantId) return;
  const typeDef = notificationTypes.find((t) => t.type === "ai_assistant.conversation_shared");
  if (!typeDef) return;
  const container = ctx.container ?? { resolve: ctx.resolve };
  let notificationService;
  try {
    notificationService = resolveNotificationService(container);
  } catch {
    return;
  }
  const notificationInput = buildNotificationFromType(typeDef, {
    recipientUserId: payload.participantUserId,
    bodyVariables: {},
    sourceEntityType: "ai_assistant:ai_chat_conversation",
    sourceEntityId: payload.conversationId,
    linkHref: `/backend?openAiConversation=${payload.conversationId}`
  });
  const resolved = await resolveDefaultLocaleStrings(typeDef.titleKey, typeDef.bodyKey);
  if (resolved.title !== void 0) notificationInput.title = resolved.title;
  if (resolved.body !== void 0) notificationInput.body = resolved.body;
  try {
    await notificationService.create(notificationInput, {
      tenantId: payload.tenantId,
      organizationId: payload.organizationId ?? null
    });
  } catch (err) {
    console.warn("[ai_assistant.conversationSharedNotify] create failed", err);
  }
}
export {
  handleConversationShared as default,
  metadata
};
//# sourceMappingURL=conversation-shared-notify.js.map
