import { resolveNotificationService } from "../../notifications/lib/notificationService.js";
import { buildFeatureNotificationFromType } from "../../notifications/lib/notificationBuilder.js";
import { notificationTypes } from "../notifications.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("catalog");
const metadata = {
  event: "catalog.product.stock_low",
  persistent: true,
  id: "catalog:low-stock-notification"
};
async function handle(payload, ctx) {
  try {
    const notificationService = resolveNotificationService(ctx);
    const typeDef = notificationTypes.find((type) => type.type === "catalog.product.low_stock");
    if (!typeDef) return;
    const notificationInput = buildFeatureNotificationFromType(typeDef, {
      requiredFeature: "catalog.products.manage",
      bodyVariables: {
        productName: payload.productName,
        sku: payload.sku ?? "",
        currentStock: String(payload.currentStock),
        threshold: String(payload.threshold)
      },
      sourceEntityType: "catalog:product",
      sourceEntityId: payload.productId,
      linkHref: `/backend/catalog/products/${payload.productId}`
    });
    await notificationService.createForFeature(notificationInput, {
      tenantId: payload.tenantId,
      organizationId: payload.organizationId ?? null
    });
  } catch (err) {
    logger.error("catalog.low-stock-notification Failed to create notification", { err });
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=low-stock-notification.js.map
