<?php
/**
 * Gets the import data for the given bundle ID for the interactive form.
 *
 * @package EaLab\WP_Deployer
 */

use EaLab\WP_Deployer\vendor\Dakwamine\Component\ComponentBasedObject;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Exception\WpDeployException;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Tracking\TrackingManager;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\FileManager;

/**
 * Gets the import data for the given bundle ID for the interactive form.
 *
 * @param string $bundle_id The bundle ID.
 * @param array<string,mixed> $context Context info. Useful to pass additional context info such as "form_context_tags".
 *
 * @return array Data for the interactive form.
 *
 * @throws WpDeployException Deploy & Sync Content exceptions.
 */
function deploy_sync_content__interactive_form__get_data__import( string $bundle_id, ?array $context = array() ): array {
	if ( empty( $bundle_id ) ) {
		throw new WpDeployException( esc_html__( 'No "bundle ID" given.', 'deploy-sync-content' ) );
	}

	$context['bundle_id'] = $bundle_id;

	$tracking_manager = ComponentBasedObject::getRootComponentByClassName( TrackingManager::class, true );

	$file_manager = ComponentBasedObject::getRootComponentByClassName( FileManager::class, true );

	// Load the bundle from disk.
	$deployment_bundle = $file_manager->loadDeploymentItemsData(
		$context['bundle_id'],
		$file_manager->getAllAvailableUuidsForImport( $context['bundle_id'] )
	);

	if ( null === $deployment_bundle ) {
		// translators: %s is a bundle ID.
		throw new WpDeployException( esc_html( sprintf( __( 'Could not load the bundle “%s” from the file system.', 'deploy-sync-content' ), $bundle_id ) ) );
	}

	require_once __DIR__ . '/interactive-form--get-data.php';

	// "form_context_tags" may come from the args.
	$context['form_context'] = 'import';

	return deploy_sync_content__interactive_form__get_data(
		$deployment_bundle,
		$context
	);
}
