import type { ConnectorSelectionMode } from "@/ipc/types";

type ResolveEffectiveConnectorIdsParams = {
  chatConnectorIds: number[] | null | undefined;
  requestConnectorIds: number[] | null | undefined;
  requestSelectionMode?: ConnectorSelectionMode;
};

export function resolveEffectiveConnectorIds({
  chatConnectorIds,
  requestConnectorIds,
  requestSelectionMode,
}: ResolveEffectiveConnectorIdsParams): number[] {
  const normalizedChatConnectorIds = Array.from(
    new Set((chatConnectorIds ?? []).filter(Number.isFinite)),
  );
  const normalizedRequestConnectorIds = Array.from(
    new Set((requestConnectorIds ?? []).filter(Number.isFinite)),
  );

  if (normalizedRequestConnectorIds.length === 0) {
    return normalizedChatConnectorIds;
  }

  if (requestSelectionMode === "replace") {
    return normalizedRequestConnectorIds;
  }

  return Array.from(
    new Set([...normalizedChatConnectorIds, ...normalizedRequestConnectorIds]),
  );
}
