/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { createContext, useContext } from 'react';
import { type Key } from '../hooks/useKeypress.js';
import { type IdeIntegrationNudgeResult } from '../IdeIntegrationNudge.js';
import { type CommandMigrationNudgeResult } from '../CommandFormatMigrationNudge.js';
import { type FolderTrustChoice } from '../components/FolderTrustDialog.js';
import {
  type AuthType,
  type EditorType,
  type ApprovalMode,
  type CodingPlanRegion,
} from '@qwen-code/qwen-code-core';
import { type SettingScope } from '../../config/settings.js';
import { type AlibabaStandardRegion } from '../../constants/alibabaStandardApiKey.js';
import type { AuthState, HistoryItem } from '../types.js';
import { type ArenaDialogType } from '../hooks/useArenaCommand.js';
// OpenAICredentials type (previously imported from OpenAIKeyPrompt)
export interface OpenAICredentials {
  apiKey: string;
  baseUrl?: string;
  model?: string;
}

export interface UIActions {
  openThemeDialog: () => void;
  openEditorDialog: () => void;
  openMemoryDialog: () => void;
  handleThemeSelect: (
    themeName: string | undefined,
    scope: SettingScope,
  ) => void;
  handleThemeHighlight: (themeName: string | undefined) => void;
  handleApprovalModeSelect: (
    mode: ApprovalMode | undefined,
    scope: SettingScope,
  ) => void;
  handleAuthSelect: (
    authType: AuthType | undefined,
    credentials?: OpenAICredentials,
  ) => Promise<void>;
  handleCodingPlanSubmit: (
    apiKey: string,
    region?: CodingPlanRegion,
  ) => Promise<void>;
  handleAlibabaStandardSubmit: (
    apiKey: string,
    region: AlibabaStandardRegion,
    modelIdsInput: string,
  ) => Promise<void>;
  setAuthState: (state: AuthState) => void;
  onAuthError: (error: string | null) => void;
  cancelAuthentication: () => void;
  handleEditorSelect: (
    editorType: EditorType | undefined,
    scope: SettingScope,
  ) => void;
  exitEditorDialog: () => void;
  closeSettingsDialog: () => void;
  closeMemoryDialog: () => void;
  closeModelDialog: () => void;
  openModelDialog: (options?: { fastModelMode?: boolean }) => void;
  openArenaDialog: (type: Exclude<ArenaDialogType, null>) => void;
  closeArenaDialog: () => void;
  handleArenaModelsSelected?: (models: string[]) => void;
  dismissCodingPlanUpdate: () => void;
  closeTrustDialog: () => void;
  closePermissionsDialog: () => void;
  setShellModeActive: (value: boolean) => void;
  vimHandleInput: (key: Key) => boolean;
  handleIdePromptComplete: (result: IdeIntegrationNudgeResult) => void;
  handleCommandMigrationComplete: (result: CommandMigrationNudgeResult) => void;
  handleFolderTrustSelect: (choice: FolderTrustChoice) => void;
  setConstrainHeight: (value: boolean) => void;
  onEscapePromptChange: (show: boolean) => void;
  onSuggestionsVisibilityChange: (visible: boolean) => void;
  refreshStatic: () => void;
  handleFinalSubmit: (value: string) => void;
  handleRetryLastPrompt: () => void;
  handleClearScreen: () => void;
  popAllQueuedMessages: () => string | null;
  // Welcome back dialog
  handleWelcomeBackSelection: (choice: 'continue' | 'restart') => void;
  handleWelcomeBackClose: () => void;
  // Subagent dialogs
  closeSubagentCreateDialog: () => void;
  closeAgentsManagerDialog: () => void;
  // Extensions manager dialog
  closeExtensionsManagerDialog: () => void;
  // MCP dialog
  closeMcpDialog: () => void;
  // Hooks dialog
  openHooksDialog: () => void;
  // Hooks dialog
  closeHooksDialog: () => void;
  // Resume session dialog
  openResumeDialog: () => void;
  closeResumeDialog: () => void;
  handleResume: (sessionId: string) => void;
  // Delete session dialog
  openDeleteDialog: () => void;
  closeDeleteDialog: () => void;
  handleDelete: (sessionId: string) => void;
  // Feedback dialog
  openFeedbackDialog: () => void;
  closeFeedbackDialog: () => void;
  temporaryCloseFeedbackDialog: () => void;
  submitFeedback: (rating: number) => void;
  // Rewind selector
  openRewindSelector: () => void;
  closeRewindSelector: () => void;
  handleRewindConfirm: (userItem: HistoryItem) => void;
}

export const UIActionsContext = createContext<UIActions | null>(null);

export const useUIActions = () => {
  const context = useContext(UIActionsContext);
  if (!context) {
    throw new Error('useUIActions must be used within a UIActionsProvider');
  }
  return context;
};
