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("business_rules").child({ component: "rule-execution-failed-notification" });
const metadata = {
  event: "business_rules.rule.execution_failed",
  persistent: true,
  id: "business_rules:rule-execution-failed-notification"
};
async function handle(payload, ctx) {
  try {
    const notificationService = resolveNotificationService(ctx);
    const typeDef = notificationTypes.find((type) => type.type === "business_rules.rule.execution_failed");
    if (!typeDef) return;
    const notificationInput = buildFeatureNotificationFromType(typeDef, {
      requiredFeature: "business_rules.manage",
      bodyVariables: {
        ruleName: payload.ruleName,
        entityType: payload.entityType ?? "",
        errorMessage: payload.errorMessage ?? "Unknown error"
      },
      sourceEntityType: "business_rules:rule",
      sourceEntityId: payload.ruleId,
      linkHref: `/backend/business-rules/${payload.ruleId}`
    });
    await notificationService.createForFeature(notificationInput, {
      tenantId: payload.tenantId,
      organizationId: payload.organizationId ?? null
    });
  } catch (err) {
    logger.error("Failed to create notification", { err });
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=rule-execution-failed-notification.js.map
