// my-sea-seats.js — one-shot "seated" animation for the my-sea table hex.
//
// When a seat is occupied (owner draws → seat 1C; visitor deposits a token →
// seat 2C), the FIRST time a given viewer sees that occupancy the seat plays
// a one-shot glow: chair fills --terUser + glows --ninUsers for ~1.5s, then
// settles back to --secUser at FULL opacity (the steady `.seated` look), with
// `.fa-ban` already swapped for `.fa-circle-check` server-side. "First time"
// is tracked per-browser in localStorage, keyed on a stable per-occupancy
// token (`data-seat-token`) so a fresh draw / fresh invite re-animates but a
// reload does not. (Spec: user 2026-05-27.)
(function () {
    'use strict';
    var STORE_PREFIX = 'mysea-seat-seen:';
    var GLOW_MS = 1500;

    function alreadySeen(token) {
        try {
            return window.localStorage.getItem(STORE_PREFIX + token) === '1';
        } catch (e) {
            return false;  // private mode / storage disabled → always animate
        }
    }

    function markSeen(token) {
        try { window.localStorage.setItem(STORE_PREFIX + token, '1'); }
        catch (e) { /* no-op */ }
    }

    // Play the one-shot glow on a `.table-seat` element unless this viewer has
    // already seen this exact occupancy. Safe to call on a seat with no token
    // (always animates, never persists).
    function playSeatGlow(seat) {
        if (!seat) return;
        var token = seat.getAttribute('data-seat-token');
        if (token && alreadySeen(token)) return;
        seat.classList.add('seat-just-seated');
        window.setTimeout(function () {
            seat.classList.remove('seat-just-seated');
        }, GLOW_MS);
        if (token) markSeen(token);
    }

    // Exposed so the owner's FREE DRAW flow can fire the same glow the moment
    // seat 1C transitions to `.seated` client-side (my_sea.html).
    window.playSeatGlow = playSeatGlow;

    document.addEventListener('DOMContentLoaded', function () {
        var seats = document.querySelectorAll('.table-seat.seated[data-seat-token]');
        Array.prototype.forEach.call(seats, function (seat) {
            playSeatGlow(seat);
        });
    });
}());
