Open History to see the last commit
1import type { UserInputQuestion } from "@t3tools/contracts";
2
3export interface PendingUserInputDraftAnswer {
4 selectedOptionValues?: string[];
5 customAnswer?: string;
6}
7
8export interface PendingUserInputProgress {
9 questionIndex: number;
10 activeQuestion: UserInputQuestion | null;
11 activeDraft: PendingUserInputDraftAnswer | undefined;
12 selectedOptionValues: string[];
13 customAnswer: string;
14 resolvedAnswer: string | string[] | null;
15 usingCustomAnswer: boolean;
16 answeredQuestionCount: number;
17 isLastQuestion: boolean;
18 isComplete: boolean;
19 canAdvance: boolean;
20}
21
22function normalizeDraftAnswer(value: string | undefined): string | null {
23 if (typeof value !== "string") {
24 return null;
25 }
26
27 const trimmed = value.trim();
28 return trimmed.length > 0 ? trimmed : null;
29}
30
31function normalizeSelectedOptionValues(value: string[] | undefined): string[] {
32 if (!Array.isArray(value)) {
33 return [];
34 }
35
36 // Provider option IDs must stay unchanged, including whitespace.
37 return Array.from(new Set(value.filter((entry) => typeof entry === "string")));
38}
39
40export function resolvePendingUserInputAnswer(
41 question: UserInputQuestion,
42 draft: PendingUserInputDraftAnswer | undefined,
43): string | string[] | null {
44 const customAnswer =
45 question.allowCustomAnswer === false ? null : normalizeDraftAnswer(draft?.customAnswer);
46 if (customAnswer) {
47 return customAnswer;
48 }
49
50 const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues).filter(
51 (value) => question.options.some((option) => (option.value ?? option.label) === value),
52 );
53 if (question.multiSelect) {
54 return selectedOptionValues.length > 0 ? selectedOptionValues : null;
55 }
56
57 return selectedOptionValues[0] ?? null;
58}
59
60export function setPendingUserInputCustomAnswer(
61 draft: PendingUserInputDraftAnswer | undefined,
62 customAnswer: string,
63): PendingUserInputDraftAnswer {
64 const selectedOptionValues =
65 customAnswer.trim().length > 0
66 ? undefined
67 : normalizeSelectedOptionValues(draft?.selectedOptionValues);
68
69 return {
70 customAnswer,
71 ...(selectedOptionValues && selectedOptionValues.length > 0 ? { selectedOptionValues } : {}),
72 };
73}
74
75export function togglePendingUserInputOptionSelection(
76 question: UserInputQuestion,
77 draft: PendingUserInputDraftAnswer | undefined,
78 optionValue: string,
79): PendingUserInputDraftAnswer {
80 if (question.multiSelect) {
81 const selectedOptionValues = normalizeSelectedOptionValues(draft?.selectedOptionValues);
82 const nextSelectedOptionValues = selectedOptionValues.includes(optionValue)
83 ? selectedOptionValues.filter((value) => value !== optionValue)
84 : [...selectedOptionValues, optionValue];
85
86 return {
87 customAnswer: "",
88 ...(nextSelectedOptionValues.length > 0
89 ? { selectedOptionValues: nextSelectedOptionValues }
90 : {}),
91 };
92 }
93
94 return {
95 customAnswer: "",
96 selectedOptionValues: [optionValue],
97 };
98}
99
100export function buildPendingUserInputAnswers(
101 questions: ReadonlyArray<UserInputQuestion>,
102 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
103): Record<string, string | string[]> | null {
104 const answers: Record<string, string | string[]> = {};
105
106 for (const question of questions) {
107 const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
108 if (answer === null) {
109 return null;
110 }
111 answers[question.id] = answer;
112 }
113
114 return answers;
115}
116
117export function countAnsweredPendingUserInputQuestions(
118 questions: ReadonlyArray<UserInputQuestion>,
119 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
120): number {
121 return questions.reduce((count, question) => {
122 return resolvePendingUserInputAnswer(question, draftAnswers[question.id]) !== null
123 ? count + 1
124 : count;
125 }, 0);
126}
127
128export function derivePendingUserInputProgress(
129 questions: ReadonlyArray<UserInputQuestion>,
130 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
131 questionIndex: number,
132): PendingUserInputProgress {
133 const normalizedQuestionIndex =
134 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
135 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
136 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
137 const resolvedAnswer = activeQuestion
138 ? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
139 : null;
140 const customAnswer =
141 activeQuestion?.allowCustomAnswer === false ? "" : (activeDraft?.customAnswer ?? "");
142 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
143 const isLastQuestion =
144 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
145
146 return {
147 questionIndex: normalizedQuestionIndex,
148 activeQuestion,
149 activeDraft,
150 selectedOptionValues: normalizeSelectedOptionValues(activeDraft?.selectedOptionValues),
151 customAnswer,
152 resolvedAnswer,
153 usingCustomAnswer: customAnswer.trim().length > 0,
154 answeredQuestionCount,
155 isLastQuestion,
156 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
157 canAdvance: resolvedAnswer !== null,
158 };
159}