import type { BobbinContext, XrpcRequestInit } from "./client";
import { jsonGet } from "./_request";

// count endpoints share { subject } params and { count, distinctAuthors } output.
export type CountName =
	| "sh.tangled.feed.countStars"
	| "sh.tangled.feed.countStarsBy"
	| "sh.tangled.feed.countComments"
	| "sh.tangled.feed.countCommentsBy"
	| "sh.tangled.feed.countReactions"
	| "sh.tangled.feed.countReactionsBy"
	| "sh.tangled.graph.countFollows"
	| "sh.tangled.graph.countFollowsBy"
	| "sh.tangled.graph.countVouches"
	| "sh.tangled.graph.countVouchesBy"
	| "sh.tangled.git.countRefUpdates"
	| "sh.tangled.git.countRefUpdatesBy"
	| "sh.tangled.knot.countKnots"
	| "sh.tangled.knot.countMembers"
	| "sh.tangled.knot.countMembersBy"
	| "sh.tangled.label.countDefinitions"
	| "sh.tangled.label.countOps"
	| "sh.tangled.label.countOpsBy"
	| "sh.tangled.pipeline.countPipelines"
	| "sh.tangled.pipeline.countPipelinesBy"
	| "sh.tangled.pipeline.countStatuses"
	| "sh.tangled.pipeline.countStatusesBy"
	| "sh.tangled.publicKey.countKeys"
	| "sh.tangled.repo.countArtifacts"
	| "sh.tangled.repo.countArtifactsBy"
	| "sh.tangled.repo.countCollaborators"
	| "sh.tangled.repo.countCollaboratorsBy"
	| "sh.tangled.repo.countForks"
	| "sh.tangled.repo.countIssues"
	| "sh.tangled.repo.countIssuesBy"
	| "sh.tangled.repo.countPulls"
	| "sh.tangled.repo.countPullsBy"
	| "sh.tangled.repo.countRepos"
	| "sh.tangled.repo.issue.countStates"
	| "sh.tangled.repo.issue.countStatesBy"
	| "sh.tangled.repo.pull.countStatuses"
	| "sh.tangled.repo.pull.countStatusesBy"
	| "sh.tangled.spindle.countSpindles"
	| "sh.tangled.spindle.countMembers"
	| "sh.tangled.spindle.countMembersBy"
	| "sh.tangled.string.countStrings";

export interface CountResult {
	count: number;
	distinctAuthors: number;
}

export interface CountFilter {
	author?: string;
	state?: "open" | "closed";
	status?: "open" | "closed" | "merged";
}

export const count = (
	ctx: BobbinContext,
	name: CountName,
	subject: string,
	filter: CountFilter = {},
	init?: XrpcRequestInit
): Promise<CountResult> => jsonGet<CountResult>(ctx, name, { subject, ...filter }, init);
