import { removeDuplicates } from '@/lib/utils';

const HUMAN_PREFIX = 'human:';
const NETWORK_PREFIX = 'network:';

export type StreamVisibility =
  | { mode: 'network' }
  | { mode: 'custom'; humanIds: string[] };

export function parseVisibleTo(
  visibleTo: string[],
  networkId: string,
): StreamVisibility {
  if (visibleTo.includes(`${NETWORK_PREFIX}${networkId}`)) {
    return { mode: 'network' };
  }
  const humanIds = visibleTo
    .filter((v) => v.startsWith(HUMAN_PREFIX))
    .map((v) => v.slice(HUMAN_PREFIX.length));
  return { mode: 'custom', humanIds };
}

export function buildNetworkVisibility(networkId: string): string[] {
  return [`${NETWORK_PREFIX}${networkId}`];
}

export function buildCustomVisibility(humanIds: string[]): string[] {
  return removeDuplicates(humanIds).map((id) => `${HUMAN_PREFIX}${id}`);
}
