import { AccountAvatarRecord } from '../../../../../../server-services/query-records/account-records';
import { getAccountQuery, getAllAccountIdQuery } from '../../../../../../server-services/querys';
import { ResponseMergerHandler } from '../../../cache-logic-interface';

export let customAccountAvatarsQueryMerger: ResponseMergerHandler = function (
  cache,
  request,
  response: AccountAvatarRecord[]
) {
  let allAccountIds = cache.getCacheDataByRequest({ query: getAllAccountIdQuery, variables: {} });

  response.forEach((accountAvatar) => {
    let cacheKey = cache
      .getCacheLogic()
      .makeCacheDataPath({ query: getAccountQuery, variables: { id: accountAvatar.id } });
    cache.setCacheData(cacheKey, accountAvatar);
    if (!allAccountIds.includes(accountAvatar.id)) {
      cache
        .getCacheDataByRequest({ query: getAllAccountIdQuery, variables: {} })
        .push(accountAvatar.id);
    }
  });
};
