import type { SymconObject } from "../types.ts"; export interface SymconTree { nodes: Map; children: Map; } export interface SymconTreeRow { node: SymconObject; depth: number; hasChildren: boolean; expanded: boolean; index: number; siblings: number; } // Symcon orders siblings by position, then object type, then name (IPS_SetPosition). const collator = new Intl.Collator("de", { numeric: true, sensitivity: "base" }); export const SYMCON_SYSTEM_ROOT = -1; const SYSTEM_GROUPS = [ { type: 1, name: "I/O" }, { type: 2, name: "Splitter" }, { type: 0, name: "Kerninstanzen" }, { type: 4, name: "Konfiguratoren" }, { type: 5, name: "Discovery" }, { type: 6, name: "Visualisierungen" }, ] as const; const isSystemInstance = (node: SymconObject): boolean => node.object_type === 1 && SYSTEM_GROUPS.some((group) => group.type === node.module_type); /** Show the main category's objects first and tuck the logical console's system branches away. * Hidden categories are never filtered; only this display copy is regrouped, never the native tree. */ function projectSymconObjects(objects: readonly SymconObject[]): readonly SymconObject[] { const native = new Map(objects.map((node) => [node.object_id, node])); const roots = objects.filter((node) => { if (!isSystemInstance(node)) return false; const seen = new Set([node.object_id]); let parent = native.get(node.parent_id); while (parent && !seen.has(parent.object_id)) { if (isSystemInstance(parent)) return false; // Keep its entire native subtree together. seen.add(parent.object_id); parent = native.get(parent.parent_id); } return true; }); if (!roots.length) return objects; const projected = new Map(native); const children = new Map(); for (const node of objects) { const siblings = children.get(node.parent_id) ?? []; siblings.push(node); children.set(node.parent_id, siblings); } const virtualCategory = (id: number, parent: number, name: string, path: string, position: number): SymconObject => ({ object_id: id, parent_id: parent, name, path, position, object_type: 0, hidden: false, icon: "", virtual: true, }); projected.set(SYMCON_SYSTEM_ROOT, virtualCategory(SYMCON_SYSTEM_ROOT, 0, "System", "System", Number.MAX_SAFE_INTEGER)); const moved = new Set(); const moveBranch = (node: SymconObject, parent: number, parentPath: string): void => { if (moved.has(node.object_id)) return; moved.add(node.object_id); const copy = { ...node, parent_id: parent, path: `${parentPath} / ${node.name}` }; projected.set(node.object_id, copy); for (const child of children.get(node.object_id) ?? []) moveBranch(child, node.object_id, copy.path); }; SYSTEM_GROUPS.forEach((group, index) => { const instances = roots.filter((node) => node.module_type === group.type); if (!instances.length) return; const id = -10 - group.type; const path = `System / ${group.name}`; projected.set(id, virtualCategory(id, SYMCON_SYSTEM_ROOT, group.name, path, index)); for (const instance of instances) moveBranch(instance, id, path); }); return [...projected.values()]; } export function indexSymconTree(objects: readonly SymconObject[]): SymconTree { const projected = projectSymconObjects(objects); const nodes = new Map(projected.map((node) => [node.object_id, node])); const children = new Map(); for (const node of projected) { const parent = nodes.has(node.parent_id) ? node.parent_id : 0; const siblings = children.get(parent) ?? []; siblings.push(node); children.set(parent, siblings); } for (const siblings of children.values()) siblings.sort((a, b) => a.position - b.position || a.object_type - b.object_type || collator.compare(a.name, b.name)); return { nodes, children }; } /** Display ancestor IDs, including virtual system groups, without splitting names into paths. */ export function symconAncestors(tree: SymconTree, objectId: number): number[] { const result: number[] = []; const seen = new Set([objectId]); let parent = tree.nodes.get(objectId)?.parent_id; while (parent && !seen.has(parent) && tree.nodes.has(parent)) { result.push(parent); seen.add(parent); parent = tree.nodes.get(parent)?.parent_id; } return result; } /** Search retains each match's displayed ancestors and opens that path without changing manual expansion. */ export function symconTreeRows(tree: SymconTree, expanded: ReadonlySet, query = ""): SymconTreeRow[] { const words = query.trim().toLocaleLowerCase("de").split(/\s+/).filter(Boolean); const matches = words.length ? new Set() : null; if (matches) { for (const node of tree.nodes.values()) { if (words.every((word) => `${node.name} ${node.path}`.toLocaleLowerCase("de").includes(word))) { matches.add(node.object_id); for (const id of symconAncestors(tree, node.object_id)) matches.add(id); } } } const rows: SymconTreeRow[] = []; const visited = new Set(); const visit = (parent: number, depth: number) => { const siblings = (tree.children.get(parent) ?? []).filter((node) => !matches || matches.has(node.object_id)); siblings.forEach((node, i) => { if (visited.has(node.object_id)) return; visited.add(node.object_id); const hasChildren = (tree.children.get(node.object_id)?.length ?? 0) > 0; const open = hasChildren && (!!matches || expanded.has(node.object_id)); rows.push({ node, depth, hasChildren, expanded: open, index: i + 1, siblings: siblings.length }); if (open) visit(node.object_id, depth + 1); }); }; visit(0, 0); return rows; }