import { safeJsonParse, ensureArraySet, addUnique } from "./util.js";

export const PROP_BESTIARY = "ow_bestiary_v1";

export function defaultProgress() {
  return { v: 1, seen: [], killed: [], full: [], advanced: [] };
}

export function getProgress(player) {
  const raw = player.getDynamicProperty(PROP_BESTIARY);
  const data = safeJsonParse(raw, defaultProgress());

  // normaliza
  data.v = 1;
  ensureArraySet(data, "seen");
  ensureArraySet(data, "killed");
  ensureArraySet(data, "full");
  ensureArraySet(data, "advanced");

  return data;
}

export function setProgress(player, data) {
  player.setDynamicProperty(PROP_BESTIARY, JSON.stringify(data));
}

/* =========================
   SEEN
========================= */

export function markSeen(player, id) {
  const p = getProgress(player);
  addUnique(p.seen, id);
  setProgress(player, p);
}

export function hasSeen(player, id) {
  return getProgress(player).seen.includes(id);
}

/* =========================
   KILLED
========================= */

export function markKilled(player, id) {
  const p = getProgress(player);
  addUnique(p.seen, id);
  addUnique(p.killed, id);
  setProgress(player, p);
}

export function hasKilled(player, id) {
  return getProgress(player).killed.includes(id);
}

/* =========================
   VARIANTS (se guardan como "entryId|variantId")
========================= */

export function markSeenVariant(player, entryId, variantId) {
  markSeen(player, `${entryId}|${variantId}`);
}

export function hasSeenVariant(player, entryId, variantId) {
  return hasSeen(player, `${entryId}|${variantId}`);
}

/* =========================
   FULL
========================= */

export function markFull(player, entryId) {
  const p = getProgress(player);
  addUnique(p.full, entryId);
  setProgress(player, p);
}

export function hasFull(player, entryId) {
  return getProgress(player).full.includes(entryId);
}

/* =========================
   ADVANCED
========================= */

export function markAdvanced(player, entryId) {
  const p = getProgress(player);
  addUnique(p.advanced, entryId);
  setProgress(player, p);
}

export function hasAdvanced(player, entryId) {
  return getProgress(player).advanced.includes(entryId);
}