// @ts-nocheck
import { debugLog } from '../common';
import { usePolicyStore } from '../../../stores/policy';
import { useModelStore } from '../../../stores/model';
import { usePersonalizationStore } from '../../../stores/personalization';
import { useTutorialStore } from '../../../stores/tutorial';
import { renderMarkdown as renderMarkdownHelper } from '../../../composables/useMarkdownRenderer';
import { scrollToBottom as scrollToBottomHelper, conditionalScrollToBottom as conditionalScrollToBottomHelper, scrollThinkingToBottom as scrollThinkingToBottomHelper } from '../../../composables/useScrollControl';
import { startResize as startPanelResize, handleResize as handlePanelResize, stopResize as stopPanelResize } from '../../../composables/usePanelResize';
import {
  SUB_AGENT_DONE_PREFIX_RE,
  BG_RUN_COMMAND_DONE_PREFIX_RE,
  userMDebug,
  UI_BOUNCE_TRACE_MAX,
  uiBounceTraceLastTsByKey,
  isUiBounceTraceEnabled,
  uiBounceTrace,
  isConnectionDiagEnabled,
  pushConnectionDiagRecord,
  connectionDiag,
  parseSubAgentDoneLabel,
  parseBackgroundRunCommandDoneLabel,
  parseSystemNoticeLabel,
} from './shared';

