<?php
/**
 * Displays the label, and if needed, the UUID of the deployment item.
 *
 * @package EaLab\WP_Deployer
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Requires.
 */
require_once __DIR__ . '/../utility/deployment-item.php';

/**
 * Displays the label, and if needed, the UUID of the deployment item.
 *
 * @param string              $label The label of the item.
 * @param string              $item_full_uuid The item full UUID.
 * @param array<string,mixed> $context The context of the form.
 */
function deploy_sync_content__deployment_item__label_uuid( string $label, string $item_full_uuid, array $context ): void {
	$prefix = function ( string $label ) use ( $context ): string {
		return ( $context['screen_label_prefix'] ?? '' ) . $label;
	};

	$parts = explode( '.', $item_full_uuid, 3 );

	if ( count( $parts ) !== 2 ) {
		// The UUID is invalid.
		echo esc_html( $prefix( $item_full_uuid ) );

		return;
	}

	if ( $label === $item_full_uuid ) {
		// There is no specific label for this item.
		echo esc_html(  $prefix( $label ) );

		return;
	}

	if ( $label === $parts[1] ) {
		// No need to add the UUID info to the label.
		echo esc_html( $prefix( $label ) );

		return;
	}

	$prefixed_label = $prefix( $label );

	// Append the UUID to the label.
	?>
	<div class="deployment_item__label_uuid">
		<div><?php echo esc_html( $prefixed_label ) ?></div>
		<div class="uuid"><?php echo esc_html( $item_full_uuid ) ?></div>
	</div>
	<?php
}
