<?php
/**
 * Displays the label of a deployment item.
 *
 * @package EaLab\WP_Deployer
 */

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

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

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

/**
 * Displays the label of a deployment item.
 *
 * @param array<string,mixed> $item The deployment item.
 * @param array<string,mixed> $context The context of the form.
 *   - form_context (string): The context of the form ('import' or 'export').
 *   - bundle_id (string): The subdirectory of the bundle, only required for imports.
 *
 * @return string Markup.
 *
 * @throws WpDeployException Could not build the label.
 */
function deploy_sync_content__deployment_item__label_from_item( array $item, array $context ): string {
	$item_full_uuid = deploy_sync_content__get_uuid_from_deployment_item( $item );

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

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

	if ( empty( $item_full_uuid ) ) {
		return '';
	}

	// The item tracker is required to get the formatted screen label.
	// So in import scenarios where the item tracker is not available,
	// we just display the UUID.
	$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 );
	}

	if ( 'export' === $context['form_context'] ) {
		/**
		 * Loads the label from the exported data.
		 *
		 * @return string Markup.
		 *
		 * @throws WpDeployException Could not build the label.
		 */
		$loadFromFileItem = function () use ( $context, $item_full_uuid, $item ) {
			// Load the label from the exported data.
			$screen_label_formatter_manager = deploy_sync_content__get_root_cbo( ScreenLabelFormatterManager::class );

			if ( empty( $context['bundle_id'] ) ) {
				throw new WpDeployException( esc_html__( 'Unknown bundle identifier. A bundle identifier (bundle_id) is required when reviewing an existing export.', 'deploy-sync-content' ) );
			}

			$label = $screen_label_formatter_manager->formatFromFileItem( $context['bundle_id'], $item );

			return esc_html( ! empty( $label ) ? $label : $item_full_uuid );
		};

		if ( in_array( 'review', ( $context['form_context_tags'] ?? array() ), true ) ) {
			// Load the label from the exported data.
			return $loadFromFileItem();
		}

		try {
			return $loadFromFileItem();
		} catch ( WpDeployException $e ) {
			// The label could not be loaded from the exported data.
			// We try to load it from the database.
			try {
				// Load from the database.
				$trackable_item = $item_tracker->loadTrackableItem( $item_full_uuid );

				if ( empty( $trackable_item ) ) {
					// We are in an export view, but the item is not available anymore in the database.
					return esc_html( $item_full_uuid );
				}

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

	if ( 'import' === $context['form_context'] ) {
		$screen_label_formatter_manager = deploy_sync_content__get_root_cbo( ScreenLabelFormatterManager::class );

		if ( empty( $context['bundle_id'] ) ) {
			throw new WpDeployException( esc_html__( 'Unknown bundle identifier. A bundle identifier (bundle_id) is required when reviewing an existing export.', 'deploy-sync-content' ) );
		}

		$label = $screen_label_formatter_manager->formatFromFileItem( $context['bundle_id'], $item );

		return esc_html( ! empty( $label ) ? $label : $item_full_uuid );
	}

	// Should never happen, but just in case.
	return esc_html( $item_full_uuid );
}
