export const createWebSocketMock = () => {
  class WebSocketMock {
    constructor(url) {
      this.url = url;
      this.readyState = WebSocket.OPEN;
      this.listeners = {};
      this.count = 0;

      console.log('Mock WebSocket connected to:', url);

      // 模拟服务器推送消息
      this.intervalId = setInterval(() => {
        this.count += 1;
        if (this.count > 5) {
          this.count = 0;
          clearInterval(this.intervalId);
          return;
        }
        this.sendMessage(JSON.stringify(logStreamData));
      }, 3000);
    }

    sendMessage(data) {
      if (this.listeners['message']) {
        this.listeners['message'].forEach((callback) => callback({ data }));
      }
    }

    addEventListener(event, callback) {
      if (!this.listeners[event]) {
        this.listeners[event] = [];
      }
      this.listeners[event].push(callback);
    }

    removeEventListener(event, callback) {
      if (this.listeners[event]) {
        this.listeners[event] = this.listeners[event].filter((cb) => cb !== callback);
      }
    }

    close() {
      this.readyState = WebSocket.CLOSED;
      console.log('Mock WebSocket closed');
    }
  }

  return WebSocketMock;
};

export const logStreamData = {
  streams: [
    {
      stream: {
        workflows_argoproj_io_completed: 'false',
        workflows_argoproj_io_workflow: 'workflow-p2ddj',
        container: 'init',
        filename:
          '/var/log/pods/argo_workflow-p2ddj-git-clone-f33abcda-3988047653_e31cf6be-e013-4885-9eb6-ec84f83b9ba9/init/0.log',
        job: 'argo/workflow-p2ddj-git-clone-f33abcda-3988047653',
        namespace: 'argo',
        pod: 'workflow-p2ddj-git-clone-f33abcda-3988047653',
        stream: 'stderr',
      },
      values: [
        [
          '1742179591969785990',
          'time="2025-03-17T02:46:31.969Z" level=info msg="Starting Workflow Executor" version=v3.5.10\n',
        ],
      ],
    },
    {
      stream: {
        filename:
          '/var/log/pods/argo_workflow-p2ddj-git-clone-f33abcda-3988047653_e31cf6be-e013-4885-9eb6-ec84f83b9ba9/init/0.log',
        job: 'argo/workflow-p2ddj-git-clone-f33abcda-3988047653',
        namespace: 'argo',
        pod: 'workflow-p2ddj-git-clone-f33abcda-3988047653',
        stream: 'stderr',
        workflows_argoproj_io_completed: 'false',
        workflows_argoproj_io_workflow: 'workflow-p2ddj',
        container: 'init',
      },
      values: [
        [
          '1742179591973414064',
          'time="2025-03-17T02:46:31.973Z" level=info msg="Using executor retry strategy" Duration=1s Factor=1.6 Jitter=0.5 Steps=5\n',
        ],
      ],
    },
  ],
};
