async function getAllConversations(adapter, userId) {
  const whereConditions = [];
  if (userId) {
    whereConditions.push({
      field: "userId",
      value: userId,
      operator: "eq"
    });
  }
  return adapter.findMany({
    model: "conversation",
    where: whereConditions.length > 0 ? whereConditions : void 0,
    sortBy: { field: "updatedAt", direction: "desc" }
  });
}
async function getConversationById(adapter, id) {
  const conversations = await adapter.findMany({
    model: "conversation",
    where: [{ field: "id", value: id, operator: "eq" }],
    limit: 1,
    join: {
      message: true
    }
  });
  if (!conversations.length) {
    return null;
  }
  const conversation = conversations[0];
  const messages = (conversation.message || []).sort(
    (a, b) => a.createdAt.getTime() - b.createdAt.getTime()
  );
  const { message: _, ...conversationWithoutJoin } = conversation;
  return {
    ...conversationWithoutJoin,
    messages
  };
}

export { getAllConversations, getConversationById };
