Open History to see the last commit
1import type { UserInputQuestion } from "@t3tools/contracts";
2
3export interface PendingUserInputDraftAnswer {
4 selectedOptionLabels?: string[];
5 customAnswer?: string;
6}
7
8export interface PendingUserInputProgress {
9 questionIndex: number;
10 activeQuestion: UserInputQuestion | null;
11 activeDraft: PendingUserInputDraftAnswer | undefined;
12 selectedOptionLabels: 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 normalizeSelectedOptionLabels(value: string[] | undefined): string[] {
32 if (!Array.isArray(value)) {
33 return [];
34 }
35
36 const normalized = value
37 .filter((entry): entry is string => typeof entry === "string")
38 .map((entry) => entry.trim())
39 .filter((entry) => entry.length > 0);
40
41 return Array.from(new Set(normalized));
42}
43
44export function resolvePendingUserInputAnswer(
45 question: UserInputQuestion,
46 draft: PendingUserInputDraftAnswer | undefined,
47): string | string[] | null {
48 const customAnswer = normalizeDraftAnswer(draft?.customAnswer);
49 if (customAnswer) {
50 return customAnswer;
51 }
52
53 const selectedOptionLabels = normalizeSelectedOptionLabels(draft?.selectedOptionLabels);
54 if (question.multiSelect) {
55 return selectedOptionLabels.length > 0 ? selectedOptionLabels : null;
56 }
57
58 return selectedOptionLabels[0] ?? null;
59}
60
61export function setPendingUserInputCustomAnswer(
62 draft: PendingUserInputDraftAnswer | undefined,
63 customAnswer: string,
64): PendingUserInputDraftAnswer {
65 const selectedOptionLabels =
66 customAnswer.trim().length > 0
67 ? undefined
68 : normalizeSelectedOptionLabels(draft?.selectedOptionLabels);
69
70 return {
71 customAnswer,
72 ...(selectedOptionLabels && selectedOptionLabels.length > 0 ? { selectedOptionLabels } : {}),
73 };
74}
75
76export function togglePendingUserInputOptionSelection(
77 question: UserInputQuestion,
78 draft: PendingUserInputDraftAnswer | undefined,
79 optionLabel: string,
80): PendingUserInputDraftAnswer {
81 if (question.multiSelect) {
82 const selectedOptionLabels = normalizeSelectedOptionLabels(draft?.selectedOptionLabels);
83 const nextSelectedOptionLabels = selectedOptionLabels.includes(optionLabel)
84 ? selectedOptionLabels.filter((label) => label !== optionLabel)
85 : [...selectedOptionLabels, optionLabel];
86
87 return {
88 customAnswer: "",
89 ...(nextSelectedOptionLabels.length > 0
90 ? { selectedOptionLabels: nextSelectedOptionLabels }
91 : {}),
92 };
93 }
94
95 return {
96 customAnswer: "",
97 selectedOptionLabels: [optionLabel],
98 };
99}
100
101export function buildPendingUserInputAnswers(
102 questions: ReadonlyArray<UserInputQuestion>,
103 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
104): Record<string, string | string[]> | null {
105 const answers: Record<string, string | string[]> = {};
106
107 for (const question of questions) {
108 const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
109 if (!answer) {
110 return null;
111 }
112 answers[question.id] = answer;
113 }
114
115 return answers;
116}
117
118export function countAnsweredPendingUserInputQuestions(
119 questions: ReadonlyArray<UserInputQuestion>,
120 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
121): number {
122 return questions.reduce((count, question) => {
123 return resolvePendingUserInputAnswer(question, draftAnswers[question.id]) ? count + 1 : count;
124 }, 0);
125}
126
127export function findFirstUnansweredPendingUserInputQuestionIndex(
128 questions: ReadonlyArray<UserInputQuestion>,
129 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
130): number {
131 const unansweredIndex = questions.findIndex(
132 (question) => !resolvePendingUserInputAnswer(question, draftAnswers[question.id]),
133 );
134
135 return unansweredIndex === -1 ? Math.max(questions.length - 1, 0) : unansweredIndex;
136}
137
138export function derivePendingUserInputProgress(
139 questions: ReadonlyArray<UserInputQuestion>,
140 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
141 questionIndex: number,
142): PendingUserInputProgress {
143 const normalizedQuestionIndex =
144 questions.length === 0 ? 0 : Math.max(0, Math.min(questionIndex, questions.length - 1));
145 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
146 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
147 const resolvedAnswer = activeQuestion
148 ? resolvePendingUserInputAnswer(activeQuestion, activeDraft)
149 : null;
150 const customAnswer = activeDraft?.customAnswer ?? "";
151 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
152 const isLastQuestion =
153 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
154
155 return {
156 questionIndex: normalizedQuestionIndex,
157 activeQuestion,
158 activeDraft,
159 selectedOptionLabels: normalizeSelectedOptionLabels(activeDraft?.selectedOptionLabels),
160 customAnswer,
161 resolvedAnswer,
162 usingCustomAnswer: customAnswer.trim().length > 0,
163 answeredQuestionCount,
164 isLastQuestion,
165 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
166 canAdvance: Boolean(resolvedAnswer),
167 };
168}