// Rôle : regroupe les libellés et helpers d'affichage purs de l'arbre d'objectifs. import { GOAL_TREE_COLOR_VALUES, formatGoalTreeNumber, getGoalTreeChildren, getGoalTreeDescendantIds, getGoalTreeParentIds } from "./goalTreeUtils.js"; export const KIND_LABELS = { step: "Étape", text: "Texte", checklist: "Checklist" }; export const RESET_LABELS = { none: "Aucun", daily: "Quotidien", weekly: "Hebdomadaire" }; export const KIND_ICONS = { step: "diagram", text: "notepad", checklist: "checklist" }; export const GOAL_TREE_VIEW_SCALE = 0.7; export const BASE_TOKEN = "base"; export function getGoalTreeKindIcon(kind) { return KIND_ICONS[kind] || "diagram"; } export function getGoalTreeNodeIcon(node) { return node.kind === "step" ? node.icon || "cube" : getGoalTreeKindIcon(node.kind); } export function getGoalTreeNodeColor(node) { return node.kind === "step" ? node.color || "gold" : "cyan"; } export function getGoalTreeNodeIconColor(node) { return GOAL_TREE_COLOR_VALUES[getGoalTreeNodeColor(node)] || GOAL_TREE_COLOR_VALUES.gold; } export function getDefaultGoalTreeItem(kind, context, textContent = {}, parentId = "") { return { id: context.uid("goalItem"), label: textContent.itemLabelPlaceholder || "Élément", formula: kind === "checklist" ? (parentId ? BASE_TOKEN : "1") : undefined, qtyTarget: kind === "checklist" ? 1 : undefined, qtyCurrent: kind === "checklist" ? 0 : undefined }; } export function getGoalTreeNodeTooltip(node) { return `${node.title} - ${formatGoalTreeNumber(node.progress.current)} / ${formatGoalTreeNumber(node.progress.target)}`; } export function isSingleValueGoalTreeNode(node) { return Math.max(1, node?.progress?.target || node?.computedQtyTarget || node?.qtyTarget || 1) <= 1; } export function getEffectiveGoalTreeProgressMode(node, children = []) { if (node?.kind !== "step") return "auto"; if (node.progressMode) return node.progressMode; if (!children.length) return "manual"; return children.every(isSingleValueGoalTreeNode) ? "manual" : "auto"; } export function getVisibleGoalTreeNodeIds(nodes, parentId = "", ancestorVisible = true) { if (!ancestorVisible) return new Set(); return getGoalTreeChildren(nodes, parentId).reduce((visibleIds, node) => { visibleIds.add(node.id); getVisibleGoalTreeNodeIds(nodes, node.id, !node.collapsed).forEach((nodeId) => visibleIds.add(nodeId)); return visibleIds; }, new Set()); } export function getVisibleGoalTreeEdges(nodes) { const visibleNodeIds = getVisibleGoalTreeNodeIds(nodes); const nodeMap = new Map(nodes.map((node) => [node.id, node])); const primaryEdges = nodes.flatMap((node) => { if (!visibleNodeIds.has(node.id) || node.collapsed) return []; const children = getGoalTreeChildren(nodes, node.id); return children.filter((child) => visibleNodeIds.has(child.id)).map((child) => ({ from: node.id, to: child.id })); }); const linkedEdges = nodes.flatMap((node) => getGoalTreeParentIds(node) .filter((parentId) => parentId !== node.parentId) .filter((parentId) => visibleNodeIds.has(parentId) && visibleNodeIds.has(node.id) && !nodeMap.get(parentId)?.collapsed) .map((parentId) => ({ from: parentId, to: node.id, grouped: false }))); return [...primaryEdges, ...linkedEdges]; } export function getGoalTreeAncestorIds(nodes, nodeId) { const nodeMap = new Map(nodes.map((node) => [node.id, node])); const ancestors = new Set(); let current = nodeMap.get(nodeId); while (current?.parentId && !ancestors.has(current.parentId)) { ancestors.add(current.parentId); current = nodeMap.get(current.parentId); } return ancestors; } export function getGoalTreeConvergenceKeepOpenIds(nodes, targetId) { const targetBranchIds = [targetId, ...getGoalTreeDescendantIds(nodes, targetId)]; const keepOpenIds = new Set(); targetBranchIds.forEach((nodeId) => { const node = nodes.find((entry) => entry.id === nodeId); getGoalTreeParentIds(node) .filter((parentId) => parentId !== node?.parentId) .forEach((parentId) => { keepOpenIds.add(parentId); getGoalTreeAncestorIds(nodes, parentId).forEach((ancestorId) => keepOpenIds.add(ancestorId)); }); }); return keepOpenIds; } export function getGoalTreeLinkedControlIds(nodes, parentId) { const controlledNodes = nodes.filter((node) => getGoalTreeParentIds(node).includes(parentId)); return new Set([ parentId, ...controlledNodes.flatMap((node) => getGoalTreeParentIds(node)) ]); }