Open History to see the last commit
1import type { UserInputQuestion } from "@t3tools/contracts";
2
3export interface PendingUserInputDraftAnswer {
4 selectedOptionLabel?: string;
5 customAnswer?: string;
6}
7
8export interface PendingUserInputProgress {
9 questionIndex: number;
10 activeQuestion: UserInputQuestion | null;
11 activeDraft: PendingUserInputDraftAnswer | undefined;
12 selectedOptionLabel: string | undefined;
13 customAnswer: string;
14 resolvedAnswer: 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
31export function resolvePendingUserInputAnswer(
32 draft: PendingUserInputDraftAnswer | undefined,
33): string | null {
34 const customAnswer = normalizeDraftAnswer(draft?.customAnswer);
35 if (customAnswer) {
36 return customAnswer;
37 }
38
39 return normalizeDraftAnswer(draft?.selectedOptionLabel);
40}
41
42export function setPendingUserInputCustomAnswer(
43 draft: PendingUserInputDraftAnswer | undefined,
44 customAnswer: string,
45): PendingUserInputDraftAnswer {
46 const selectedOptionLabel =
47 customAnswer.trim().length > 0 ? undefined : draft?.selectedOptionLabel;
48
49 return {
50 customAnswer,
51 ...(selectedOptionLabel ? { selectedOptionLabel } : {}),
52 };
53}
54
55export function buildPendingUserInputAnswers(
56 questions: ReadonlyArray<UserInputQuestion>,
57 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
58): Record<string, string> | null {
59 const answers: Record<string, string> = {};
60
61 for (const question of questions) {
62 const answer = resolvePendingUserInputAnswer(draftAnswers[question.id]);
63 if (!answer) {
64 return null;
65 }
66 answers[question.id] = answer;
67 }
68
69 return answers;
70}
71
72export function countAnsweredPendingUserInputQuestions(
73 questions: ReadonlyArray<UserInputQuestion>,
74 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
75): number {
76 return questions.reduce((count, question) => {
77 return resolvePendingUserInputAnswer(draftAnswers[question.id]) ? count + 1 : count;
78 }, 0);
79}
80
81export function findFirstUnansweredPendingUserInputQuestionIndex(
82 questions: ReadonlyArray<UserInputQuestion>,
83 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
84): number {
85 const unansweredIndex = questions.findIndex(
86 (question) => !resolvePendingUserInputAnswer(draftAnswers[question.id]),
87 );
88
89 return unansweredIndex === -1 ? Math.max(questions.length - 1, 0) : unansweredIndex;
90}
91
92export function derivePendingUserInputProgress(
93 questions: ReadonlyArray<UserInputQuestion>,
94 draftAnswers: Record<string, PendingUserInputDraftAnswer>,
95 questionIndex: number,
96): PendingUserInputProgress {
97 const normalizedQuestionIndex =
98 questions.length === 0
99 ? 0
100 : Math.max(0, Math.min(questionIndex, questions.length - 1));
101 const activeQuestion = questions[normalizedQuestionIndex] ?? null;
102 const activeDraft = activeQuestion ? draftAnswers[activeQuestion.id] : undefined;
103 const resolvedAnswer = resolvePendingUserInputAnswer(activeDraft);
104 const customAnswer = activeDraft?.customAnswer ?? "";
105 const answeredQuestionCount = countAnsweredPendingUserInputQuestions(questions, draftAnswers);
106 const isLastQuestion =
107 questions.length === 0 ? true : normalizedQuestionIndex >= questions.length - 1;
108
109 return {
110 questionIndex: normalizedQuestionIndex,
111 activeQuestion,
112 activeDraft,
113 selectedOptionLabel: activeDraft?.selectedOptionLabel,
114 customAnswer,
115 resolvedAnswer,
116 usingCustomAnswer: customAnswer.trim().length > 0,
117 answeredQuestionCount,
118 isLastQuestion,
119 isComplete: buildPendingUserInputAnswers(questions, draftAnswers) !== null,
120 canAdvance: Boolean(resolvedAnswer),
121 };
122}