function formatChangeTypeLegend()

in src/helpers/whatif.ts [119:162]


function formatChangeTypeLegend(
  builder: ColorStringBuilder,
  resourceChanges: WhatIfChange[],
): void {
  if (!resourceChanges.length) return;

  const changeTypeSet = new Set<ChangeType>();

  function populateChangeTypeSet(
    propertyChanges: WhatIfPropertyChange[],
  ): void {
    if (!propertyChanges.length) return;

    for (const propertyChange of propertyChanges) {
      const propertyChangeType = propertyChange.propertyChangeType;
      changeTypeSet.add(propertyChangeToChangeType[propertyChangeType]);
      populateChangeTypeSet(propertyChange.children ?? []);
    }
  }

  for (const resourceChange of resourceChanges) {
    changeTypeSet.add(resourceChange.changeType);
    populateChangeTypeSet(resourceChange.delta ?? []);
  }

  const changeTypes = Array.from(changeTypeSet).sort(
    (a, b) => changeTypeToWeight[a] - changeTypeToWeight[b],
  );

  builder.append("Resource and property changes are indicated with ");
  builder.appendLine(
    changeTypes.length === 1 ? "this symbol:" : "these symbols:",
  );

  for (const changeType of changeTypes) {
    const changeTypeSymbol = changeTypeToSymbol[changeType];
    const changeTypeColor = changeTypeToColor[changeType];
    formatIndent(builder);
    builder.append(changeTypeSymbol, changeTypeColor).append(Symbol.WhiteSpace);
    builder.appendLine(
      changeType.charAt(0).toUpperCase() + changeType.slice(1),
    );
  }
}