// @ts-nocheck
/**
 * boardUtils.js
 * Shared utility functions for participant board operations.
 * Can be imported by both src/ui/ and other modules without violating import boundaries.
 */

/**
 * Resolves the target state for a visibility toggle action.
 * active → hidden, hidden → active, ghost → ghost (no toggle)
 * @param {string} currentState
 * @returns {string} Target state
 */
export function resolveToggleTarget(currentState) {
  if (currentState === 'ghost') return 'ghost';
  if (currentState === 'hidden') return 'active';
  return 'hidden';
}

/**
 * Builds context for a single participant in the Director's Board.
 * @param {object} stateStore
 * @param {string} userId
 * @param {object} [user] - Optional user object for additional data
 * @param {object} [privacyManager] - Optional PlayerPrivacyManager for privacy settings
 * @param {object} [controller] - Optional ScryingPoolController for pending-op state
 * @returns {object} Participant context
 */
export function buildSimpleParticipantContext(stateStore, userId, user, privacyManager, controller) {
  const state = stateStore.getState(userId);
  const resolvedState = state ?? 'active';
  const context = {
    userId,
    state: resolvedState,
    isGhost: state === 'ghost',
    isHidden: resolvedState === 'hidden',
    hasPendingOp: controller?._pendingOps?.has?.(userId) ?? false,
  };

  // Name, avatar and aria labels from the user object
  if (user && typeof user === 'object') {
    context.name = user.name ?? '';
    context.avatarSrc = user.avatar ?? 'icons/svg/mystery-man.svg';
    context.cardAriaLabel = user.name ?? userId;
    context.toggleAriaLabel = resolvedState === 'hidden'
      ? `Show ${user.name}`
      : `Hide ${user.name}`;
  }

  // Add privacy settings if privacyManager is provided
  if (privacyManager && user) {
    try {
      const settings = privacyManager.getSettings(userId);
      context.isReactionCamEnabled = settings.reactionCamEnabled;
    } catch {
      // If privacy manager fails, just skip adding the badge
      context.isReactionCamEnabled = false;
    }
  }

  return context;
}

/**
 * Builds context for the Director's Board grid.
 * @param {object} stateStore
 * @param {object} controller
 * @param {object} adapter
 * @param {object} [privacyManager] - Optional PlayerPrivacyManager for privacy settings
 * @returns {object} Board context
 */
export function buildBoardContext(stateStore, controller, adapter, privacyManager) {
  try {
    const users = adapter.users.all?.() ?? [];
    const participants = users.map(u => {
      const userId = u.id ?? u;
      return buildSimpleParticipantContext(stateStore, userId, u, privacyManager, controller);
    });
    return { participants, isEmpty: participants.length === 0 };
  } catch (err) {
    console.error('[ScryingPool] buildBoardContext failed:', err);
    return { participants: [], isEmpty: true };
  }
}
