import { cosineSimilarity } from "@/utils/math";

interface Tag {
    index: number;
    tag: string;
}

export function isSensitive(tag: Tag) {
  return tag.tag.endsWith("_sensitive")
}

export const selected_tags = [
    "politics", "gardening", "art", "nature", "pride", "cycling", "climate",
    "programming", "dogs", "privacy", "cats", "gaming", "education",
    "science", "music", "movies", "food", "books", "lgbtq", "python",
    "emacs", "gay", "trans", "furry", "photography", "cooking", "literature",
    "television"
  ];
  
  export function find_most_similar_tags(all_tags: Tag[], tag_names: string[], selectedTagIds: number[], positions: number[][]) {
    let most_similar: Tag[] = [];
    // get the average position of all selected tags
    if (selectedTagIds.length > 0) {
      // loop through all selected tags and get their positions
      const selected_positions = selectedTagIds.map((tagId) => positions[tagId]);
      for (let i = 0; i < selected_positions.length; i++) {
        let tag_similarity = positions.map((row) => cosineSimilarity(selected_positions[i], row));
        let tag_rank = tag_similarity.map((similarity, index) => ({index, similarity})).sort((a, b) => b.similarity - a.similarity).map((item, index) => ({index: item.index, similarity: item.similarity, name: tag_names[item.index]}));
        console.log(tag_rank.slice(0, 10));
        let topServerIds = tag_rank.slice(0, 10).map((item) => item.index);
        for (let tagId of all_tags.filter((tag) => topServerIds.includes(tag.index))) {
          if (!selectedTagIds.includes(tagId.index) && !most_similar.includes(tagId)) {
            most_similar.push(tagId);
          }
        }
      }
    }
    return most_similar;
  }

export type { Tag };