import React from "react";
import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native";
import { Ionicons } from "@expo/vector-icons";
import { useRouter } from "expo-router";
import { moderateScale, verticalScale } from "react-native-size-matters";
import { Colors } from "@/theme";
import type { DetailRendererProps } from "../../types/renderer.types";
import {
buildContentReferenceRoute,
parseNavigableContentReference,
} from "../../utils/content-reference";
import { asRecord, asString } from "../helpers/value";
import { BulletList } from "../shared/BulletList";
import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections";
import { Hero } from "../shared/Hero";
import { Paragraphs } from "../shared/Paragraphs";
import { SectionCard } from "../shared/SectionCard";
function parseExplanationBullets(value: unknown): string[] {
if (!value) return [];
if (Array.isArray(value)) {
return value.map(asString).filter(Boolean);
}
const str = asString(value);
if (!str) return [];
if (str.includes("\n")) {
return str
.split(/\n+/)
.map((s) => s.trim().replace(/^\t+/, ""))
.filter(Boolean);
}
const regex =
/(?<=[^\s:,\+])(? s.trim().replace(/^\t+/, ""))
.filter(Boolean);
return parts.length > 0 ? parts : [str];
}
function parseTreatmentBullets(value: unknown): string[] {
if (!value) return [];
if (Array.isArray(value)) {
return value.map(asString).filter(Boolean);
}
const str = asString(value);
if (!str) return [];
if (str.includes("\n")) {
return str
.split(/\n+/)
.map((s) => s.trim().replace(/^\t+/, ""))
.filter(Boolean);
}
const items = str
.split(/,\s*(?![^()]*\))/)
.map((s) => s.trim().replace(/\.$/, ""))
.filter(Boolean);
return items.length > 0 ? items : [str];
}
export function FourLevelsDetailRenderer({
content,
data,
definitions,
presentationAttributes,
}: DetailRendererProps) {
const router = useRouter();
const title =
asString(data.name) ||
asString(data.title) ||
content.title;
const overview = asString(data.overview);
const rawPatterns = Array.isArray(data.patterns) ? data.patterns : [];
const hasPatterns = rawPatterns.length > 0;
const clinicalManifestations =
asString(data.clinical_manifestation) ||
asString(data.clinical_manifestations) ||
asString(data.clinical_menifestation);
const tongue = asString(data.tongue);
const pulse = asString(data.pulse);
const pathology = asString(data.pathology);
const treatmentPrinciple =
asString(data.treatment_principle) ||
asString(data["treatment principle"]);
const treatment = asString(data.treatment);
const treatmentBullets = parseTreatmentBullets(treatment);
const explanation = asString(data.explanation);
const explanationBullets = parseExplanationBullets(explanation);
const contentText = asString(data.content);
if (hasPatterns) {
return (
{/* Direct Overview Card matching Four Levels 1st page */}
{overview ? (
Overview
{overview}
) : null}
{/* Patterns List with numbered cards matching 1st page */}
Patterns
{rawPatterns.length} {rawPatterns.length === 1 ? "pattern" : "patterns"}
{rawPatterns.map((item, idx) => {
const reference = parseNavigableContentReference(item);
const raw = asRecord(item);
const patternTitle =
reference?.label?.trim() ||
asString(raw?.label) ||
asString(raw?.name) ||
`Pattern ${idx + 1}`;
const handlePress = () => {
if (reference) {
router.push(buildContentReferenceRoute(reference) as never);
} else if (raw && typeof raw.target === "object" && raw.target !== null) {
const target = raw.target as Record;
const mod = asString(target.module_code);
const tpl = asString(target.template_code);
const cid = asString(target.content_id);
if (mod && tpl && cid) {
router.push(
`/modules/${encodeURIComponent(mod)}/${encodeURIComponent(tpl)}/${encodeURIComponent(cid)}` as never,
);
}
}
};
return (
[styles.patternRow, pressed && styles.pressed]}
>
{idx + 1}
{patternTitle}
pattern
);
})}
);
}
return (
{overview ? (
) : null}
{clinicalManifestations ? (
) : null}
{tongue ? (
) : null}
{pulse ? (
) : null}
{pathology ? (
) : null}
{treatmentPrinciple ? (
) : null}
{treatment ? (
{treatmentBullets.length > 0 ? (
) : (
)}
) : null}
{explanation ? (
{explanationBullets.length > 0 ? (
) : (
)}
) : null}
{contentText ? (
) : null}
);
}
const styles = StyleSheet.create({
content: {
paddingHorizontal: moderateScale(16),
paddingTop: verticalScale(14),
paddingBottom: verticalScale(40),
gap: verticalScale(14),
},
overviewCard: {
backgroundColor: "#FFFFFF",
borderRadius: moderateScale(16),
borderWidth: 1,
borderColor: "#E2EAE5",
padding: moderateScale(16),
},
overviewHeader: {
flexDirection: "row",
alignItems: "center",
marginBottom: verticalScale(8),
gap: moderateScale(8),
},
overviewTitle: {
fontSize: moderateScale(16),
fontWeight: "700",
color: Colors.textPrimary,
},
overviewText: {
fontSize: moderateScale(13),
lineHeight: verticalScale(19),
color: Colors.textSecondary,
},
patternsSection: {
gap: verticalScale(10),
},
sectionHeaderRow: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingHorizontal: moderateScale(4),
},
sectionHeading: {
fontSize: moderateScale(15),
fontWeight: "700",
color: Colors.textPrimary,
},
countBadge: {
backgroundColor: "#E8F5E9",
paddingHorizontal: moderateScale(10),
paddingVertical: verticalScale(4),
borderRadius: moderateScale(12),
},
countBadgeText: {
fontSize: moderateScale(12),
fontWeight: "600",
color: Colors.primaryDark,
},
patternsList: {
gap: verticalScale(12),
},
patternCard: {
backgroundColor: "#FFFFFF",
borderRadius: moderateScale(18),
borderWidth: 1,
borderColor: "#E2EAE5",
overflow: "hidden",
},
patternRow: {
minHeight: moderateScale(68),
flexDirection: "row",
alignItems: "center",
padding: moderateScale(14),
},
pressed: {
opacity: 0.72,
},
iconWrapper: {
width: moderateScale(40),
height: moderateScale(40),
borderRadius: moderateScale(20),
justifyContent: "center",
alignItems: "center",
backgroundColor: "#E8F5E9",
},
levelNumber: {
fontSize: moderateScale(16),
fontWeight: "800",
color: Colors.primaryDark,
},
textContainer: {
flex: 1,
marginLeft: moderateScale(12),
marginRight: moderateScale(10),
},
title: {
fontSize: moderateScale(15),
fontWeight: "600",
color: Colors.textPrimary,
marginBottom: verticalScale(2),
},
description: {
fontSize: moderateScale(12),
color: Colors.textSecondary,
},
});