import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { SyncExternalIdMapping } from "../../integrations/data/entities.js";
function createExternalIdMappingService(em) {
  return {
    async lookupLocalId(integrationId, entityType, externalId, scope) {
      const row = await findOneWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          externalId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      return row?.internalEntityId ?? null;
    },
    async lookupExternalId(integrationId, entityType, localId, scope) {
      const row = await findOneWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          internalEntityId: localId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      return row?.externalId ?? null;
    },
    async storeExternalIdMapping(integrationId, entityType, localId, externalId, scope) {
      const existingByLocalId = await findOneWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          internalEntityId: localId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      const existingByExternalId = await findOneWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          externalId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      const existing = existingByExternalId ?? existingByLocalId;
      if (existing) {
        const now = /* @__PURE__ */ new Date();
        existing.internalEntityId = localId;
        existing.externalId = externalId;
        existing.syncStatus = "synced";
        existing.lastSyncedAt = now;
        existing.deletedAt = null;
        if (existingByExternalId && existingByLocalId && existingByExternalId.id !== existingByLocalId.id) {
          const duplicate = existing.id === existingByExternalId.id ? existingByLocalId : existingByExternalId;
          duplicate.deletedAt = now;
        }
        await em.flush();
        return existing;
      }
      const created = em.create(SyncExternalIdMapping, {
        integrationId,
        internalEntityType: entityType,
        internalEntityId: localId,
        externalId,
        syncStatus: "synced",
        lastSyncedAt: /* @__PURE__ */ new Date(),
        organizationId: scope.organizationId,
        tenantId: scope.tenantId
      });
      await em.persist(created).flush();
      return created;
    },
    async deleteExternalIdMapping(integrationId, entityType, localId, scope) {
      const row = await findOneWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          internalEntityId: localId,
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      if (!row) return false;
      row.deletedAt = /* @__PURE__ */ new Date();
      await em.flush();
      return true;
    },
    async deleteExternalIdMappings(integrationId, entityType, localIds, scope) {
      const uniqueLocalIds = Array.from(new Set(localIds));
      if (uniqueLocalIds.length === 0) return 0;
      const rows = await findWithDecryption(
        em,
        SyncExternalIdMapping,
        {
          integrationId,
          internalEntityType: entityType,
          internalEntityId: { $in: uniqueLocalIds },
          organizationId: scope.organizationId,
          tenantId: scope.tenantId,
          deletedAt: null
        },
        void 0,
        scope
      );
      if (rows.length === 0) return 0;
      const now = /* @__PURE__ */ new Date();
      for (const row of rows) {
        row.deletedAt = now;
      }
      await em.flush();
      return rows.length;
    }
  };
}
export {
  createExternalIdMappingService
};
//# sourceMappingURL=id-mapping.js.map
