import { resolveAttachmentScopePair } from "@open-mercato/core/modules/attachments/lib/scope";
import { MESSAGE_ATTACHMENT_ENTITY_ID } from "./constants.js";
const LIBRARY_ATTACHMENT_ENTITY_ID = "attachments:library";
function buildOrganizationScopeFilter(organizationId) {
  if (!organizationId) {
    return { organizationId: null };
  }
  return {
    $or: [
      { organizationId },
      { organizationId: null }
    ]
  };
}
async function linkAttachmentsToMessage(em, messageId, attachmentIds, organizationId, tenantId) {
  if (attachmentIds.length === 0) return;
  const { Attachment } = await import("@open-mercato/core/modules/attachments/data/entities");
  const attachments = await em.find(Attachment, {
    id: { $in: attachmentIds },
    tenantId,
    ...buildOrganizationScopeFilter(organizationId)
  });
  for (const attachment of attachments) {
    attachment.entityId = MESSAGE_ATTACHMENT_ENTITY_ID;
    attachment.recordId = messageId;
  }
  await em.flush();
}
async function getMessageAttachments(em, messageId, organizationId, tenantId) {
  const { Attachment } = await import("@open-mercato/core/modules/attachments/data/entities");
  const attachments = await em.find(Attachment, {
    entityId: MESSAGE_ATTACHMENT_ENTITY_ID,
    recordId: messageId,
    tenantId,
    ...buildOrganizationScopeFilter(organizationId)
  });
  return attachments.map((attachment) => ({
    id: attachment.id,
    fileName: attachment.fileName,
    fileSize: attachment.fileSize,
    mimeType: attachment.mimeType,
    url: attachment.url
  }));
}
async function getMessageEmailAttachments(em, messageId, organizationId, tenantId) {
  const { Attachment } = await import("@open-mercato/core/modules/attachments/data/entities");
  const attachments = await em.find(Attachment, {
    entityId: MESSAGE_ATTACHMENT_ENTITY_ID,
    recordId: messageId,
    tenantId,
    ...buildOrganizationScopeFilter(organizationId)
  });
  return attachments.map((attachment) => ({
    fileName: attachment.fileName,
    fileSize: attachment.fileSize,
    mimeType: attachment.mimeType,
    partitionCode: attachment.partitionCode,
    storagePath: attachment.storagePath,
    storageDriver: attachment.storageDriver
  }));
}
async function linkLibraryAttachmentsToMessage(em, messageId, sourceRecordId, organizationId, tenantId) {
  if (!sourceRecordId.trim()) return;
  const { Attachment } = await import("@open-mercato/core/modules/attachments/data/entities");
  const attachments = await em.find(Attachment, {
    entityId: LIBRARY_ATTACHMENT_ENTITY_ID,
    recordId: sourceRecordId,
    tenantId,
    ...buildOrganizationScopeFilter(organizationId)
  });
  if (!attachments.length) return;
  for (const attachment of attachments) {
    attachment.entityId = MESSAGE_ATTACHMENT_ENTITY_ID;
    attachment.recordId = messageId;
  }
  await em.flush();
}
async function copyAttachmentsForForward(em, sourceMessageId, targetMessageId, tenantId) {
  return copyAttachmentsForForwardMessages(
    em,
    [sourceMessageId],
    targetMessageId,
    tenantId
  );
}
async function copyAttachmentsForForwardMessages(em, sourceMessageIds, targetMessageId, tenantId) {
  if (sourceMessageIds.length === 0) return 0;
  const { Attachment } = await import("@open-mercato/core/modules/attachments/data/entities");
  const sourceAttachments = await em.find(Attachment, {
    entityId: MESSAGE_ATTACHMENT_ENTITY_ID,
    recordId: { $in: sourceMessageIds },
    tenantId
  });
  if (sourceAttachments.length === 0) {
    return 0;
  }
  const dedupedById = /* @__PURE__ */ new Map();
  for (const sourceAttachment of sourceAttachments) {
    if (!dedupedById.has(sourceAttachment.id)) {
      dedupedById.set(sourceAttachment.id, sourceAttachment);
    }
  }
  let copied = 0;
  for (const sourceAttachment of dedupedById.values()) {
    const scope = resolveAttachmentScopePair(sourceAttachment);
    if (!scope) continue;
    const copy = em.create(Attachment, {
      entityId: MESSAGE_ATTACHMENT_ENTITY_ID,
      recordId: targetMessageId,
      organizationId: scope.organizationId,
      tenantId: scope.tenantId,
      fileName: sourceAttachment.fileName,
      mimeType: sourceAttachment.mimeType,
      fileSize: sourceAttachment.fileSize,
      storageDriver: sourceAttachment.storageDriver,
      storagePath: sourceAttachment.storagePath,
      storageMetadata: sourceAttachment.storageMetadata,
      url: sourceAttachment.url,
      partitionCode: sourceAttachment.partitionCode
    });
    em.persist(copy);
    copied += 1;
  }
  if (copied > 0) {
    await em.flush();
  }
  return copied;
}
export {
  copyAttachmentsForForward,
  copyAttachmentsForForwardMessages,
  getMessageAttachments,
  getMessageEmailAttachments,
  linkAttachmentsToMessage,
  linkLibraryAttachmentsToMessage
};
//# sourceMappingURL=attachments.js.map
