// Sentinel game state + helpers.
// 8 guesses marked by S·E·N·T·I·N·E·L, one-word answers 4-9 letters.

const SENTINEL_LETTERS = ['S', 'E', 'N', 'T', 'I', 'N', 'E', 'L'];

// Evaluate a guess vs answer -> array of 'correct' | 'present' | 'absent'
// Handles duplicate letters correctly (Wordle-style two-pass).
function evaluateGuess(guess, answer) {
  const g = guess.toUpperCase().split('');
  const a = answer.toUpperCase().split('');
  const result = new Array(g.length).fill('absent');
  const counts = {};
  // Pass 1: mark correct
  for (let i = 0; i < g.length; i++) {
    if (g[i] === a[i]) {
      result[i] = 'correct';
    } else {
      counts[a[i]] = (counts[a[i]] || 0) + 1;
    }
  }
  // Pass 2: mark present
  for (let i = 0; i < g.length; i++) {
    if (result[i] === 'correct') continue;
    if (counts[g[i]] > 0) {
      result[i] = 'present';
      counts[g[i]]--;
    }
  }
  return result;
}

// Aggregate keyboard state from all guesses.
// Priority: correct > present > absent.
function computeKeyState(guesses, answer) {
  const state = {};
  const rank = { absent: 1, present: 2, correct: 3 };
  for (const guess of guesses) {
    const evaluation = evaluateGuess(guess, answer);
    for (let i = 0; i < guess.length; i++) {
      const L = guess[i].toUpperCase();
      const s = evaluation[i];
      if (!state[L] || rank[s] > rank[state[L]]) state[L] = s;
    }
  }
  return state;
}

const COLORS = {
  bg: '#121422',
  bgElevated: '#1a1c2e',
  bgCard: '#1e2036',
  core: '#560079',
  guardian: '#662D91',
  aurora: '#AF88CC',
  white: '#F8F8F3',
  whiteDim: 'rgba(248, 248, 243, 0.55)',
  whiteFaint: 'rgba(248, 248, 243, 0.35)',
  charcoal: '#2A2A31',
  border: 'rgba(248, 248, 243, 0.08)',
  borderStrong: 'rgba(175, 136, 204, 0.25)',
};

const PUZZLE = {
  category: "History",
  clue: "The Roman city buried under ash when Vesuvius erupted in AD 79, and dug out again seventeen centuries later.",
  answer: "POMPEII", // 7 letters
};

// Pre-made guesses for the mid-game state shown in the design.
// Intentionally varied lengths (5, 8, 6) to show the player does NOT know
// the answer length in advance — core mechanic of the game.
const DEMO_GUESSES = [];

Object.assign(window, {
  SENTINEL_LETTERS,
  evaluateGuess,
  computeKeyState,
  COLORS,
  PUZZLE,
  DEMO_GUESSES,
});
