import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { getIntegration } from "@open-mercato/shared/modules/integrations/types";
import { SyncExternalIdMapping } from "./entities.js";
function buildIntegrationData(mappings) {
  const data = {};
  for (const mapping of mappings) {
    const definition = getIntegration(mapping.integrationId);
    data[mapping.integrationId] = {
      externalId: mapping.externalId,
      externalUrl: definition?.buildExternalUrl?.(mapping.externalId),
      lastSyncedAt: mapping.lastSyncedAt?.toISOString(),
      syncStatus: mapping.syncStatus
    };
  }
  return data;
}
const externalIdMappingEnricher = {
  id: "integrations.external-id-mapping",
  targetEntity: "*",
  features: ["integrations.view"],
  priority: 10,
  timeout: 500,
  critical: false,
  fallback: {},
  async enrichOne(record, context) {
    const em = context.em.fork();
    const targetEntity = context.targetEntity;
    if (!targetEntity) return { ...record, _integrations: {} };
    const mappings = await findWithDecryption(
      em,
      SyncExternalIdMapping,
      {
        internalEntityType: targetEntity,
        internalEntityId: record.id,
        organizationId: context.organizationId,
        tenantId: context.tenantId,
        deletedAt: null
      },
      void 0,
      { organizationId: context.organizationId, tenantId: context.tenantId }
    );
    if (mappings.length === 0) return { ...record, _integrations: {} };
    return {
      ...record,
      _integrations: buildIntegrationData(mappings)
    };
  },
  async enrichMany(records, context) {
    const em = context.em.fork();
    const targetEntity = context.targetEntity;
    if (!targetEntity || records.length === 0) return records.map((r) => ({ ...r, _integrations: {} }));
    const recordIds = records.map((r) => r.id);
    const allMappings = await findWithDecryption(
      em,
      SyncExternalIdMapping,
      {
        internalEntityType: targetEntity,
        internalEntityId: { $in: recordIds },
        organizationId: context.organizationId,
        tenantId: context.tenantId,
        deletedAt: null
      },
      void 0,
      { organizationId: context.organizationId, tenantId: context.tenantId }
    );
    if (allMappings.length === 0) return records.map((r) => ({ ...r, _integrations: {} }));
    const mappingsByRecord = /* @__PURE__ */ new Map();
    for (const mapping of allMappings) {
      const list = mappingsByRecord.get(mapping.internalEntityId) ?? [];
      list.push(mapping);
      mappingsByRecord.set(mapping.internalEntityId, list);
    }
    return records.map((record) => {
      const mappings = mappingsByRecord.get(record.id);
      if (!mappings || mappings.length === 0) return { ...record, _integrations: {} };
      return {
        ...record,
        _integrations: buildIntegrationData(mappings)
      };
    });
  }
};
const enrichers = [externalIdMappingEnricher];
export {
  enrichers
};
//# sourceMappingURL=enrichers.js.map
