import React, { useEffect, useRef } from 'react';
// 根据你的项目结构调整导入路径
import LogSender from '../lib/browser-index.js';

const LogSenderComponent = ({ 
  serverUrl = 'http://localhost:3001', 
  appName = 'react-app',
  enabled = true 
}) => {
  const logSenderRef = useRef(null);
  const isInstalledRef = useRef(false);

  useEffect(() => {
    if (!enabled) return;

    // 创建 LogSender 实例
    logSenderRef.current = new LogSender(serverUrl, appName);
    
    // 标记已安装，避免 React StrictMode 重复安装
    if (!isInstalledRef.current) {
      logSenderRef.current.install();
      isInstalledRef.current = true;
      
      console.log('LogSender React 组件已初始化 - serverUrl:', serverUrl, 'appName:', appName);
    }

    // 清理函数：组件卸载时卸载 LogSender
    return () => {
      if (logSenderRef.current && isInstalledRef.current) {
        logSenderRef.current.uninstall();
        isInstalledRef.current = false;
        console.log('LogSender React 组件已卸载 - serverUrl:', serverUrl, 'appName:', appName);
      }
    };
  }, [serverUrl, appName, enabled]);

  // 这个组件不渲染任何内容
  return null;
};

export default LogSenderComponent;