import type { FilterType } from './filterType.js'; interface StackOverlappingLabelsOptions { startPositionKey: keyof FilterType; labelWidthKey: keyof FilterType; padding?: number; } type StackOverlappingLabelsMapReturnType = Record; type StackOverlappingLabelsArrayItemReturnType = T & { stackIndex: number; }; type StackOverlappingLabelsArrayReturnType = Array< StackOverlappingLabelsArrayItemReturnType >; export function stackOverlappingLabelsArray>( items: T[], options: StackOverlappingLabelsOptions, ): StackOverlappingLabelsArrayReturnType { const groups = stackOverlappingLabels(items, options); return groups.flatMap((group) => { let i = 0; return group.map((item: any) => { const stackIndex = i; if (item.assignment) { i++; } return { ...item, stackIndex, }; }); }); } export function stackOverlappingLabelsMap>( items: T[], options: StackOverlappingLabelsOptions & { idKey: keyof T }, ): StackOverlappingLabelsMapReturnType { const { idKey } = options; const groups = stackOverlappingLabels(items, options); const stackMap: StackOverlappingLabelsMapReturnType = {}; for (const group of groups) { let i = 0; for (const item of group) { const key = item[idKey]; if (key === undefined || key === null) { throw new Error( `Invalid or missing idKey value for item: ${JSON.stringify(item)}`, ); } stackMap[key as string | number] = i; if (item.assignment) i++; } } return stackMap; } function stackOverlappingLabels>( items: T[], options: StackOverlappingLabelsOptions, ) { const { startPositionKey, labelWidthKey, padding = 0 } = options; const groups: any[][] = []; let currentGroup: any[] = []; let lastInPixel = 0; for (const item of items) { const startPosition = item[startPositionKey]; const labelWidth = item[labelWidthKey]; if (startPosition < lastInPixel) { currentGroup.push(item); } else { if (currentGroup.length > 0) { groups.push(currentGroup); } currentGroup = [item]; } lastInPixel = startPosition + labelWidth + padding; } if (currentGroup.length > 0) { groups.push(currentGroup); // Push the last group after the loop } return groups; }