1import { type UserInputAttachmentAnswerPayload } from "@t3tools/contracts";
2
3function record(value: unknown): Record<string, unknown> | undefined {
4 return value !== null && typeof value === "object" && !Array.isArray(value)
5 ? (value as Record<string, unknown>)
6 : undefined;
7}
8
9export function getQuestionAnswerText(value: unknown): string {
10 if (typeof value === "string") return value;
11 if (Array.isArray(value)) return value.map(getQuestionAnswerText).filter(Boolean).join(", ");
12 const nested = record(value);
13 return nested ? getQuestionAnswerText(nested.answers) : "";
14}
15
16export function getQuestionTextPreview(answer: UserInputAttachmentAnswerPayload): string {
17 return Object.values(answer.questionTextById ?? {})
18 .map((text) => text.replace(/\s+/g, " ").trim())
19 .filter(Boolean)
20 .join(" · ");
21}
22
23export function getQuestionAnswerPreview(answer: UserInputAttachmentAnswerPayload): string {
24 const answers = Object.values(answer.answers).map(getQuestionAnswerText).filter(Boolean);
25 const attachments = Object.values(answer.attachmentsByQuestionId)
26 .flat()
27 .map((attachment) => attachment.name);
28 return (
29 answers.length > 0
30 ? answers.join(" · ")
31 : attachments.length > 0
32 ? attachments.join(", ")
33 : Object.values(answer.questionTextById ?? {}).join(" · ")
34 )
35 .replace(/\s+/g, " ")
36 .trim();
37}
38
39export function hasQuestionAnswer(answer: UserInputAttachmentAnswerPayload): boolean {
40 return (
41 Object.values(answer.answers).some(getQuestionAnswerText) ||
42 Object.values(answer.attachmentsByQuestionId).some((attachments) => attachments.length > 0)
43 );
44}