import { getActiveClient } from './activeClient';

/**
 * ```js
 * import { Client } from '@amityco/ts-sdk'
 * const unsubscribe = Client.onRTEConnectionStateChange((state) => console.log(state))
 * unsubscribe()
 * ```
 * @param callback The function to call when the event was fired
 * @returns an {@link Amity.Unsubscriber} function to stop listening
 *
 * @category Client API
 */
export const onRTEConnectionStateChange = (
  callback: (state: Amity.RTEConnectionState) => void,
): (() => void) | undefined => {
  const client = getActiveClient();

  if (client.mqtt) {
    return client.mqtt?.listen(callback);
  }

  return undefined;
};
