// Copied from:
// app.bsky.authFullAppClient
// chat.bsky.authFullChatClient

const BSKY_OAUTH_RPC_SCOPES = [
  "rpc:app.bsky.actor.getPreferences",
  "rpc:app.bsky.actor.getProfile",
  "rpc:app.bsky.actor.getProfiles",
  "rpc:app.bsky.actor.getSuggestions",
  "rpc:app.bsky.actor.putPreferences",
  "rpc:app.bsky.actor.searchActors",
  "rpc:app.bsky.actor.searchActorsTypeahead",
  "rpc:app.bsky.bookmark.createBookmark",
  "rpc:app.bsky.bookmark.deleteBookmark",
  "rpc:app.bsky.bookmark.getBookmarks",
  "rpc:app.bsky.contact.dismissMatch",
  "rpc:app.bsky.contact.getMatches",
  "rpc:app.bsky.contact.getSyncStatus",
  "rpc:app.bsky.contact.importContacts",
  "rpc:app.bsky.contact.removeData",
  "rpc:app.bsky.contact.startPhoneVerification",
  "rpc:app.bsky.contact.verifyPhone",
  "rpc:app.bsky.feed.describeFeedGenerator",
  "rpc:app.bsky.feed.getActorFeeds",
  "rpc:app.bsky.feed.getActorLikes",
  "rpc:app.bsky.feed.getAuthorFeed",
  "rpc:app.bsky.feed.getFeed",
  "rpc:app.bsky.feed.getFeedGenerator",
  "rpc:app.bsky.feed.getFeedGenerators",
  "rpc:app.bsky.feed.getFeedSkeleton",
  "rpc:app.bsky.feed.getLikes",
  "rpc:app.bsky.feed.getListFeed",
  "rpc:app.bsky.feed.getPostThread",
  "rpc:app.bsky.feed.getPosts",
  "rpc:app.bsky.feed.getQuotes",
  "rpc:app.bsky.feed.getRepostedBy",
  "rpc:app.bsky.feed.getSuggestedFeeds",
  "rpc:app.bsky.feed.getTimeline",
  "rpc:app.bsky.feed.searchPosts",
  "rpc:app.bsky.feed.sendInteractions",
  "rpc:app.bsky.graph.getActorStarterPacks",
  "rpc:app.bsky.graph.getBlocks",
  "rpc:app.bsky.graph.getFollowers",
  "rpc:app.bsky.graph.getFollows",
  "rpc:app.bsky.graph.getKnownFollowers",
  "rpc:app.bsky.graph.getList",
  "rpc:app.bsky.graph.getListBlocks",
  "rpc:app.bsky.graph.getListMutes",
  "rpc:app.bsky.graph.getLists",
  "rpc:app.bsky.graph.getListsWithMembership",
  "rpc:app.bsky.graph.getMutes",
  "rpc:app.bsky.graph.getRelationships",
  "rpc:app.bsky.graph.getStarterPack",
  "rpc:app.bsky.graph.getStarterPacks",
  "rpc:app.bsky.graph.getStarterPacksWithMembership",
  "rpc:app.bsky.graph.getSuggestedFollowsByActor",
  "rpc:app.bsky.graph.muteActor",
  "rpc:app.bsky.graph.muteActorList",
  "rpc:app.bsky.graph.muteThread",
  "rpc:app.bsky.graph.searchStarterPacks",
  "rpc:app.bsky.graph.unmuteActor",
  "rpc:app.bsky.graph.unmuteActorList",
  "rpc:app.bsky.graph.unmuteThread",
  "rpc:app.bsky.labeler.getServices",
  "rpc:app.bsky.notification.getPreferences",
  "rpc:app.bsky.notification.getUnreadCount",
  "rpc:app.bsky.notification.listActivitySubscriptions",
  "rpc:app.bsky.notification.listNotifications",
  "rpc:app.bsky.notification.putActivitySubscription",
  "rpc:app.bsky.notification.putPreferences",
  "rpc:app.bsky.notification.putPreferencesV2",
  "rpc:app.bsky.notification.registerPush",
  "rpc:app.bsky.notification.unregisterPush",
  "rpc:app.bsky.notification.updateSeen",
  "rpc:app.bsky.unspecced.getAgeAssuranceState",
  "rpc:app.bsky.unspecced.getConfig",
  "rpc:app.bsky.unspecced.getOnboardingSuggestedStarterPacks",
  "rpc:app.bsky.unspecced.getPopularFeedGenerators",
  "rpc:app.bsky.unspecced.getPostThreadOtherV2",
  "rpc:app.bsky.unspecced.getPostThreadV2",
  "rpc:app.bsky.unspecced.getSuggestedFeeds",
  "rpc:app.bsky.unspecced.getSuggestedFeedsSkeleton",
  "rpc:app.bsky.unspecced.getSuggestedStarterPacks",
  "rpc:app.bsky.unspecced.getSuggestedStarterPacksSkeleton",
  "rpc:app.bsky.unspecced.getSuggestedUsers",
  "rpc:app.bsky.unspecced.getSuggestedUsersSkeleton",
  "rpc:app.bsky.unspecced.getSuggestionsSkeleton",
  "rpc:app.bsky.unspecced.getTaggedSuggestions",
  "rpc:app.bsky.unspecced.getTrendingTopics",
  "rpc:app.bsky.unspecced.getTrends",
  "rpc:app.bsky.unspecced.getTrendsSkeleton",
  "rpc:app.bsky.unspecced.initAgeAssurance",
  "rpc:app.bsky.unspecced.searchActorsSkeleton",
  "rpc:app.bsky.unspecced.searchPostsSkeleton",
  "rpc:app.bsky.unspecced.searchStarterPacksSkeleton",
  "rpc:app.bsky.video.getJobStatus",
  "rpc:app.bsky.video.getUploadLimits",
  "rpc:app.bsky.video.uploadVideo",
];

