import { ENTRIES } from "./entries.js";
import { hasSeen, hasSeenVariant } from "./storage.js";

export function getEntryCompletion(player, entry) {
  const variants = Array.isArray(entry.variants) ? entry.variants : [];

  if (variants.length === 0) {
    return hasSeen(player, entry.id) ? 100 : 0;
  }

  let seenCount = 0;

  for (const v of variants) {
    if (hasSeenVariant(player, entry.id, v.id)) {
      seenCount++;
    }
  }

  const percent = (seenCount / variants.length) * 100;
  return Math.floor(percent);
}

export function getGlobalCompletion(player) {
  if (ENTRIES.length === 0) return 0;

  let totalPercent = 0;

  for (const entry of ENTRIES) {
    totalPercent += getEntryCompletion(player, entry);
  }

  return Math.floor(totalPercent / ENTRIES.length);
}