import { PaginationController } from '~/core/liveCollection/PaginationController';
import { COLLECTION_DEFAULT_PAGINATION_LIMIT } from '~/utils/constants';

/**
 * TODO: handle cache receive cache option, and cache policy
 * TODO: check if querybyIds is supported
 */
export class JoinRequestListPaginationController extends PaginationController<
  'joinRequest',
  Amity.JoinRequestListLiveCollection
> {
  async getRequest(queryParams: Amity.JoinRequestListLiveCollection, token: string | undefined) {
    const { limit = COLLECTION_DEFAULT_PAGINATION_LIMIT, communityIds, ...params } = queryParams;
    const options = token ? { token } : { limit };

    const encodedChannelIds = communityIds.map(communityId => encodeURIComponent(communityId));

    const { data: queryResponse } = await this.http.get<
      Amity.CommunityJoinRequestPayload & Amity.Pagination
    >(`/api/v4/communities/join/requests`, {
      params: {
        ...params,
        options,
        communityIds: encodedChannelIds,
      },
    });

    return queryResponse;
  }
}
