/**
 * Generates recommended styles based on the currently selected styles
 */
export function getRecommendedStyles(
  styles: string[],
  coexistingStylesDict: Record<string, Record<string, number>>,
  size: number
): string[] {
  // Short-circuit
  if (!styles.length) {
    return [];
  }

  // Start with the currently selected styles
  const selectedStyles = new Set(
    styles.filter((style) => style in coexistingStylesDict)
  );

  // Build a set of recommended styles with weights
  const recommendedStylesWeight: Map<string, number> = new Map();
  for (const selectedStyle of selectedStyles) {
    const coexistingStyles = coexistingStylesDict[selectedStyle];
    if (coexistingStyles) {
      for (const [style, weight] of Object.entries(coexistingStyles)) {
        // If the coexisting style has not already been selected, give it more weight
        if (!selectedStyles.has(style)) {
          recommendedStylesWeight.set(
            style,
            (recommendedStylesWeight.get(style) || 0) + weight
          );
        }
      }
    }
  }

  // Sort recommended styles entries by weight
  const recommendedStyles = Array.from(recommendedStylesWeight.entries()).sort(
    ([, a], [, b]) => b - a
  );

  // Start building the output with the top coexisting styles
  return recommendedStyles.slice(0, size).map(([style]) => style);
}
