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 findFirstUnansweredPendingUserInputQuestionIndex(
129 questions: ReadonlyArray<UserInputQuestion>,
130 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
131): number {
132 const unansweredIndex = questions.findIndex(
133 (question) => !resolvePendingUserInputAnswer(question, draftAnswers[question.id]),
134 );
135
136 return unansweredIndex === -1 ? Math.max(questions.length - 1, 0) : unansweredIndex;
137}
138
139export function derivePendingUserInputProgress(
140 questions: ReadonlyArray<UserInputQuestion>,
141 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
142 questionIndex: number,
143): PendingUserInputProgress {
144 const normalizedQuestionIndex =
145 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
146 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
147 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
148 const resolvedAnswer = activeQuestion
149 ? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
150 : null;
151 const customAnswer =
152 activeQuestion?.allowCustomAnswer === false ? "" : (activeDraft?.customAnswer ?? "");
153 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
154 const isLastQuestion =
155 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
156
157 return {
158 questionIndex: normalizedQuestionIndex,
159 activeQuestion,
160 activeDraft,
161 selectedOptionValues: normalizeSelectedOptionValues(activeDraft?.selectedOptionValues),
162 customAnswer,
163 resolvedAnswer,
164 usingCustomAnswer: customAnswer.trim().length > 0,
165 answeredQuestionCount,
166 isLastQuestion,
167 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
168 canAdvance: resolvedAnswer !== null,
169 };
170}