import React, { useMemo } from "react"; import { ScrollView, Text } from "react-native"; import type { DetailRendererProps } from "../../types/renderer.types"; import { bareAttributeKey, findAttributePresentationRule, getEmptyDisplayText, presentationDisplayOrder, shouldRenderAttribute, } from "../../utils/attribute-presentation"; import { asRecord, asRecordArray, asString, asStringArray, humanizeKey } from "../helpers/value"; import { BulletList } from "../shared/BulletList"; import { ChipList } from "../shared/ChipList"; import { ContentValue } from "../shared/ContentValue"; import { Hero } from "../shared/Hero"; import { ImageGallery } from "../shared/ImageGallery"; import { KeyValueGrid } from "../shared/KeyValueGrid"; import { ObjectSections } from "../shared/ObjectSections"; import { SectionCard } from "../shared/SectionCard"; const RESERVED_KEYS = new Set([ "point_name", "name", "title", "alt_name_1", "alt_name_2", "point_images", "images", "code", "sort_order", "link", ]); function isStringArray(value: unknown): value is string[] { return Array.isArray(value) && value.every((item) => typeof item === "string"); } function hasRenderableMethodValues(value: unknown): boolean { const record = asRecord(value); return ["needling", "needling_response", "moxibustion", "additional_method"].some( (key) => typeof record[key] === "string" && record[key].trim().length > 0, ); } export function AcuPointDetailRenderer({ content, data, definitions, presentationAttributes, }: DetailRendererProps) { const orderedAttributes = useMemo(() => { const rootDefinitions = definitions.filter((definition) => definition.is_root); const definitionByBareKey = new Map( rootDefinitions.map((definition) => [ definition.code || bareAttributeKey(definition.attribute_key), definition, ]), ); const contractIndex = new Map( presentationAttributes.map((rule, index) => [rule.attribute_key, index]), ); const keys: string[] = []; const seen = new Set(); for (const definition of rootDefinitions) { const key = definition.code || bareAttributeKey(definition.attribute_key); if (!key || seen.has(key)) continue; seen.add(key); keys.push(key); } // Backward-compatible fallback when definitions are incomplete: add only // contract attributes that correspond to reconstructed root data keys. for (const rule of presentationAttributes) { const key = bareAttributeKey(rule.attribute_key); if (!key || seen.has(key) || !Object.prototype.hasOwnProperty.call(data, key)) continue; seen.add(key); keys.push(key); } return keys .flatMap((key) => { if (RESERVED_KEYS.has(key)) return []; const value = data[key]; const definition = definitionByBareKey.get(key); const rule = findAttributePresentationRule(definition?.attribute_key ?? key, presentationAttributes); if (!shouldRenderAttribute(value, rule)) return []; const label = definition?.label?.trim() || humanizeKey(key); const emptyText = getEmptyDisplayText(value, rule); const index = rule ? contractIndex.get(rule.attribute_key) ?? 0 : 0; const order = presentationDisplayOrder(rule, definition?.sort_order, index); return [{ key, label, value, emptyText, order }]; }) .sort((a, b) => a.order - b.order || a.label.localeCompare(b.label)); }, [data, definitions, presentationAttributes]); const renderAttributeValue = (key: string, value: unknown, emptyText: string | null) => { if (key === "point_type" && isStringArray(value)) { return ; } if ((key === "indications" || key === "actions") && isStringArray(value)) { return ; } if ((key === "channel" || key === "location" || key === "location_note") && typeof value === "string") { return ( {value} ); } if (key === "caution" && typeof value === "string") { return ( {value} ); } if (key === "regional_anatomy" && value !== null && typeof value === "object" && !Array.isArray(value)) { return ; } if (key === "methods" && hasRenderableMethodValues(value)) { const methods = asRecord(value); return ( ); } // Specialized-field fallback: any unexpected runtime shape is still // rendered through the generic reference-aware value renderer. return ; }; return ( {orderedAttributes.map((attribute) => ( {renderAttributeValue(attribute.key, attribute.value, attribute.emptyText)} ))} ); }