export interface TagSuggestion {
  value: string;
  count: number;
}

export async function searchTagSuggestions(
  query: string,
  libraryId: string,
): Promise<TagSuggestion[]> {
  if (!query || query.length < 2 || !libraryId) return [];

  const token = localStorage.getItem("token");
  if (!token) return [];

  try {
    const response = await fetch(
      `/api/media-items/search?tags=${encodeURIComponent(query)}&library_id=${libraryId}&limit=20`,
      { headers: { Authorization: `Bearer ${token}` } },
    );

    if (!response.ok) return [];

    const data = await response.json();
    return (data.results || []) as TagSuggestion[];
  } catch {
    return [];
  }
}
