export default async function FcmHandler({ client, payload }) {
  switch (payload.type) {
    case "messaging-inbound":
      client.cache.modify({
        id: client.cache.identify({
          __typename: "conversations",
          id: payload.conversationid,
        }),
        fields: {
          messages_aggregate(cached) {
            return { aggregate: { count: cached.aggregate.count + 1 } };
          },
        },
      });
      break;
    case "messaging-outbound":
      client.cache.modify({
        id: client.cache.identify({
          __typename: "conversations",
          id: payload.conversationid,
        }),
        fields: {
          updated_at(oldupdated0) {
            return new Date();
          },
          // messages_aggregate(cached) {
          //   return { aggregate: { count: cached.aggregate.count + 1 } };
          // },
        },
      });
      break;
    case "messaging-mark-conversation-read":
      client.cache.modify({
        id: client.cache.identify({
          __typename: "conversations",
          id: payload.conversationid,
        }),
        fields: {
          messages_aggregate(cached) {
            return { aggregate: { count: 0 } };
          },
        },
      });
      break;
    default:
      console.log("No payload type set.");
      break;
  }
}
