// @ts-nocheck
import { debugLog, traceLog } from './methods/common';
import { useConversationStore } from '../stores/conversation';

export const watchers = {
  multiAgentMode(newValue) {
    useConversationStore().$patch({ multiAgentMode: !!newValue });
  },
  inputMessage() {
    this.autoResizeInput();
    if (typeof this.scheduleComposerDraftPersist === 'function') {
      this.scheduleComposerDraftPersist('watch-input-message');
    }
  },
  messages: {
    deep: true,
    handler() {
      this.refreshBlankHeroState();
    }
  },
  composerBusy(newValue, oldValue) {
    if (oldValue && !newValue && typeof this.tryAutoSendRuntimeQueuedMessages === 'function') {
      this.tryAutoSendRuntimeQueuedMessages('watch-composer-idle');
    }
  },
  runtimeQueuedMessages: {
    deep: true,
    handler(list) {
      if (
        Array.isArray(list) &&
        list.length > 0 &&
        !this.composerBusy &&
        typeof this.tryAutoSendRuntimeQueuedMessages === 'function'
      ) {
        this.tryAutoSendRuntimeQueuedMessages('watch-runtime-queue');
      }
    }
  },
  runtimeGuidanceFallbackQueue: {
    deep: true,
    handler(list) {
      if (
        Array.isArray(list) &&
        list.length > 0 &&
        !this.composerBusy &&
        typeof this.tryAutoSendRuntimeQueuedMessages === 'function'
      ) {
        this.tryAutoSendRuntimeQueuedMessages('watch-runtime-guidance-fallback');
      }
    }
  },
  currentConversationTitle(newVal, oldVal) {
    const target = (newVal && newVal.trim()) || '';
    if (this.suppressTitleTyping) {
      this.titleTypingText = target;
      this.titleTypingTarget = target;
      return;
    }
    const previous =
      (oldVal && oldVal.trim()) || (this.titleTypingText && this.titleTypingText.trim()) || '';
    const placeholderPrev = !previous || previous === '新对话';
    const placeholderTarget = !target || target === '新对话';
    const animate = placeholderPrev && !placeholderTarget; // 仅从空/占位切换到真实标题时动画
    this.startTitleTyping(target, { animate });
  },
  currentConversationId: {
    immediate: false,
    handler(newValue, oldValue) {
      debugLog('currentConversationId 变化', {
        oldValue,
        newValue,
        skipConversationHistoryReload: this.skipConversationHistoryReload
      });
      traceLog('watch:currentConversationId', {
        oldValue,
        newValue,
        skipConversationHistoryReload: this.skipConversationHistoryReload,
        historyLoading: this.historyLoading,
        historyLoadingFor: this.historyLoadingFor,
        historyLoadSeq: this.historyLoadSeq
      });
      this.refreshBlankHeroState();
      this.logMessageState('watch:currentConversationId', {
        oldValue,
        newValue,
        skipConversationHistoryReload: this.skipConversationHistoryReload
      });
      if (
        oldValue !== newValue &&
        !this.taskInProgress &&
        !this.composerBusy &&
        typeof this.restoreComposerDraftState === 'function'
      ) {
        this.restoreComposerDraftState(
          `watch-conversation-id:${oldValue || 'none'}->${newValue || 'none'}`
        );
      }
      if (!newValue || typeof newValue !== 'string' || newValue.startsWith('temp_')) {
        this.versioningEnabled = false;
        this.versioningTrackingMode = 'conversation_only';
        return;
      }
      this.fetchVersioningStatus(newValue, { silent: true });
      if (this.skipConversationHistoryReload) {
        this.skipConversationHistoryReload = false;
        return;
      }
      if (oldValue && newValue === oldValue) {
        return;
      }
      this.fetchAndDisplayHistory();
      this.fetchConversationTokenStatistics();
      this.updateCurrentContextTokens();
    }
  },
  fileTree: {
    immediate: true,
    handler(newValue) {
      this.monitorSyncDesktop(newValue);
    }
  }
};
