import type { SearchItem } from '$lib/navigation/client-navigation';
import type { AppSession } from '$lib/session';

export const PALETTE_RESULT_LIMIT = 10;

export function buildSessionKey(session: AppSession | null | undefined): string | null {
  if (!session) return null;
  return `${session.role}:${session.email}:${session.user_id ?? ''}`;
}

export function filterSearchItems(items: SearchItem[], query: string, limit = PALETTE_RESULT_LIMIT) {
  const trimmedQuery = query.trim().toLowerCase();
  const matchingItems = items.filter((item) => {
    const haystack = `${item.label} ${item.description} ${item.keywords}`.toLowerCase();
    return haystack.includes(trimmedQuery);
  });

  const filteredItems = matchingItems.slice(0, limit);
  return {
    matchingItems,
    filteredItems,
    hiddenResultCount: matchingItems.length - filteredItems.length
  };
}
