export const MAX_ATTACHMENT_BYTES = 8 * 1024 * 1024;
export const MAX_MESSAGE_ATTACHMENT_COUNT = 8;
export const MAX_MESSAGE_ATTACHMENT_BYTES = 32 * 1024 * 1024;

export function selectAttachmentUploads(currentAttachments = [], files = []) {
  let count = currentAttachments.length;
  let totalBytes = currentAttachments.reduce((total, item) => total + safeFileSize(item), 0);
  const accepted = [];
  const rejected = [];

  for (const file of files) {
    const size = safeFileSize(file);
    if (size <= 0 || size > MAX_ATTACHMENT_BYTES || count >= MAX_MESSAGE_ATTACHMENT_COUNT
      || totalBytes + size > MAX_MESSAGE_ATTACHMENT_BYTES) {
      rejected.push(file);
      continue;
    }
    accepted.push(file);
    count += 1;
    totalBytes += size;
  }

  return { accepted, rejected };
}

function safeFileSize(file) {
  return Number.isSafeInteger(file?.size) && file.size >= 0 ? file.size : 0;
}
