const EDITOR_DRAFT_CONFLICT_ID = "__draft__";
function sharesActor(first, second) {
  if (first.ownerUserId && second.ownerUserId && first.ownerUserId === second.ownerUserId) return true;
  if (first.participants.length === 0 || second.participants.length === 0) return false;
  const firstParticipantIds = new Set(first.participants.map((participant) => participant.userId));
  return second.participants.some((participant) => firstParticipantIds.has(participant.userId));
}
function isActor(item, userId) {
  if (item.ownerUserId === userId) return true;
  return item.participants.some((participant) => participant.userId === userId);
}
function appendConflict(conflicts, itemId, otherId) {
  const existing = conflicts.get(itemId);
  if (existing) existing.push(otherId);
  else conflicts.set(itemId, [otherId]);
}
function findConflicts(items, options = {}) {
  const conflicts = /* @__PURE__ */ new Map();
  const restrictToCurrentUser = options.scope === "mine" && Boolean(options.currentUserId);
  const currentUserId = options.currentUserId ?? "";
  const candidates = items.filter((item) => item.status !== "canceled").sort((first, second) => first.start.getTime() - second.start.getTime());
  for (let index = 0; index < candidates.length; index += 1) {
    const current = candidates[index];
    const currentEnd = current.end.getTime();
    for (let lookahead = index + 1; lookahead < candidates.length; lookahead += 1) {
      const other = candidates[lookahead];
      if (other.start.getTime() >= currentEnd) break;
      if (current.start.getTime() >= other.end.getTime()) continue;
      if (!sharesActor(current, other)) continue;
      if (restrictToCurrentUser && !(isActor(current, currentUserId) && isActor(other, currentUserId))) continue;
      appendConflict(conflicts, current.id, other.id);
      appendConflict(conflicts, other.id, current.id);
    }
  }
  return conflicts;
}
function findEditorConflictItems(draft, others, excludeId, options = {}) {
  const draftStatus = draft.status ?? "planned";
  const draftItem = {
    id: EDITOR_DRAFT_CONFLICT_ID,
    title: "",
    interactionType: "meeting",
    category: "other",
    status: draftStatus,
    start: draft.start,
    end: draft.end,
    allDay: false,
    location: null,
    platform: null,
    locationKind: null,
    participants: draft.participants,
    ownerUserId: draft.ownerUserId,
    entityId: null,
    dealId: null,
    color: null,
    isRecurringOccurrence: false,
    updatedAt: null,
    raw: { id: EDITOR_DRAFT_CONFLICT_ID, interactionType: "meeting", status: draftStatus }
  };
  const candidates = others.filter(
    (item) => item.id !== EDITOR_DRAFT_CONFLICT_ID && item.raw.id !== excludeId
  );
  const conflictIds = findConflicts([draftItem, ...candidates], options).get(EDITOR_DRAFT_CONFLICT_ID) ?? [];
  const byId = new Map(candidates.map((item) => [item.id, item]));
  return conflictIds.map((id) => byId.get(id)).filter((item) => Boolean(item));
}
export {
  EDITOR_DRAFT_CONFLICT_ID,
  findConflicts,
  findEditorConflictItems
};
//# sourceMappingURL=conflicts.js.map
