1import type {
2 ExecutionEnvironmentCapabilities,
3 ThreadId,
4 ThreadPullRequestKey,
5} from "@t3tools/contracts";
6import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts";
7
8/** "unsupported" until the environment's server config has loaded. */
9export function threadPullRequestLinkMode(
10 capabilities: ExecutionEnvironmentCapabilities | null | undefined,
11) {
12 return capabilities ? ("multiple" as const) : ("unsupported" as const);
13}
14
15export function planThreadPullRequestMutation({
16 capabilities,
17 threadId,
18 reference,
19 linked,
20}: {
21 capabilities: ExecutionEnvironmentCapabilities | null | undefined;
22 threadId: ThreadId;
23 reference: ThreadPullRequestKey & { readonly url: string };
24 linked: boolean;
25}) {
26 const key = normalizeThreadPullRequestKey(reference);
27 switch (threadPullRequestLinkMode(capabilities)) {
28 case "multiple":
29 return linked
30 ? {
31 type: "thread.pull-request.link" as const,
32 input: { threadId, ...reference, ...key, source: "manual" as const },
33 }
34 : {
35 type: "thread.pull-request.unlink" as const,
36 input: {
37 threadId,
38 ...key,
39 },
40 };
41 case "unsupported":
42 return null;
43 }
44}