import { observable } from "../_vendor/@legendapp/state/index.js";
import { Status } from "../types/core.js";
const queueStore$ = observable({
  // 失败消息队列（存储消息ID）
  failedQueue: [],
  // 发送中消息队列（存储消息ID）
  sendingQueue: [],
  // 流式传输中消息队列（存储消息ID）
  streamingQueue: [],
  // 待发送消息队列（存储完整的待发送消息对象）
  // 用于用户输入多条消息后批量发送
  pendingQueue: []
});
const queueComputed$ = observable({
  // 获取各队列的状态统计
  queueStatus: () => {
    return {
      sendingCount: queueStore$.sendingQueue.get().length,
      streamingCount: queueStore$.streamingQueue.get().length,
      failedCount: queueStore$.failedQueue.get().length,
      pendingCount: queueStore$.pendingQueue.get().length
    };
  },
  // 计算所有队列的总消息数
  totalQueueCount: () => {
    const queues = queueStore$;
    return queues.sendingQueue.get().length + queues.streamingQueue.get().length + queues.failedQueue.get().length + queues.pendingQueue.get().length;
  },
  // 是否有待发送的消息
  hasPendingMessages: () => {
    return queueStore$.pendingQueue.get().length > 0;
  },
  // 获取下一条待发送的消息（队列头部）
  nextPendingMessage: () => {
    const queue = queueStore$.pendingQueue.get();
    return queue.length > 0 ? queue[0] : null;
  },
  // 按优先级和时间排序的待发送消息
  sortedPendingMessages: () => {
    const queue = queueStore$.pendingQueue.get();
    return [...queue].sort((a, b) => {
      const priorityA = a.priority ?? 0;
      const priorityB = b.priority ?? 0;
      if (priorityA !== priorityB) {
        return priorityA - priorityB;
      }
      return new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime();
    });
  }
});
const queueActions = {
  // 将消息添加到指定队列
  enqueueMessage: (messageId, queueType) => {
    const queue = queueStore$[queueType].peek();
    if (!queue.includes(messageId)) {
      queueStore$[queueType].set([...queue, messageId]);
    }
  },
  // 从指定队列中移除消息
  dequeueMessage: (messageId, queueType) => {
    const queue = queueStore$[queueType].peek();
    queueStore$[queueType].set(queue.filter((id) => id !== messageId));
  },
  // 清空指定队列
  // ❌ 未使用
  clearQueue: (queueType) => {
    queueStore$[queueType].set([]);
  },
  // 清空所有队列
  clearAllQueues: () => {
    queueStore$.sendingQueue.set([]);
    queueStore$.streamingQueue.set([]);
    queueStore$.failedQueue.set([]);
    queueStore$.pendingQueue.set([]);
  },
  // 重置所有队列（clearAllQueues 的别名）
  // ❌ 未使用
  resetAllQueues: () => {
    queueStore$.sendingQueue.set([]);
    queueStore$.streamingQueue.set([]);
    queueStore$.failedQueue.set([]);
    queueStore$.pendingQueue.set([]);
  },
  // 获取队列状态（不包括 pendingQueue）
  // ❌ 未使用
  getQueueStatus: () => {
    return {
      sendingCount: queueStore$.sendingQueue.peek().length,
      streamingCount: queueStore$.streamingQueue.peek().length,
      failedCount: queueStore$.failedQueue.peek().length
    };
  },
  // 根据消息状态更新队列
  // ❌ 未使用
  updateMessageStatus: (messageId, status) => {
    const sendingQueue = queueStore$.sendingQueue.peek().filter((id) => id !== messageId);
    const streamingQueue = queueStore$.streamingQueue.peek().filter((id) => id !== messageId);
    const failedQueue = queueStore$.failedQueue.peek().filter((id) => id !== messageId);
    queueStore$.sendingQueue.set(sendingQueue);
    queueStore$.streamingQueue.set(streamingQueue);
    queueStore$.failedQueue.set(failedQueue);
    switch (status) {
      case Status.Sending:
        queueStore$.sendingQueue.set([...queueStore$.sendingQueue.peek(), messageId]);
        break;
      case Status.Streaming:
        queueStore$.streamingQueue.set([...queueStore$.streamingQueue.peek(), messageId]);
        break;
      case Status.Failed:
        queueStore$.failedQueue.set([...queueStore$.failedQueue.peek(), messageId]);
        break;
    }
  },
  // ============================================================================
  // 待发送消息队列管理
  // ============================================================================
  // 添加待发送消息到队列
  addPendingMessage: (message) => {
    const currentQueue = queueStore$.pendingQueue.peek();
    queueStore$.pendingQueue.set([...currentQueue, message]);
  },
  // 从队列中移除指定的待发送消息
  removePendingMessage: (messageId) => {
    const currentQueue = queueStore$.pendingQueue.peek();
    queueStore$.pendingQueue.set(currentQueue.filter((msg) => msg.id !== messageId));
  },
  // 清空待发送消息队列
  clearPendingQueue: () => {
    queueStore$.pendingQueue.set([]);
  },
  // 获取并移除下一条待发送消息（FIFO）
  dequeuePendingMessage: () => {
    const queue = queueStore$.pendingQueue.peek();
    if (queue.length === 0) return null;
    const nextMessage = queue[0];
    queueStore$.pendingQueue.set(queue.slice(1));
    return nextMessage;
  },
  // 更新待发送消息的优先级
  // ❌ 未使用
  updatePendingMessagePriority: (messageId, priority) => {
    const currentQueue = queueStore$.pendingQueue.peek();
    const updatedQueue = currentQueue.map(
      (msg) => msg.id === messageId ? { ...msg, priority } : msg
    );
    queueStore$.pendingQueue.set(updatedQueue);
  },
  // 移动待发送消息位置（用于拖拽排序）
  movePendingMessage: (fromIndex, toIndex) => {
    const currentQueue = queueStore$.pendingQueue.peek();
    if (fromIndex < 0 || fromIndex >= currentQueue.length || toIndex < 0 || toIndex >= currentQueue.length) {
      return;
    }
    const newQueue = [...currentQueue];
    const [movedItem] = newQueue.splice(fromIndex, 1);
    newQueue.splice(toIndex, 0, movedItem);
    queueStore$.pendingQueue.set(newQueue);
  },
  // 批量更新队列排序
  // ❌ 未使用
  reorderPendingQueue: (newOrder) => {
    queueStore$.pendingQueue.set(newOrder);
  },
  // 插队功能：将指定消息移到队列最前面
  // ❌ 未使用
  jumpQueue: (messageId) => {
    const currentQueue = queueStore$.pendingQueue.peek();
    const messageIndex = currentQueue.findIndex((msg) => msg.id === messageId);
    if (messageIndex > 0) {
      const newQueue = [...currentQueue];
      const [jumpMessage] = newQueue.splice(messageIndex, 1);
      newQueue.unshift(jumpMessage);
      queueStore$.pendingQueue.set(newQueue);
      return true;
    }
    return false;
  }
};
export {
  queueActions,
  queueComputed$,
  queueStore$
};
//# sourceMappingURL=queue-store.js.map
