import { SalesOrder, SalesQuote } from "@open-mercato/core/modules/sales/data/entities";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("sales");
const metadata = {
  event: "customers.address.deleted",
  persistent: true,
  id: "sales:reconcile-on-address-delete"
};
async function handle(payload, ctx) {
  const data = payload;
  const addressId = data?.id;
  const tenantId = data?.tenantId;
  if (!addressId || !tenantId) return;
  const em = ctx.resolve("em");
  try {
    await Promise.all([
      em.nativeUpdate(
        SalesOrder,
        { billingAddressId: addressId, tenantId },
        { billingAddressId: null }
      ),
      em.nativeUpdate(
        SalesOrder,
        { shippingAddressId: addressId, tenantId },
        { shippingAddressId: null }
      ),
      em.nativeUpdate(
        SalesQuote,
        { billingAddressId: addressId, tenantId },
        { billingAddressId: null }
      ),
      em.nativeUpdate(
        SalesQuote,
        { shippingAddressId: addressId, tenantId },
        { shippingAddressId: null }
      )
    ]);
  } catch (err) {
    logger.error("sales.reconcile-on-address-delete Failed to null address references on sales documents", { err });
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=reconcileOnAddressDelete.js.map
