import { buildAttachmentImageUrl } from "../../attachments/lib/imageUrls.js";
function getDb(em) {
  const getter = em?.getKysely;
  return typeof getter === "function" ? getter.call(em) : null;
}
async function fetchMediaIds(db, table, ids, organizationId) {
  const map = /* @__PURE__ */ new Map();
  if (ids.size === 0) return map;
  const rows = await db.selectFrom(table).select(["id", "default_media_id"]).where("id", "in", [...ids]).where("organization_id", "=", organizationId).where("deleted_at", "is", null).execute();
  for (const row of rows) {
    map.set(row.id, row.default_media_id ? buildAttachmentImageUrl(row.default_media_id) : null);
  }
  return map;
}
function enrichRecords(records, productMedia, variantMedia) {
  return records.map((record) => {
    const productId = record["product_id"];
    const variantId = record["product_variant_id"];
    const productUrl = productId ? productMedia.get(productId) : void 0;
    const variantUrl = variantId ? variantMedia.get(variantId) : void 0;
    if (productUrl === void 0 && variantUrl === void 0) return record;
    const snapshot = record["catalog_snapshot"] ?? {};
    const updatedSnapshot = { ...snapshot };
    if (productUrl !== void 0) {
      updatedSnapshot.product = { ...snapshot.product, thumbnailUrl: productUrl ?? snapshot.product?.thumbnailUrl };
    }
    if (variantUrl !== void 0) {
      updatedSnapshot.variant = { ...snapshot.variant, thumbnailUrl: variantUrl ?? snapshot.variant?.thumbnailUrl };
    }
    const changed = updatedSnapshot.product?.thumbnailUrl !== snapshot.product?.thumbnailUrl || updatedSnapshot.variant?.thumbnailUrl !== snapshot.variant?.thumbnailUrl;
    if (!changed) return record;
    return { ...record, catalog_snapshot: updatedSnapshot };
  });
}
function createCatalogImageEnricher(targetEntity) {
  return {
    id: `sales.catalog-image:${targetEntity}`,
    targetEntity,
    features: [],
    priority: 5,
    timeout: 1e3,
    critical: false,
    fallback: {},
    async enrichOne(record, context) {
      return (await this.enrichMany([record], context))[0];
    },
    async enrichMany(records, context) {
      if (records.length === 0) return records;
      const db = getDb(context.em);
      if (!db) return records;
      const productIds = /* @__PURE__ */ new Set();
      const variantIds = /* @__PURE__ */ new Set();
      for (const record of records) {
        if (typeof record["product_id"] === "string") productIds.add(record["product_id"]);
        if (typeof record["product_variant_id"] === "string") variantIds.add(record["product_variant_id"]);
      }
      if (productIds.size === 0 && variantIds.size === 0) return records;
      const [productMedia, variantMedia] = await Promise.all([
        fetchMediaIds(db, "catalog_products", productIds, context.organizationId),
        fetchMediaIds(db, "catalog_product_variants", variantIds, context.organizationId)
      ]);
      return enrichRecords(records, productMedia, variantMedia);
    }
  };
}
const enrichers = [
  createCatalogImageEnricher("sales:sales_quote_line"),
  createCatalogImageEnricher("sales:sales_order_line")
];
export {
  enrichers
};
//# sourceMappingURL=enrichers.js.map
