export interface BrowserBootState
{
  adapterCount?: number;
  bootstrapStartedAtUtc?: string;
  commandReceiveCount?: number;
  fallbackReason?: string;
  lastCommandReceivedAtUtc?: string;
  lastShellStateReceivedAtUtc?: string;
  pendingCommands?: unknown[];
  pendingShellStates?: unknown[];
  queuedCommandCountAtRuntimeReady?: number;
  queuedShellStateCountAtRuntimeReady?: number;
  runtimeMode?: string;
  runtimeReadyAtUtc?: string;
  runtimeStatusId?: string;
  shellStateReceiveCount?: number;
  shellAuthority?: string;
}

declare global
{
  interface Window
  {
    HyperTwistBrowserBootState?: BrowserBootState;
  }
}

export function resolveBootState(): BrowserBootState
{
  const ExistingBootState = window.HyperTwistBrowserBootState;
  if (ExistingBootState && typeof ExistingBootState === 'object')
  {
    return ExistingBootState;
  }

  const BootState: BrowserBootState = {};
  window.HyperTwistBrowserBootState = BootState;
  return BootState;
}

export function noteBrowserCommandReceived(): BrowserBootState
{
  const BootState = resolveBootState();
  BootState.commandReceiveCount = (BootState.commandReceiveCount ?? 0) + 1;
  BootState.lastCommandReceivedAtUtc = new Date().toISOString();
  return BootState;
}

export function noteBrowserShellStateReceived(): BrowserBootState
{
  const BootState = resolveBootState();
  BootState.shellStateReceiveCount = (BootState.shellStateReceiveCount ?? 0) + 1;
  BootState.lastShellStateReceivedAtUtc = new Date().toISOString();
  return BootState;
}
