<?php
/**
 * Human-readable operation summaries.
 *
 * @package ScheduledChanges
 */

namespace ScheduledChanges\Domain\Operation;

/**
 * Derives plan prose from executable operations.
 */
final class HumanSummaryGenerator {
	public function summarize( OperationGraph $graph ): string {
		$operations = $graph->topologicalOrder();

		if ( array() === $operations ) {
			return 'No product changes are planned.';
		}

		$summaries = array_map(
			static fn ( Operation $operation ): string => rtrim( $operation->summary(), ". \t\n\r\0\x0B" ),
			$operations
		);

		if ( 1 === count( $summaries ) ) {
			return $summaries[0] . '.';
		}

		$last = array_pop( $summaries );

		return implode( '; then ', $summaries ) . '; then ' . $last . '.';
	}
}
