import React, { useMemo } from "react"; import type { AttributeDefinitionResponse } from "../../types/content.types"; import type { TemplateAttributePresentationContract } from "../../types/presentation.types"; import { bareAttributeKey, createAttributePresentationMap, getAttributePresentationRule, presentationDisplayOrder, shouldRenderAttribute, } from "../../utils/attribute-presentation"; import { getNavigableContentReferences } from "../../utils/content-reference"; import { ContentValue } from "./ContentValue"; import { SectionCard } from "./SectionCard"; interface ContentLinkAttributeSectionsProps { data: Record; definitions?: readonly AttributeDefinitionResponse[]; presentationAttributes?: readonly TemplateAttributePresentationContract[]; excludeAttributeKeys?: readonly string[]; } function containsNavigableReference(value: unknown): boolean { if (getNavigableContentReferences(value).length > 0) return true; if (Array.isArray(value)) return value.some(containsNavigableReference); if (value && typeof value === "object") { return Object.values(value as Record).some(containsNavigableReference); } return false; } const DEFAULT_EXCLUDED_KEYS = new Set([ "point_codes", "source_point", "source_points", "treatment", "treatment_principle", ]); export function ContentLinkAttributeSections({ data, definitions = [], presentationAttributes = [], excludeAttributeKeys = [], }: ContentLinkAttributeSectionsProps) { const rules = useMemo( () => createAttributePresentationMap(presentationAttributes), [presentationAttributes], ); const excluded = useMemo(() => { const set = new Set(DEFAULT_EXCLUDED_KEYS); for (const key of excludeAttributeKeys) { set.add(bareAttributeKey(key)); } return set; }, [excludeAttributeKeys]); const items = useMemo(() => { const roots = [...definitions] .filter((definition) => definition.is_root) .sort((a, b) => a.sort_order - b.sort_order || a.attribute_key.localeCompare(b.attribute_key)); const orderedKeys = roots.length > 0 ? roots.map((definition) => definition.attribute_key) : Object.keys(data); const seen = new Set(); const output: Array<{ key: string; label: string; value: unknown; order: number; }> = []; for (const key of orderedKeys) { if (seen.has(key)) continue; seen.add(key); const definition = roots.find((item) => item.attribute_key === key); const dataKey = definition?.code || bareAttributeKey(key); if (excluded.has(bareAttributeKey(key)) || excluded.has(dataKey)) continue; const value = data[dataKey] !== undefined ? data[dataKey] : data[key]; if (!containsNavigableReference(value)) continue; const rule = getAttributePresentationRule(key, rules); if (!shouldRenderAttribute(value, rule)) continue; output.push({ key, label: definition?.label?.trim() || dataKey.replace(/_/g, " "), value, order: presentationDisplayOrder( rule, definition?.sort_order, output.length, ), }); } return output.sort( (a, b) => a.order - b.order || a.key.localeCompare(b.key), ); }, [data, definitions, excluded, rules]); if (!items.length) return null; return ( <> {items.map((item) => ( ))} ); }