import { dropFromCache } from '~/cache/api/dropFromCache';
import { getActiveClient } from '~/client/api/activeClient';
import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
import { JoinRequestListLiveCollectionController } from './getJoinRequestList/JoinRequestListLiveCollectionController';

/* begin_public_function
  id: community.getJoinRequestList
*/
/**
 * Get Join Requests
 *
 * @param params the query parameters
 * @param callback the callback to be called when the join request are updated
 * @returns joinRequest[]
 *
 * @category joinRequestList Live Collection
 *
 */
export const getJoinRequestList = (
  params: Amity.JoinRequestListLiveCollection,
  callback: Amity.LiveCollectionCallback<Amity.JoinRequest>,
  config?: Amity.LiveCollectionConfig,
) => {
  const { log, cache } = getActiveClient();

  if (!cache) {
    console.log(ENABLE_CACHE_MESSAGE);
  }

  const timestamp = Date.now();
  log(`getJoinRequestList: (tmpid: ${timestamp}) > listen`);

  const joinRequestListLiveCollection = new JoinRequestListLiveCollectionController(
    params,
    callback,
  );

  const disposers = joinRequestListLiveCollection.startSubscription();

  const cacheKey = joinRequestListLiveCollection.getCacheKey();

  disposers.push(() => {
    dropFromCache(cacheKey);
  });

  return () => {
    log(`getJoinRequestList (tmpid: ${timestamp}) > dispose`);
    disposers.forEach(fn => fn());
  };
};
/* end_public_function */
