// 自选服务
import { generateFutureData, futuresList } from '../utils/mockData';

// 模拟自选列表存储
let watchlist: string[] = ['AU', 'AG', 'CU'];

// 获取自选列表
export const fetchWatchlist = async () => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  
  const watchlistData = watchlist.map(symbol => {
    const future = futuresList.find(item => item.code === symbol);
    if (!future) return null;
    const data = generateFutureData(symbol, future.name);
    return {
      symbol: data.code,
      name: data.name,
      price: data.currentPrice,
      change: data.changePercent
    };
  }).filter(Boolean);
  
  return watchlistData;
};

// 添加自选
export const addToWatchlist = async (symbol: string, name: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  
  if (!watchlist.includes(symbol)) {
    watchlist.push(symbol);
  }
  
  return { symbol, name };
};

// 删除自选
export const removeFromWatchlist = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  
  watchlist = watchlist.filter(item => item !== symbol);
  
  return { symbol };
};

// 检查自选状态
export const checkWatchlistStatus = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 100));
  
  return {
    is_in_watchlist: watchlist.includes(symbol)
  };
};