<?php
/**
 * Gets the screen label for the given deployment item.
 *
 * @package EaLab\WP_Deployer
 */

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

/**
 * Requires.
 */
require_once __DIR__ . '/../utility/component-based-object.php';

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Exception\WpDeployException;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\TrackingManager;

/**
 * Gets the screen label for the deployment item with the given UUID.
 *
 * @param string              $item_full_uuid The full UUID of the trackable item.
 * @param array<string,mixed> $context The context of the form.
 *
 * @return string The screen label for the given trackable item.
 *
 * @deprecated
 */
function deploy_sync_content__deployment_item__screen_label( string $item_full_uuid = '', array $context ): string {
	if ( empty( $item_full_uuid ) ) {
		return '';
	}

	$tracking_manager = deploy_sync_content__get_root_cbo( TrackingManager::class );

	if ( empty( $tracking_manager ) ) {
		return esc_html( $item_full_uuid );
	}

	$item_tracker = $tracking_manager->getItemTrackerByItemFullUuid( $item_full_uuid );

	if ( empty( $item_tracker ) ) {
		return esc_html( $item_full_uuid );
	}

	try {
		$trackable_item = $item_tracker->loadTrackableItem( $item_full_uuid );

		return esc_html( $trackable_item->getScreenLabel() ?? $item_full_uuid );
	} catch ( WpDeployException $e ) {
		return esc_html( $item_full_uuid );
	}
}