const BSKY_OAUTH_REPO_SCOPES = [
  "repo:app.bsky.actor.profile",
  "repo:app.bsky.actor.status",
  "repo:app.bsky.feed.like",
  "repo:app.bsky.feed.post",
  "repo:app.bsky.feed.postgate",
  "repo:app.bsky.feed.repost",
  "repo:app.bsky.feed.threadgate",
  "repo:app.bsky.graph.block",
  "repo:app.bsky.graph.follow",
  "repo:app.bsky.graph.list",
  "repo:app.bsky.graph.listblock",
  "repo:app.bsky.graph.listitem",
  "repo:app.bsky.graph.starterpack",
  "repo:app.bsky.notification.declaration",
];

const CHAT_OAUTH_RPC_SCOPES = [
  "rpc:chat.bsky.actor.deleteAccount",
  "rpc:chat.bsky.convo.acceptConvo",
  "rpc:chat.bsky.convo.addReaction",
  "rpc:chat.bsky.convo.deleteMessageForSelf",
  "rpc:chat.bsky.convo.exportAccountData",
  "rpc:chat.bsky.convo.getConvo",
  "rpc:chat.bsky.convo.getConvoAvailability",
  "rpc:chat.bsky.convo.getConvoForMembers",
  "rpc:chat.bsky.convo.getLog",
  "rpc:chat.bsky.convo.getMessages",
  "rpc:chat.bsky.convo.getUnreadCounts",
  "rpc:chat.bsky.convo.leaveConvo",
  "rpc:chat.bsky.convo.listConvos",
  "rpc:chat.bsky.convo.muteConvo",
  "rpc:chat.bsky.convo.removeReaction",
  "rpc:chat.bsky.convo.sendMessage",
  "rpc:chat.bsky.convo.sendMessageBatch",
  "rpc:chat.bsky.convo.unmuteConvo",
  "rpc:chat.bsky.convo.updateAllRead",
  "rpc:chat.bsky.convo.updateRead",
  "rpc:chat.bsky.group.getJoinLinkPreviews",
  "rpc:chat.bsky.group.requestJoin",
];

const CHAT_OAUTH_REPO_SCOPES = ["repo:chat.bsky.actor.declaration"];

const ATPROTO_OAUTH_RPC_SCOPES = [
  "rpc:com.atproto.moderation.createReport",
  "rpc:com.atproto.repo.uploadBlob",
];

const OPTIONAL_OAUTH_RPC_SCOPES = [
  "rpc:chat.bsky.convo.getConvoMembers",
  "rpc:app.bsky.draft.getDrafts",
  "rpc:app.bsky.draft.createDraft",
  "rpc:app.bsky.draft.updateDraft",
  "rpc:app.bsky.draft.deleteDraft",
];

function expandScope(scope) {
  if (scope.startsWith("rpc:")) {
    return [scope + "?aud=*"];
  }
  if (scope.startsWith("repo:")) {
    return ["create", "update", "delete"].map(
      (action) => scope + "?action=" + action,
    );
  }
  return [scope];
}

function expandScopes(scopes) {
  return scopes.flatMap(expandScope);
}

function buildOauthScopesString() {
  const scopes = [
    "atproto",
    "blob:*/*",
    ...expandScopes(BSKY_OAUTH_RPC_SCOPES),
    ...expandScopes(CHAT_OAUTH_RPC_SCOPES),
    ...expandScopes(ATPROTO_OAUTH_RPC_SCOPES),
    ...expandScopes(BSKY_OAUTH_REPO_SCOPES),
    ...expandScopes(CHAT_OAUTH_REPO_SCOPES),
    ...expandScopes(OPTIONAL_OAUTH_RPC_SCOPES),
  ];
  return scopes.join(" ");
}

export const OAUTH_SCOPES = buildOauthScopesString();
export const OPTIONAL_OAUTH_SCOPES = expandScopes(
  OPTIONAL_OAUTH_RPC_SCOPES,
).join(" ");
