import type { BindAttachmentsResult } from "./bindingTypes";

export function dedupeAttachmentIds(ids: readonly string[]): string[] {
  return [...new Set(ids)];
}

export function toggleAttachmentSelection(
  selected: readonly string[],
  id: string,
): string[] {
  return selected.includes(id)
    ? selected.filter((value) => value !== id)
    : [...selected, id];
}

export function retainAvailableSelections(
  selected: readonly string[],
  availableIds: ReadonlySet<string>,
): string[] {
  return selected.filter((id) => availableIds.has(id));
}

export function describeBindResult(result: BindAttachmentsResult): string {
  if (result.createdCount === 0) {
    return result.existingCount > 0
      ? `所选附件均已挂载（已有 ${result.existingCount} 项）`
      : "未新增挂载关系";
  }
  const existing = result.existingCount > 0
    ? `，其中已有挂载 ${result.existingCount} 项`
    : "";
  return `新增挂载 ${result.createdCount} 项${existing}，当前共 ${result.attachmentCount} 项`;
}