export const panelMethods = {
  toggleSidebar() {
    if (this.isMobileViewport && this.activeMobileOverlay === 'conversation') {
      this.closeMobileOverlay();
      return;
    }
    this.uiToggleSidebar();
  },
  togglePanelMenu() {
    this.uiTogglePanelMenu();
  },
  selectPanelMode(mode) {
    this.uiSetPanelMode(mode);
    this.uiSetPanelMenuOpen(false);
    if (mode === 'subAgents') {
      // 切换到子智能体面板时立即刷新，避免等待轮询间隔
      this.subAgentFetch();
      this.subAgentStartPolling();
      return;
    }
    if (mode === 'backgroundCommands') {
      // 切换到后台指令面板时立即刷新，避免等待轮询间隔
      this.backgroundCommandFetch();
      this.backgroundCommandStartPolling();
    }
  },
  toggleFocusPanel() {
    this.rightCollapsed = !this.rightCollapsed;
    if (!this.rightCollapsed && this.rightWidth < this.minPanelWidth) {
      this.rightWidth = this.minPanelWidth;
    }
  },
  toggleApprovalPanel() {
    console.log(
      '[UI_DEBUG] toggleApprovalPanel called, current rightCollapsed:',
      this.rightCollapsed,
      'new state:',
      !this.rightCollapsed
    );
    this.rightCollapsed = !this.rightCollapsed;
    if (!this.rightCollapsed && this.rightWidth < this.minPanelWidth) {
      this.rightWidth = this.minPanelWidth;
    }
    if (!this.rightCollapsed) {
      this.fetchPendingToolApprovals();
    }
  },
  toggleTerminalPanel() {
    this.terminalPanelOpen = !this.terminalPanelOpen;
    if (this.terminalPanelOpen) {
      this.subscribeTerminalEvents();
    }
  },
  closeTerminalPanel() {
    this.terminalPanelOpen = false;
  },
  handleFocusPanelToggleClick() {
    if (!this.isConnected) {
      return;
    }
    if (this.isPolicyBlocked('block_focus_panel', '聚焦面板已被管理员禁用')) {
      return;
    }
    this.toggleFocusPanel();
  },
  handleApprovalPanelToggleClick() {
    console.log(
      '[UI_DEBUG] handleApprovalPanelToggleClick called, isMobileViewport:',
      this.isMobileViewport,
      'currentConversationId:',
      this.currentConversationId,
      'activeMobileOverlay:',
      this.activeMobileOverlay
    );
    if (!this.currentConversationId) {
      console.log('[UI_DEBUG] handleApprovalPanelToggleClick: blocked - no conversation');
      return;
    }
    if (this.isMobileViewport) {
      console.log('[UI_DEBUG] handleApprovalPanelToggleClick: mobile path, toggling overlay');
      // 手机端：获取审批列表（不自动关闭），然后切换遮罩
      this.fetchPendingToolApprovals();
      this.openMobileOverlay('approval');
      return;
    }
    console.log('[UI_DEBUG] handleApprovalPanelToggleClick: desktop path, toggling panel');
    this.toggleApprovalPanel();
  },
  handleTokenPanelToggleClick(fromSettingsMenu = false) {
    console.log(
      '[UI_DEBUG] handleTokenPanelToggleClick called, fromSettingsMenu:',
      fromSettingsMenu,
      'isMobileViewport:',
      this.isMobileViewport,
      'tokenPanelCollapsed:',
      this.tokenPanelCollapsed,
      'currentConversationId:',
      this.currentConversationId
    );
    if (!this.currentConversationId) {
      console.log('[UI_DEBUG] handleTokenPanelToggleClick: blocked - no conversation');
      return;
    }
    if (this.isPolicyBlocked('block_token_panel', '用量统计已被管理员禁用')) {
      console.log('[UI_DEBUG] handleTokenPanelToggleClick: blocked - policy');
      return;
    }
    // 移动端禁用“点击展开顶部用量面板”，仅允许在已展开时点击收起
    if (this.isMobileViewport && this.tokenPanelCollapsed && !fromSettingsMenu) {
      console.log('[UI_DEBUG] handleTokenPanelToggleClick: blocked - mobile viewport restriction');
      return;
    }
    console.log('[UI_DEBUG] handleTokenPanelToggleClick: calling toggleTokenPanel');
    this.toggleTokenPanel();
  },
  handleWorkspaceToggle() {
    if (this.isMobileViewport) {
      return;
    }
    if (this.isPolicyBlocked('collapse_workspace', '工作区已被管理员强制折叠')) {
      this.uiSetWorkspaceCollapsed(true);
      return;
    }
    const nextState = !this.workspaceCollapsed;
    this.uiSetWorkspaceCollapsed(nextState);
    if (nextState) {
      this.uiSetPanelMenuOpen(false);
    }
  },
  handleDisplayModeToggle() {
    if (this.displayModeSwitchDisabled) {
      // 显式提示管理员禁用
      this.isPolicyBlocked('block_virtual_monitor', '虚拟显示器已被管理员禁用');
      return;
    }
    if (
      this.chatDisplayMode === 'chat' &&
      this.isPolicyBlocked('block_virtual_monitor', '虚拟显示器已被管理员禁用')
    ) {
      return;
    }
    const next = this.chatDisplayMode === 'chat' ? 'monitor' : 'chat';
    this.uiSetChatDisplayMode(next);
  },
  getMessagesAreaElement() {
    const ref = this.$refs.messagesArea;
    if (!ref) {
      return null;
    }
    if (ref instanceof HTMLElement) {
      return ref;
    }
    if (ref.rootEl) {
      return ref.rootEl.value || ref.rootEl;
    }
    if (ref.$el && ref.$el.querySelector) {
      const el = ref.$el.querySelector('.messages-area');
      if (el) {
        return el;
      }
    }
    return null;
  },
  getChatAreaController() {
    const ref = this.$refs.messagesArea;
    if (!ref) return null;
    if (typeof ref === 'object') return ref;
    return null;
  },
  getThinkingContentElement(blockId) {
    const chatArea = this.$refs.messagesArea;
    if (chatArea && typeof chatArea.getThinkingRef === 'function') {
      const el = chatArea.getThinkingRef(blockId);
      if (el) {
        return el;
      }
    }
    const refName = `thinkingContent-${blockId}`;
    const elRef = this.$refs[refName];
    if (Array.isArray(elRef)) {
      return elRef[0] || null;
    }
    return elRef || null;
  }
};
