import type { WebSocketLike } from '@hocuspocus/server';
import type { RSAMessageSend } from './redis-sync.types';

// Stands in for the client WebSocket on the server that owns the document.
// Outgoing traffic is relayed over redis to the origin server, which holds the real socket.
export class CollabProxySocket implements WebSocketLike {
  readyState = 1;
  onClose?: (code?: number, reason?: string) => void;

  constructor(
    private readonly socketId: string,
    private readonly publish: (message: RSAMessageSend) => void,
  ) {}

  // The origin server already closed the real socket; stop relaying without echoing a close back
  markClosed() {
    this.readyState = 3;
  }

  close(code?: number, reason?: string) {
    if (this.readyState !== 1) return;
    this.readyState = 3;
    this.onClose?.(code, reason);
  }

  send(message: Uint8Array) {
    if (this.readyState !== 1) return;
    const msg: RSAMessageSend = {
      type: 'send',
      socketId: this.socketId,
      message,
    };
    this.publish(msg);
  }
}
