// composables/useLargeDataOptimization.ts
export function useLargeDataOptimization() {
  // 数据分片加载
  const loadDataInChunks = async (totalPoints: number, chunkSize: number = 10000) => {
    const chunks = [];

    for (let i = 0; i < totalPoints; i += chunkSize) {
      const chunk = await fetchChunk(i, Math.min(chunkSize, totalPoints - i));
      chunks.push(chunk);

      // 分批渲染，避免界面卡顿
      if (i % (chunkSize * 5) === 0) {
        await new Promise(resolve => setTimeout(resolve, 0));
      }
    }

    return chunks.flat();
  };

  // 数据采样（大数据量时）
  const sampleData = (data: ThermalPoint[], sampleRate: number = 0.1) => {
    return data.filter(() => Math.random() < sampleRate);
  };

  // 使用 Web Worker 处理数据
  const processDataInWorker = (data: ThermalPoint[]) => {
    return new Promise((resolve) => {
      const worker = new Worker('./thermalWorker.js');
      worker.postMessage(data);
      worker.onmessage = (e) => resolve(e.data);
    });
  };

  return {
    loadDataInChunks,
    sampleData,
    processDataInWorker
  };
}
