import type { SessionShare } from '@/types'

/**
 * Build the full share URL from a SessionShare object.
 * Uses share.share_url if present, otherwise constructs from token.
 */
export function buildSessionShareUrl(share: SessionShare): string {
  if (share.share_url) return share.share_url
  return `${window.location.origin}/share/${share.share_token}`
}

/**
 * Filter shares to only those belonging to a specific session.
 */
export function filterSharesForSession(shares: SessionShare[], sessionId: string): SessionShare[] {
  return shares.filter(s => s.session_id === sessionId && s.is_active)
}

/**
 * Get the most recent active share for a given session.
 * Returns null if no active shares exist.
 */
export function getLatestActiveShareForSession(shares: SessionShare[], sessionId: string): SessionShare | null {
  const sessionShares = filterSharesForSession(shares, sessionId)
  if (sessionShares.length === 0) return null
  return sessionShares.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime())[0]
}
