import React from "react"; import { Pressable, ScrollView, StyleSheet, Text, View } from "react-native"; import { Ionicons } from "@expo/vector-icons"; import { router } from "expo-router"; import { moderateScale, verticalScale } from "react-native-size-matters"; import { Colors } from "@/theme"; import type { DetailRendererProps } from "../../types/renderer.types"; import { ContentLinkAttributeSections } from "../shared/ContentLinkAttributeSections"; import { Hero } from "../shared/Hero"; import { asRecordArray, asString, asStringArray } from "../helpers/value"; function resolveSequenceTargetTemplate( currentModule: string, sectionTitle: string, sectionRecord: Record, index: number, ): string { // 1. Direct template_code in data section if present const directTemplate = asString(sectionRecord.template_code) || asString( (sectionRecord.link as Record | undefined)?.target ? ((sectionRecord.link as Record).target as Record).template_code : undefined, ); if (directTemplate) return directTemplate; // 2. Resolve by semantic title keyword const normalized = sectionTitle.toLowerCase(); if (normalized.includes("generating")) return `${currentModule}_ct0002`; if (normalized.includes("overacting")) return `${currentModule}_ct0003`; if (normalized.includes("insulting")) return `${currentModule}_ct0004`; // 3. Fallback by ordinal index return `${currentModule}_ct000${index + 2}`; } function extractParagraphs(value: unknown): string[] { if (Array.isArray(value)) { return value .map((item) => asString(item).trim()) .filter((item) => item.length > 0); } const str = asString(value).trim(); return str.length > 0 ? [str] : []; } export function FiveElementsDetailRenderer({ moduleCode, templateCode, content, data, definitions, presentationAttributes, }: DetailRendererProps) { const currentModule = moduleCode || "m0019"; const title = asString(data.name) || asString(data.title) || content.title || ""; const code = asString(data.code) || templateCode || content.slug || ""; // Dynamic Overview resolution from data only (generic fallback hierarchy) const overviewCandidates = [ extractParagraphs(data.overview), extractParagraphs(data.content), extractParagraphs(data.description), extractParagraphs(content.summary), ]; const overviewParagraphs = overviewCandidates.find((candidates) => candidates.length > 0) ?? []; const rawSections = asRecordArray(data.sections); // The Identification page is the parent/entry screen containing sequence links const isIdentificationPage = templateCode.toLowerCase() === `${currentModule.toLowerCase()}_ct0001` || code.toLowerCase().includes("identification") || title.toLowerCase().includes("identification"); // ---------------------------------------------------- // SCREEN 1: Identification Page with Dynamic Sequence Buttons // ---------------------------------------------------- if (isIdentificationPage) { return ( {/* Overview Card (rendered only when overview/content exists in data) */} {overviewParagraphs.length > 0 ? ( Overview {overviewParagraphs.map((para, idx) => ( 0 && styles.paragraphSpacing]} > {para} ))} ) : null} {/* Dynamic Sequences List from data.sections */} {rawSections.length > 0 ? ( Sequences {rawSections.map((sec, idx) => { const sectionTitle = asString(sec.title) || `Sequence ${idx + 1}`; const sectionItems = asStringArray(sec.content); const count = sectionItems.length; const targetTemplate = resolveSequenceTargetTemplate( currentModule, sectionTitle, sec, idx, ); return ( router.push( `/modules/${encodeURIComponent(currentModule)}/${encodeURIComponent(targetTemplate)}` as never, ) } style={({ pressed }) => [styles.sequenceRow, pressed && styles.pressed]} > {idx + 1} {sectionTitle} {count > 0 ? ( {count} patterns ) : null} ); })} ) : null} ); } // ---------------------------------------------------- // SCREEN 2: Leaf Node Data Page (Specific Sequence Patterns) // ---------------------------------------------------- return ( {/* Overview Card (rendered only when overview/content exists in data) */} {overviewParagraphs.length > 0 ? ( Overview {overviewParagraphs.map((para, idx) => ( 0 && styles.paragraphSpacing]} > {para} ))} ) : null} {/* Numbered Pattern Cards from data.sections */} {rawSections.length > 0 ? ( Patterns & Manifestations {rawSections.map((sec, idx) => { const sectionTitle = asString(sec.title) || `Pattern ${idx + 1}`; const sectionContent = extractParagraphs(sec.content); return ( {idx + 1} {sectionTitle} {sectionContent.length > 0 ? ( {sectionContent.map((text, cIdx) => ( {text} ))} ) : null} ); })} ) : null} ); } const styles = StyleSheet.create({ container: { 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, }, paragraphSpacing: { marginTop: verticalScale(10), }, sequencesContainer: { gap: verticalScale(10), }, sectionHeading: { fontSize: moderateScale(15), fontWeight: "700", color: Colors.textPrimary, marginLeft: moderateScale(4), marginBottom: verticalScale(2), }, sequenceRow: { minHeight: moderateScale(60), flexDirection: "row", alignItems: "center", padding: moderateScale(14), backgroundColor: "#FFFFFF", borderRadius: moderateScale(14), borderWidth: 1, borderColor: "#E2EAE5", }, pressed: { opacity: 0.72, }, sequenceNumberWrapper: { width: moderateScale(36), height: moderateScale(36), borderRadius: moderateScale(18), justifyContent: "center", alignItems: "center", backgroundColor: "#E8F5E9", }, sequenceNumber: { fontSize: moderateScale(15), fontWeight: "800", color: Colors.primaryDark, }, sequenceTextWrapper: { flex: 1, marginLeft: moderateScale(12), marginRight: moderateScale(8), }, sequenceTitle: { fontSize: moderateScale(15), fontWeight: "600", color: Colors.textPrimary, }, badgeWrapper: { backgroundColor: "#F3F4F6", paddingHorizontal: moderateScale(8), paddingVertical: verticalScale(4), borderRadius: moderateScale(8), marginRight: moderateScale(6), }, badgeText: { fontSize: moderateScale(11), fontWeight: "600", color: Colors.textSecondary, }, sectionsContainer: { gap: verticalScale(12), }, patternCard: { backgroundColor: "#FFFFFF", borderRadius: moderateScale(14), borderWidth: 1, borderColor: "#E5E7EB", overflow: "hidden", }, patternHeader: { flexDirection: "row", alignItems: "center", paddingHorizontal: moderateScale(14), paddingVertical: verticalScale(12), backgroundColor: "#F9FAFB", borderBottomWidth: 1, borderBottomColor: "#EEF2F6", gap: moderateScale(10), }, numberBadge: { width: moderateScale(28), height: moderateScale(28), borderRadius: moderateScale(14), backgroundColor: "#E8F5E9", justifyContent: "center", alignItems: "center", }, numberText: { fontSize: moderateScale(14), fontWeight: "800", color: Colors.primaryDark, }, patternTitle: { flex: 1, fontSize: moderateScale(14), fontWeight: "700", color: Colors.textPrimary, }, patternBody: { padding: moderateScale(14), gap: verticalScale(10), }, contentText: { fontSize: moderateScale(13), lineHeight: verticalScale(19), color: Colors.textSecondary, }, });