function buildPostTweetRequest(text, opts = {}) {
  return {
    status: text ?? "",
    ...(opts.mediaIds ? { media_ids: opts.mediaIds } : {}),
    ...(opts.replyTo ? { in_reply_to_status_id: opts.replyTo } : {}),
    auto_populate_reply_metadata: true,
    ...opts.postTweetRequest,
  };
}

export async function list(opts = {}) {
  const res = await this.graphql("FetchDraftTweets", {
    variables: { ascending: opts.ascending ?? false, ...opts.variables },
  });
  return res?.data?.viewer?.draft_list?.response_data ?? res;
}

export async function create(text, opts = {}) {
  return await this.graphql("CreateDraftTweet", {
    body: { variables: { post_tweet_request: buildPostTweetRequest(text, opts) } },
  });
}

export async function edit(draftId, text, opts = {}) {
  if (!draftId) throw new Error("edit requires a draftId");
  return await this.graphql("EditDraftTweet", {
    body: {
      variables: { draft_tweet_id: draftId, post_tweet_request: buildPostTweetRequest(text, opts) },
    },
  });
}

export async function remove(draftId) {
  if (!draftId) throw new Error("remove requires a draftId");
  return await this.graphql("DeleteDraftTweet", {
    body: { variables: { draft_tweet_id: draftId } },
  });
}
