import { resolveNotificationService } from "../../notifications/lib/notificationService.js";
import { buildNotificationFromType } from "../../notifications/lib/notificationBuilder.js";
import { notificationTypes } from "../notifications.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
async function deliverDealClosureNotification(payload, ctx, notificationType) {
  if (!payload?.ownerUserId || !payload.tenantId) return;
  const typeDef = notificationTypes.find((type) => type.type === notificationType);
  if (!typeDef) return;
  const container = ctx.container ?? { resolve: ctx.resolve };
  let notificationService;
  try {
    notificationService = resolveNotificationService(container);
  } catch (err) {
    logger.warn("notificationService resolve failed", { component: "dealClosureNotification", err });
    return;
  }
  const valueDisplay = payload.valueAmount && payload.valueCurrency ? `${payload.valueCurrency} ${payload.valueAmount}` : "";
  const notificationInput = buildNotificationFromType(typeDef, {
    recipientUserId: payload.ownerUserId,
    bodyVariables: {
      dealTitle: payload.title,
      dealValue: valueDisplay
    },
    sourceEntityType: "customers:customer_deal",
    sourceEntityId: payload.id,
    linkHref: `/backend/customers/deals/${payload.id}`
  });
  try {
    await notificationService.create(notificationInput, {
      tenantId: payload.tenantId,
      organizationId: payload.organizationId ?? null
    });
  } catch (err) {
    logger.warn("Notification create failed", { component: "dealClosureNotification", notificationType, err });
  }
}
export {
  deliverDealClosureNotification
};
//# sourceMappingURL=dealClosureNotification.js.map
