import parseTimeline from "../parsers/timeline.js";

export async function get(articleEntityId) {
  if (!articleEntityId) throw new Error("get requires an articleEntityId");
  const res = await this.graphql("ArticleEntityResultByRestId", {
    variables: { articleEntityId },
  });
  return res?.data?.article_result_by_rest_id?.result ?? res;
}

export async function byUser(userId, opts = {}) {
  if (!userId) throw new Error("byUser requires a userId");
  const res = await this.graphql("UserArticlesTweets", {
    variables: {
      userId,
      count: opts.count || 20,
      cursor: opts.cursor,
      includePromotedContent: false,
      withVoice: false,
      ...opts.variables,
    },
  });
  if (opts.raw) return res;
  return parseTimeline(res);
}

export async function slice(userId, lifecycle = "Published", opts = {}) {
  if (!userId) throw new Error("slice requires a userId");
  const res = await this.graphql("ArticleEntitiesSlice", {
    variables: { userId, lifecycle, cursor: opts.cursor, ...opts.variables },
  });
  return res?.data ?? res;
}

export async function drafts(userId, opts = {}) {
  return await slice.call(this, userId, "Draft", opts);
}

export async function published(userId, opts = {}) {
  return await slice.call(this, userId, "Published", opts);
}

export async function createDraft(contentState, opts = {}) {
  if (!contentState) throw new Error("createDraft requires a content_state");
  return await this.graphql("ArticleEntityDraftCreate", {
    body: { variables: { content_state: contentState, ...opts.variables } },
  });
}

export async function updateTitle(articleEntityId, title) {
  if (!articleEntityId) throw new Error("updateTitle requires an articleEntityId");
  return await this.graphql("ArticleEntityUpdateTitle", {
    body: { variables: { articleEntityId, title } },
  });
}

export async function updateContent(articleEntityId, contentState) {
  if (!articleEntityId) throw new Error("updateContent requires an articleEntityId");
  return await this.graphql("ArticleEntityUpdateContent", {
    body: { variables: { articleEntityId, content_state: contentState } },
  });
}

export async function updateCoverMedia(articleEntityId, coverMediaId) {
  if (!articleEntityId) throw new Error("updateCoverMedia requires an articleEntityId");
  return await this.graphql("ArticleEntityUpdateCoverMedia", {
    body: { variables: { articleEntityId, cover_media_id: coverMediaId } },
  });
}

export async function publish(articleEntityId) {
  if (!articleEntityId) throw new Error("publish requires an articleEntityId");
  return await this.graphql("ArticleEntityPublish", {
    body: { variables: { articleEntityId } },
  });
}

export async function unpublish(articleEntityId) {
  if (!articleEntityId) throw new Error("unpublish requires an articleEntityId");
  return await this.graphql("ArticleEntityUnpublish", {
    body: { variables: { articleEntityId } },
  });
}

export async function remove(articleEntityId) {
  if (!articleEntityId) throw new Error("remove requires an articleEntityId");
  return await this.graphql("ArticleEntityDelete", {
    body: { variables: { articleEntityId } },
  });
}
