import React, { useMemo } from 'react';

import StatusBar from './StatusBar.js';
import { useTuiCliAdapter } from './tui-cli-adapter-context.js';

import type { TPermissionMode } from '@robota-sdk/agent-core';
import type { IStatusLineCommandSettings } from '@robota-sdk/agent-interface-transport';

interface IProps {
  cwd: string;
  permissionMode: TPermissionMode;
  modelId?: string;
  providerType?: string | undefined;
  sessionId: string;
  isThinking: boolean;
  activeToolCount: number;
  activeBackgroundTaskCount: number;
  hasPendingPrompt: boolean;
  contextState: { percentage: number; usedTokens: number; maxTokens: number };
  sessionName?: string;
  settings: IStatusLineCommandSettings;
  activeAgentLabel?: string;
  activePresetId?: string;
  gitRefreshToken?: number;
}

export default function SessionStatusBar({
  cwd,
  permissionMode,
  modelId,
  providerType,
  sessionId,
  isThinking,
  activeToolCount,
  activeBackgroundTaskCount,
  hasPendingPrompt,
  contextState,
  sessionName,
  settings,
  activeAgentLabel,
  activePresetId,
  gitRefreshToken,
}: IProps): React.ReactElement | null {
  const cliAdapter = useTuiCliAdapter();
  const gitBranch = useMemo(() => cliAdapter.getGitBranch(cwd), [cliAdapter, cwd, gitRefreshToken]);
  const providerDisplayName = useMemo(
    () =>
      providerType !== undefined ? cliAdapter.getProviderDisplayName(providerType) : undefined,
    [cliAdapter, providerType],
  );
  if (!settings.enabled) return null;

  return (
    <StatusBar
      permissionMode={permissionMode}
      modelName={modelId ?? ''}
      providerDisplayName={providerDisplayName}
      sessionId={sessionId}
      isThinking={isThinking}
      activeToolCount={activeToolCount}
      activeBackgroundTaskCount={activeBackgroundTaskCount}
      hasPendingPrompt={hasPendingPrompt}
      contextPercentage={contextState.percentage}
      contextUsedTokens={contextState.usedTokens}
      contextMaxTokens={contextState.maxTokens}
      sessionName={sessionName}
      gitBranch={gitBranch}
      showGitBranch={settings.gitBranch}
      activeAgentLabel={activeAgentLabel}
      activePresetId={activePresetId}
    />
  );
}
