"use client"; import { useMemo } from "react"; import { IoAlert, IoCheckmark } from "react-icons/io5"; import { getLocalSectionProgress, getStoredAge } from "./progress-helper"; import { getQuestionListItems, isQuestionListItemVisibleForProfile, type QuestionListItem, } from "@/data/question-data"; import { toFrontendSlug } from "@/data/section-slug-map"; import { useMarriageProfileQuery } from "@/hooks/marriage/use-profile-main"; import { useMarriageSectionsQuery } from "@/hooks/marriage/use-sections"; import { useI18n } from "@/translations/provider"; type RequiredStepsCardProps = { items?: QuestionListItem[]; progressBySlug?: Map; }; type RequiredStep = { slug: string; required: boolean; progress: number; }; function getRequiredStepStats(steps: RequiredStep[]) { const requiredSteps = steps.filter((step) => step.required); const completedSteps = requiredSteps.filter((step) => step.progress >= 100); return { completed: completedSteps.length, total: requiredSteps.length, }; } export default function RequiredStepsCard({ items, progressBySlug, }: RequiredStepsCardProps = {}) { const { dictionary: t, locale } = useI18n(); const { data: profile } = useMarriageProfileQuery(); const { data: sections } = useMarriageSectionsQuery(); const questionListItems = useMemo( () => items ?? getQuestionListItems(locale).filter((item) => isQuestionListItemVisibleForProfile(item, { gender: profile?.gender, }), ), [items, locale, profile?.gender], ); const steps: RequiredStep[] = useMemo(() => { const age = getStoredAge(); type SectionType = NonNullable[number]; const sectionMap = new Map(); sections?.forEach((s) => { sectionMap.set(toFrontendSlug(s.slug), s); sectionMap.set(s.slug, s); }); return questionListItems.map((item) => { const localProgress = getLocalSectionProgress(item, profile, age); const section = sectionMap.get(item.slug); let progress = 0; if (localProgress !== null) { progress = localProgress; } else if ( progressBySlug && typeof progressBySlug.get(item.slug) === "number" ) { progress = progressBySlug.get(item.slug) ?? 0; } else if (item.slug === "family_marital_history" && sections) { const fbSec = sections.find((s) => s.slug === "family_background"); const mhSec = sections.find((s) => s.slug === "marital_history"); if (fbSec || mhSec) { const fbTotal = fbSec?.total_steps ?? 6; const fbCurrent = fbSec ? Math.round((fbSec.completion_percent / 100) * fbTotal) : 0; const mhTotal = mhSec?.total_steps ?? 6; const mhCurrent = mhSec ? Math.round((mhSec.completion_percent / 100) * mhTotal) : 0; progress = fbTotal + mhTotal > 0 ? Math.max( 0, Math.min( 100, Math.round( ((fbCurrent + mhCurrent) / (fbTotal + mhTotal)) * 100, ), ), ) : 0; } } else if (section) { progress = Math.max( 0, Math.min(100, Math.round(section.completion_percent)), ); } else { progress = item.progress; } return { slug: item.slug, required: Boolean(item.required), progress, }; }); }, [questionListItems, progressBySlug, sections, profile]); const { completed, total } = getRequiredStepStats(steps); const completion = total > 0 ? Math.round((completed / total) * 100) : 0; const isCompleted = total > 0 && completed === total; return (
{isCompleted ? (

{t.questions.requiredSteps}

{isCompleted ? t.questions.requiredStepsDescriptionCompleted : t.questions.requiredStepsDescription}

{completed}/{total}
); }