// Click-lock state for `.applet-list-entry.row-3col` rows on the dashboard
// applet grids (My Posts / My Buds / My Notes / My Scrolls / My Games).
//
// Hover styling is pure CSS (`.applet-list-entry.row-3col:hover`); this
// module just persists the same highlight on tap/click so touch devices can
// pin a row to read it, and mirrors the toggle-off / shift-to-other-row /
// click-outside-dismiss behaviour the note-page click-lock established.
//
// State machine (clicking …):
//   • a row that's not locked       → lock it (clearing any prior lock)
//   • the currently-locked row again → unlock it
//   • a different row                → move the lock to that row
//   • anywhere not inside a row      → clear the lock

(function () {
    'use strict';

    var _lockedRow = null;
    var _bound = false;

    function _clearLock() {
        if (_lockedRow) {
            _lockedRow.classList.remove('row-locked');
            _lockedRow = null;
        }
    }

    function _onClick(e) {
        var row = e.target.closest('.applet-list-entry.row-3col');
        if (row) {
            if (row === _lockedRow) {
                _clearLock();
            } else {
                _clearLock();
                row.classList.add('row-locked');
                _lockedRow = row;
            }
            return;
        }
        _clearLock();
    }

    function _init() {
        if (_bound) return;
        _bound = true;
        document.addEventListener('click', _onClick);
    }

    function _testReset() {
        _clearLock();
    }

    window.RowLock = {
        _init: _init,
        _testReset: _testReset,
        get _lockedRow() { return _lockedRow; },
    };

    document.addEventListener('DOMContentLoaded', _init);
}());
