import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './router/index';
import App from './App';

// 添加electron全局对象检查和降级方案
if (typeof window !== 'undefined' && !window.electron) {
  console.warn('Electron API 不可用，使用模拟实现');
  // 在浏览器环境中提供一个模拟的electron对象
  window.electron = {
    // IPC通信
    ipcRenderer: {
      send: (channel, ...args) => {
        console.warn(`[Electron模拟] 发送消息到通道 ${channel}`, args);
      },
      invoke: (channel, ...args) => {
        console.warn(`[Electron模拟] 调用 ${channel}`, args);
        // 返回不同API的模拟数据
        if (channel === 'save-audio-file') {
          return Promise.resolve({
            success: true,
            filePath: '/模拟路径/audio.mp3'
          });
        } else if (channel === 'process-project-audio') {
          return Promise.resolve({
            success: true,
            totalSegments: args[0]?.segments?.length || 0,
            processedSegments: 0,
            segments: []
          });
        } else if (channel === 'get-audio-duration') {
          return Promise.resolve({
            success: true,
            duration: 60 // 默认返回60秒
          });
        } else if (channel === 'get-app-version') {
          return Promise.resolve('1.0.0');
        } else if (channel === 'open-directory-dialog') {
          return Promise.resolve({ canceled: false, filePaths: ['/模拟目录路径'] });
        } else if (channel === 'open-file-path' || channel === 'open-url' || channel === 'get-file-stats') {
          return Promise.resolve(null);
        }
        return Promise.resolve(null);
      },
      on: (channel, func) => {
        console.warn(`[Electron模拟] 监听通道 ${channel}`);
        return () => {}; // 返回清理函数
      },
      once: (channel, func) => {
        console.warn(`[Electron模拟] 一次性监听通道 ${channel}`);
      }
    },
    // 模拟系统相关API
    system: {
      getAppVersion: () => Promise.resolve('1.0.0')
    },
    // 模拟文件操作API
    files: {
      openDirectoryDialog: () => Promise.resolve({ canceled: false, filePaths: ['/模拟目录路径'] }),
      showItemInFolder: () => Promise.resolve(),
      getFileStats: () => Promise.resolve(null)
    },
    // 模拟外部链接API
    shell: {
      openExternal: () => Promise.resolve()
    }
  };
}

ReactDOM.render(<App />, document.getElementById('root'));
