"use strict";
// COMMENTED OUT BC: Was only used by RelaySocketListener, which is obsolete.
// import { injectable } from 'inversify';
// import { Subject, Observer, Observable, interval, fromEvent, timer } from 'rxjs';
// import { tap, delayWhen, distinctUntilChanged, retryWhen, share, takeWhile } from 'rxjs/operators';
// import { WebSocketSubject, WebSocketSubjectConfig } from 'rxjs/webSocket';
// import { IBetterLogger } from "@codiac.io/codiac-common";
// import { SocketAction } from './socket-contracts';
// (global as any).WebSocket = require('ws');
// @injectable()
// export class RxWebsocketSubject<T> extends Subject<T> {
//   private data: any;
//   private reconnectionObservable: Observable<number> | undefined;
//   private wsSubjectConfig: WebSocketSubjectConfig<T>;
//   private socket: WebSocketSubject<any> | undefined;
//   private connectionObserver: Observer<boolean> | undefined;
//   public connectionStatus: Observable<boolean>;
//   defaultResultSelector = (e: MessageEvent | SocketAction) => {
//     if ((<any>e).data) return JSON.parse((<any>e).data);
//   }
//   defaultSerializer = (data: any): string => {
//     this.logger.debug("deserializing...")
//     return JSON.stringify(data);
//   }
//   constructor(
//     private url: string,
//     private reconnectInterval: number = 5000,
//     private reconnectAttempts: number = 10,
//     private resultSelector: (e: MessageEvent | SocketAction) => any,
//     private logger: IBetterLogger,
//     private serializer?: (data: any) => string,
//   ) {
//     super();
//     this.connectionStatus = new Observable<boolean>((observer) => {
//       this.connectionObserver = observer;
//     }).pipe(share(), distinctUntilChanged());
//     if (!resultSelector) {
//       this.resultSelector = this.defaultResultSelector;
//     }
//     if (!this.serializer) {
//       this.serializer = this.defaultSerializer;
//     }
//     this.wsSubjectConfig = {
//       url: url,
//       closeObserver: {
//         next: (e: CloseEvent) => {
//           this.logger.detail(`connection CLOSE [code: ${e.code}]`);
//           this.socket = undefined;
//           if (this.connectionObserver) {
//             this.connectionObserver.next(false);
//           } else {
//             return undefined;  // false?
//           }
//         }
//       },
//       openObserver: {
//         next: (e: Event) => {
//           this.logger.detail(`connection OPEN`);
//           if (this.connectionObserver) {
//             this.connectionObserver.next(true);
//           } else {
//             return undefined; // false?
//           }
//         }
//       }
//     };
//     this.connect();
//     this.connectionStatus.subscribe((isConnected) => {
//       this.logger.debug(`Connection status: ${isConnected ? "connected" : "not connected"}`);
//       if (!this.reconnectionObservable && typeof (isConnected) == "boolean" && !isConnected) {
//         this.reconnect();
//       }
//     });
//   }
//   connect(): void {
//     this.socket = new WebSocketSubject(this.wsSubjectConfig);
//     this.socket
//       .pipe(tap(msg => {
//         if (msg) {
//           if (msg.action === "connection" && this.cnxnHandler) {
//             try {
//               this.cnxnHandler(msg.error, msg.payload)
//             } catch (err) {
//               this.logger.warn(`Unhandled exception in connection handler: ${err.message ?? "(no details available)"}`)
//             }
//             // } else {
//             //   this.logger.debug(msg);
//           }
//         }
//       }))
//       .subscribe(
//         (m) => {
//           this.next(m);
//           this.resultSelector(m);
//         },
//         (error: Event) => {
//           this.logger.error(`Connection error: ${(<Error><any>error).message ?? ""}`);
//           if (!this.socket) {
//             this.reconnect();
//           }
//         });
//   }
//   private cnxnHandler: ((err: Error | undefined, data?: any) => void) | undefined;
//   public onSocketConnection(handler: (err: Error | undefined, data?: any) => void): void {
//     this.cnxnHandler = handler;
//   }
//   reconnect(): void {
//     this.reconnectionObservable = interval(this.reconnectInterval)
//       .pipe(takeWhile((v, index) => {
//         return index < this.reconnectAttempts && !this.socket
//       }));
//     this.reconnectionObservable.subscribe(
//       () => {
//         this.connect();
//         // this.send(this.data);
//       },
//       null,
//       () => {
//         this.reconnectionObservable = undefined;
//         if (!this.socket) {
//           this.complete();
//           if (this.connectionObserver) {
//             this.connectionObserver.complete();
//           } else {
//             return undefined;
//           }
//         }
//       });
//   }
//   send(data: any): void {
//     this.data = data;
//     if (this.data && this.socket) this.socket.next(this.data);
//   }
// }
//# sourceMappingURL=rx-websocket-subject.js.map