export type ProfileQuestionAnswerValue = string | number | boolean | null; export interface EditableProfileQuestion { id: number; admin_only_edit: boolean; can_edit: boolean; } export interface DependentProfileQuestion { id: number; depends_on_question_id: number | null; depends_on_value: string | null; } export const answerToComparable = (value: ProfileQuestionAnswerValue): string | null => { if (value === null || value === undefined) { return null; } if (typeof value === 'boolean') { return value ? 'true' : 'false'; } return String(value); }; export const canEditProfileQuestion = ( question: EditableProfileQuestion, allowAdminManagedEdit = false ): boolean => { if (allowAdminManagedEdit) { return true; } if (!question.can_edit) { return false; } if (question.admin_only_edit) { return false; } return true; }; export const isProfileQuestionVisible = ( question: TQuestion, questionsById: Map, answers: Record ): boolean => { if (!question.depends_on_question_id) { return true; } const parentQuestion = questionsById.get(question.depends_on_question_id); if (!parentQuestion) { return true; } const parentAnswer = answerToComparable(answers[parentQuestion.id] ?? null); if (question.depends_on_value === null || question.depends_on_value === undefined) { return parentAnswer !== null && parentAnswer !== ''; } return parentAnswer === question.depends_on_value; };